Solved

some C programming problems...

Posted on 1998-06-29
10
311 Views
Last Modified: 2010-04-15
1.)Dates are commonly printed in several different formats in business correspondence.  Two of the more common formats are:
     07/21/97 and July 21, 1997
Write a program that reads a date in the first format and prints the date in the second format.


2.)Write a program that reads a line of text and prints a table indicating the number of occurrences of each letter of the alphabet in the text.


3.)Implement a multipurpose sorting program in C using function pointers.  The following functions are expected.
a) bubble()  -> for sorting
b) swap()    -> to interchange two values in two locations
c) ascending() -> to verify the two values passed to this                   function are in ascending order
d) descending() -> to verify the two values passed to this                    function are in descending order
The bubble() function should be passed with the array of integers, the array size and the pointer to the function ascending() or descending() (depending upon
the user's choice).   Use bubble sort.
0
Comment
Question by:suelow
10 Comments
 
LVL 84

Expert Comment

by:ozo
ID: 1251470
If you're having problems on your final exam, we can try to help you to understand why the code you wrote is not doing what you expected,
but it would be unethical to help you cheat, or to spread Y2K bugs.
0
 
LVL 1

Expert Comment

by:agolan
ID: 1251471
I'll second ozo.
0
 
LVL 2

Accepted Solution

by:
JYoungman earned 50 total points
ID: 1251472
#!/bin/sh
# Answer to question 1.
# Requires GNU date(1).
date --date="$1"
0
 

Author Comment

by:suelow
ID: 1251473
ozo, you have misunderstood my problems, it is just an assignment of mine and now I'm searching the opinions from the experts for pointing on how to solve the above problems in more efficient way. you can ommit the Y2K problem.
0
 
LVL 84

Expert Comment

by:ozo
ID: 1251474
I'm sorry if I misunderstood.  Many of us would be happy to help you find ways to improve the efficiency of your programs,
as long as it wasn't being unfair to other students, and didn't interfere with your own efforts to learn programming.
But it hard to know where your program is being inefficient unless we can see your program first.
About the only thing appatent so far is that you might want to use a more efficient sort than bubble sort.
Perhaps a partition exchange sort or a radix sort, depending on what kind of data you're expecting.
Or just use the standard qsort function.
0
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 10

Expert Comment

by:RONSLOW
ID: 1251475
Please don't put an assignment question here and expert an expert to answer it .. we won't.

If you had asked a particular question about your assignment, then we could/can help you (eg. "I am having trouble with the bubble sort .. it seems to hang .. here is my code, can you help me find the problem?")

However, simply putting your assugnment question up verbatim as a question, with no other comments or indication of what problems you are having, just won't get a response (well, not the sort of response you want).

Try doing the assignment yourself, then come back to us with whatever problems you have with the program and/or concepts

0
 

Author Comment

by:suelow
ID: 1251476
Here are the solutions for Q1, Q2 and Q3...

Q1)
#include <stdio.h>

void main()
{
      int d, m , y;

      char *month[13] = {"Zero", "January", "February",      
                           "March", "April","May", "June",      
                           "July", "August", "September"
                     "October", "November", "December"};

      char slash;

      printf("Enter date (mm/dd/yy) >");
      scanf("%d%c%d%c%d", &m, &slash, &d, &slash, &y);

      printf("\n%s %d, 19%d", month[m], d, y);
}


Q2)
#include <stdio.h>
#include <ctype.h>

void main()
{
      char text[80];
      int alpha[26];
      int count[26];
      int i, n;

      gets(text);

      for(i = 0;i < 26;i++)
      {
            count[i] = 0;
            alpha[i] = 97 + i;
      }

      i = 0;
      while(text[i] != '\0')
      {
            n = tolower(text[i]) - 97;
            count[n] = count[n] + 1;
            i++;
      }

      for(i = 0;i < 26;i++)
      {
            if(count[i] != 0)
                  printf("The character %c occurs %d times.
                                \n", alpha[i], count[i]);
      }
}


Q3)
#include <stdio.h>
#include <conio.h>

#define MAX 50

void swap(int *, int *);

void main()

{
      void bubble(void (*)(int [], int), int[], int);
      void (*order[2])(int [], int);
      void ascending(int[], int);
      void descending(int [], int);


      int a[MAX];
      int i, n, choice;

      clrscr();
      for(i = 0;i < 50;i++)
            a[i] = 0;

      printf("Enter number of elements >");
      scanf("%d", &n);

      printf("\nEnter %d elements separate by space >", n);

      for(i = 0;i < n;i++)
            scanf("%d", &a[i]);

      order[0] = descending;
      order[1] = ascending;

      printf("\n\tMenu");
      printf("\n0.\tDescending Order.");
      printf("\n1.\tAscending Order.");
      printf("\nYour choice >");
      scanf("%d", &choice);

      bubble(order[choice], a, n);

      for(i = 0;i < n;i++)
            printf("%d ", a[i]);
}



void bubble(void (*x)(int y[], int n), int y[], int n)
{
      (*x)(y, n);
}

void ascending(int y[], int n)
{
      int i, j;

      for(i = 0;i < n - 1;i++)
            for(j = 0;j <= n - 2;j++)
            {
                  if(y[j] > y[j + 1])
                        swap(&y[j], &y[j + 1]);
            }
}

void descending(int y[], int n)
{
      int i, j;

      for(i = 0;i < n - 1;i++)
            for(j = 0;j <= n - 2;j++)
            {
                  if(y[j] < y[j + 1])
                        swap(&y[j], &y[j + 1]);
            }
}

void swap(int *a, int *b)
{
      int temp;
      temp = *a;
      *a = *b;
      *b = temp;
}


0
 

Author Comment

by:suelow
ID: 1251477
ozo,
Is that the best way to solve these kind of problems ?
0
 
LVL 84

Expert Comment

by:ozo
ID: 1251478
Depending on your compiler, your Q2) program may do interesting things with
{|}~{|}~abcdefg
or
0123456789abcdefghijklmnopqrstuvwxyz
or
[\]_`[\]_`abcdefghijklmnopqrstuvwxyz
0
 

Author Comment

by:suelow
ID: 1251479
ozo,
I'm using Turbo C while compiling the source code. The output I got for Q2 is what I expected.
0

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
The goal of this video is to provide viewers with basic examples to understand and use pointers in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them in the C programming language.

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now