We help IT Professionals succeed at work.

Check out our new AWS podcast with Certified Expert, Phil Phillips! Listen to "How to Execute a Seamless AWS Migration" on EE or on your favorite podcast platform. Listen Now

x

some C programming problems...

suelow
suelow asked
on
Medium Priority
349 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.
Comment
Watch Question

ozo
CERTIFIED EXPERT
Most Valuable Expert 2014
Top Expert 2015

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

Commented:
I'll second ozo.
Unlock this solution with a free trial preview.
(No credit card required)
Get Preview

Author

Commented:
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.
ozo
CERTIFIED EXPERT
Most Valuable Expert 2014
Top Expert 2015

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

Commented:
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

Author

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


Author

Commented:
ozo,
Is that the best way to solve these kind of problems ?
ozo
CERTIFIED EXPERT
Most Valuable Expert 2014
Top Expert 2015

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

Author

Commented:
ozo,
I'm using Turbo C while compiling the source code. The output I got for Q2 is what I expected.
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a free trial preview!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.