Thread: c programming problem

  1. #1
    Registered User
    Join Date
    Nov 2010
    Posts
    6

    c programming problem

    I am trying to do a few practice problems from C by Dissection and can't figure out this problem at all.


    The problem asks "write a program that reads n integers into an array, then prints on a separate line the value of each distinct element along with the number of times it occurs. the values should be printed in descending order."

    it gives the example that the user inputs:

    -7 -7 3 3 5 -7

    and the program prints:

    5 occurs 1 times
    3 occurs 2 times
    -7 occurs 3 times


    it also asks that I use dynamic memory allocation and pointer arithmetic


    if anyone can figure out this program that would be greatly appreciated.

  2. #2
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Isn't it the point of the exercise that you figure it out?

    Give it your best try, if you get stuck post your code (in code tags, please) and maybe we can help...

  3. #3
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    What part do you need help with? Have you attempted to solve this on your own yet? If so, where's the code?
    bit∙hub [bit-huhb] n. A source and destination for information.

  4. #4
    Registered User
    Join Date
    Nov 2010
    Posts
    6

    Trying

    am working on some of it. The code I have is:

    Code:
    /* Program that reads n integers into an array and prints them in descending order */
    
    #include <stdio.h>
    #include <stdlib.h>
    int main(void)
    {	int *a, i, n, sum = 0;
    	printf("\n%s%s',
    		"An array will be created.\n\n",
    		"Input an array size n followed by n integers: ");
    	scanf("%d", &n);
    	
    	a = calloc(n, sizeof(int)); /* get space for n ints*/
    for (i= 0; i< n; ++i)
    scanf("%d", &a[i]);
    for (i= 0; i< n; ++i)
    sum += a[i];
    free(a); /* free the space */
    printf("\n%s%7d\n%s%7d\n\n",
    " Number of elements:", n, "Sum of the elements:", sum);
    return 0;
    }
    I know this isn't the right program but it is close. instead of figuring out the occurences it sums them though.

    I know they say that people learn by doing but with programming (for me) if I can see how a problem is done then I can know how to do one like it.
    Last edited by Salem; 11-19-2010 at 10:40 PM. Reason: tag fixing

  5. #5
    Registered User
    Join Date
    Sep 2010
    Posts
    25
    If you have a copy of K&R's C, try looking at Chapter 1, Exercises 1-9 to 1-12 for examples of simple counting and reporting; also, for the core of the problem maybe consider Chapter 6.3 to 6.5 (arrays and pointers to structures), that section has a more involved set of examples which is closer to your problem, but doesn't cover the basic matter of one line per reported characteristic.

    I'm sorry I cannot help you about the book that you have. K&R's C is the only C book I have and use.

    Break that problem down into smaller parts. Use your flowcharting skills to solve the logic of the problem before you start typing. Try notebook paper before typing. I find it helps.

  6. #6
    Registered User
    Join Date
    Nov 2010
    Posts
    6
    Yea, I don't have that book unfortunately.

  7. #7
    Registered User
    Join Date
    Nov 2010
    Posts
    6
    I have the C by dissection 4th ed. and I am looking all over the book for hints or ways to do this problem and I can't find anything.

  8. #8
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Start by writing bits of your assignment into the code as comments.
    Code:
    /*
     * write a program that reads n integers into an array, then prints on a separate line the value of each distinct element 
     * along with the number of times it occurs. the values should be printed in descending order." 
     */
    int main ( ) {
      /* read n */
    
      /* read n integers into an array  */
    
      /* sort array into descending order */
    
      /* print values and number of occurrences */
      return 0;
    }
    The first two steps are easy enough - you did them in your previous assignment.

    Try step 3, and print the resulting array.

    DO NOT try step 4, until you can reliably get the output of
    5 3 3 -7 -7 -7

    from the input of
    -7 -7 3 3 5 -7
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  9. #9
    Registered User
    Join Date
    Nov 2010
    Posts
    42

    answer for you,

    here is the code for you, friend. but i seem abit too long.
    Code:
    #include<stdio.h>
    main()
    {
    	int arr[10],i,temp=0,count1=0,count2=0,count3=0,count4=0,count5=0,count6=0,count7=0,count8=0,count9=0,count10=0;
    for(i=0;i<10;i++)
    {
    	scanf("%d",&arr[i]);
    }
    
    for(i=0;i<10;i++)
    {	temp=arr[0];
    	if(arr[i]==temp)
    	count1++;
    }
    for(i=0;i<10;i++)
    {	temp=arr[1];
    	if(arr[i]==temp)
    	count2++;
    }
    
    for(i=0;i<10;i++)
    {	temp=arr[0];
    	if(arr[i]==temp)
    	count3++;
    }
    for(i=0;i<10;i++)
    {	temp=arr[0];
    	if(arr[i]==temp)
    	count4++;
    }
    for(i=0;i<10;i++)
    {	temp=arr[0];
    	if(arr[i]==temp)
    	count5++;
    }
    for(i=0;i<10;i++)
    {	temp=arr[0];
    	if(arr[i]==temp)
    	count6++;
    }
    for(i=0;i<10;i++)
    {	temp=arr[0];
    	if(arr[i]==temp)
    	count7++;
    }
    for(i=0;i<10;i++)
    {	temp=arr[0];
    	if(arr[i]==temp)
    	count8++;
    }
    for(i=0;i<10;i++)
    {	temp=arr[0];
    	if(arr[i]==temp)
    	count9++;
    }
    for(i=0;i<10;i++)
    {	temp=arr[0];
    	if(arr[i]==temp)
    	count10++;
    }
    
    
    printf("%d occur %d times\n",arr[0],count1);
    printf("%d occur %d times\n",arr[1],count2);
    printf("%d occur %d times\n",arr[2],count3);
    printf("%d occur %d times\n",arr[3],count4);
    printf("%d occur %d times\n",arr[4],count5);
    printf("%d occur %d times\n",arr[5],count6);
    printf("%d occur %d times\n",arr[6],count7);
    printf("%d occur %d times\n",arr[7],count8);
    printf("%d occur %d times\n",arr[8],count9);
    printf("%d occur %d times\n",arr[9],count10);
    }

  10. #10
    Registered User
    Join Date
    Aug 2010
    Posts
    231
    What do you do if n > 1000?

  11. #11
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    You might want to think about cutting that down...

    If you use an array for your count and work with two nested loops, you could probably do that whole function in about 10 lines of code.

    Something like this pseudocode...
    Code:
    array 10
    count 10
    
    for outerloop = 0 to 9
      for innerloop = 0 to 9
             test innerloop not equals outerloop
                compare  array[innerloop] to array[outerloop]
                    increase count[outerloop] by 1 if equal

  12. #12
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    Quote Originally Posted by BillyTKid View Post
    What do you do if n > 1000?
    It could very tedious if the value for 'n' keep going up. In that case the better solution would be to use hash table.

    ssharish
    Life is like riding a bicycle. To keep your balance you must keep moving - Einstein

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help understanding a problem
    By dnguyen1022 in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2009, 04:21 PM
  2. Memory problem with Borland C 3.1
    By AZ1699 in forum C Programming
    Replies: 16
    Last Post: 11-16-2007, 11:22 AM
  3. Someone having same problem with Code Block?
    By ofayto in forum C++ Programming
    Replies: 1
    Last Post: 07-12-2007, 08:38 AM
  4. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  5. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 06:54 PM