Link to home
Start Free TrialLog in
Avatar of suelow
suelow

asked on

some C programming problems...

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.
Avatar of ozo
ozo
Flag of United States of America image

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.
Avatar of agolan
agolan

I'll second ozo.
ASKER CERTIFIED SOLUTION
Avatar of JYoungman
JYoungman

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of suelow

ASKER

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.
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.
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

Avatar of suelow

ASKER

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;
}


Avatar of suelow

ASKER

ozo,
Is that the best way to solve these kind of problems ?
Depending on your compiler, your Q2) program may do interesting things with
{|}~{|}~abcdefg
or
0123456789abcdefghijklmnopqrstuvwxyz
or
[\]_`[\]_`abcdefghijklmnopqrstuvwxyz
Avatar of suelow

ASKER

ozo,
I'm using Turbo C while compiling the source code. The output I got for Q2 is what I expected.