Thread: Avg. Number Proggie

  1. #1
    Registered User
    Join Date
    Apr 2004
    Posts
    173

    Avg. Number Proggie

    I'm kinda stuck on what to do, basically the user can input as many inputs as he/she wants to (within reasonable amount) and the program is supposed to calculate the avg, simple enuff. But I can't get it working.. heres wat I tried so far..

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main()
    {
    	float num[30];
    	int n = 0;
    	float total = 0, avg = 0;
    
    	printf("Input numbers separated by a space: ");
    
    	// Input numbers 
    	for (n = 0; (num[n] != '\n'); n++)
    	{
    		scanf("%d ",&num[n]);
    	}
    	
    	// Total numbers
    	for (n = 0; (num[n] != '\n'); n++)
    	{
    		total += num[n];
    	}
    
    	avg = total / ((float)n);
    
    	printf("The average number was: %f",total);
    
    	return 0;
    }
    The problem lies in me receiving input from the scanf() function into an array.. any suggestions?

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >basically the user can input as many inputs as he/she wants to (within reasonable amount)
    ie. 30.

    >for (n = 0; (num[n] != '\n'); n++)
    Just out of curiosity, how were you planning on representing '\n' as a floating-point value in an array initialized by a function that ignores whitespace? Perhaps using 0 as a stopping condition would be better suited because adding 0 to the sum does nothing and thus has no purpose in the input:
    Code:
    for (n = 0; n < 30; n++)
    {
      scanf("%f",&num[n]);
      if ( num[n] == 0 )
        break;
    }
    Your call to scanf is also wrong in two ways. First, you tell it to read an integer but give it a pointer to float. This will rarely work as you want. Second, the trailing space in the format string causes scanf to search for and discard any amount of whitespace after the call. Your program will probably hang for no obvious reason because of this unless you add extra junk input for scanf to stop on.
    My best code is written with the delete key.

  3. #3
    Registered User
    Join Date
    Apr 2004
    Posts
    173
    Oo okies, I fixed up some of the code. I kinda mis-typed the scanf() to read an integer instead of a float..

    Code:
    int main()
    {
    	float num[30];
    	int n = 0;
    	int m = 0;
    	float total = 0, avg = 0;
    
    	printf("Input numbers separated by a space: ");
    
    	// Input numbers 
    	for (n = 0; n < 30; n++)
    	{
    		scanf("%f",&num[n]);
    		if (num[n] == 0)
    			break;
    	}
    	m = n;
    	
    	// Total numbers
    	for (n = 0; n < m; n++)
    	{
    		total += num[n];
    	}
    
    	avg = total / ((float)(n));
    
    	printf("The average number was: %f\n",&total);
    
    	return 0;
    }
    Ok, it terminates well when I input 0, the problem is that I can't see why the output is alwayz equiv to 0.. Since I incremented n when I tally'ed the numbers, shouldn't it work? Unless my eyes are missing somethiung again

  4. #4
    Registered User
    Join Date
    Apr 2004
    Posts
    173
    Dun owrri I fink I got it.. need more sleep I ain't thinking straight

  5. #5
    Registered User
    Join Date
    Mar 2004
    Posts
    536
    Quote Originally Posted by 0rion
    Dun owrri I fink I got it.. need more sleep I ain't thinking straight
    What are you printing?

    Code:
    printf("The average number was: %f\n",&total);
    Not right, for a couple of reasons.

    Dave

  6. #6
    Registered User caroundw5h's Avatar
    Join Date
    Oct 2003
    Posts
    751
    I hope this code is correct. I'm currently at work and on the phone with clients but at least the logic should be correct. Someone correct me if i'm wrong.
    Code:
    /* avg program */
    
    #define MAX 30
    #include <stdio.h>
    #include <stdlib.h>
    
    
    
    int main(void){
    
       int count;
       float num, avg;
    
       for(count=0; count<=MAX; count++){
           printf("Enter in num %d\n", count);
           while( (scanf("%f", &num)) ==1 && num>0){
              num+=num;
           }Printf("Number must be 1 or greater\n");
             continue;
    
        }
       float(avg) = num/MAX;
      printf("The average is %f", avg);
      system("pause");
      return 0;
    }
    I'll check it when i get home. should work though.


  7. #7
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,164
    Quote Originally Posted by caroundw5h
    I hope this code is correct. I'm currently at work and on the phone with clients but at least the logic should be correct. Someone correct me if i'm wrong.
    Code:
    /* avg program */
    
    #define MAX 30
    #include <stdio.h>
    #include <stdlib.h>
    
    
    
    int main(void){
    
       int count;
       float num, avg;
    
       for(count=0; count<=MAX; count++){
           printf("Enter in num %d\n", count);
           while( (scanf("%f", &num)) ==1 && num>0){
              num+=num;
           }Printf("Number must be 1 or greater\n");
             continue;
    
        }
       float(avg) = num/MAX;
      printf("The average is %f", avg);
      system("pause");
      return 0;
    }
    I'll check it when i get home. should work though.

    What variable are you 'scanf'ing into?
    What variable is holding your total?
    Is there a conflict?
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

  8. #8
    Registered User caroundw5h's Avatar
    Join Date
    Oct 2003
    Posts
    751
    Quote Originally Posted by WaltP
    What variable are you 'scanf'ing into?
    What variable is holding your total?
    Is there a conflict?
    expalain that a bit more to me on where the problem lay walt. I coded it on the fly so i was expecting it to work. Here is a corrected version,Define MAX to be whatever u want
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #define MAX 4
    
    
    
    int main(void){
    
       int count;
       float num, avg;
    
       for(count=1; count<=MAX; count++){
           printf("Enter in num %d\n", count);
           scanf("%f", &num);
           num+=num;
           avg = num/MAX;
       }
      printf("The average is %.2f\n", avg);
      system("pause");
      return 0;
    }
    Last edited by caroundw5h; 04-01-2004 at 11:48 PM.

  9. #9
    Registered User
    Join Date
    Dec 2003
    Posts
    53
    Essentially, when you loop and run the scanf to num, you clear out anything that was originally stored in the num variable... that includes the sum of previous data(not that it actually sums it up...)

    so what will happen is that let's say the last value entered by the user is 300.0, it hits the num +=num line. expanding and substituting the values in get me num = 300.0 + 300.0;
    meaning num == 600.0...
    hit the divide part, assuming MAX == 30, avg = 600.0/30 == 20.0... Hardly the average of all values entered.

    There's also the fact that it was supposed to be a user defined amount of data entered, not a fixed amount...

  10. #10
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,164
    Quote Originally Posted by caroundw5h
    expalain that a bit more to me on where the problem lay walt. I coded it on the fly so i was expecting it to work.
    You are expecting "on-the-fly" code to work? If you spend time on it do you expect it to not work? The problem with "on-the-fly" code is usually it does not work

    Quote Originally Posted by caroundw5h
    Code:
    for(count=1; count<=MAX; count++){
        printf("Enter in num %d\n", count);
        scanf("%f", &num);
        num+=num;
        avg = num/MAX;
    }
    Walk thru this code with pencil & paper, keeping track of all the variables as they change. You will see exactly where the problem is.
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

  11. #11
    Registered User caroundw5h's Avatar
    Join Date
    Oct 2003
    Posts
    751
    Quote Originally Posted by WaltP
    You are expecting "on-the-fly" code to work? If you spend time on it do you expect it to not work? The problem with "on-the-fly" code is usually it does not work
    More likely not to work. On the fly, mean i'm doing things mecanically, not really thinking about it.


    Walk thru this code with pencil & paper, keeping track of all the variables as they change. You will see exactly where the problem is.
    Yeah, I give up already. The code seems to work fine for me when i run?
    Tell me what the problem is.

    Also that user defined amount is easily taken care of.
    I think the person understand that.
    Code:
    for(count = 1;count<=USER_NUM;count++)

  12. #12
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    They mean it doesn't work:
    Code:
    for( x = 1; x <=4; x++ )
    {
        /* first time through loop */
        scanf into number /* say they input 5 */
        number = number + number /* number now is 10 */
    
        /* second time through loop */
        scanf into number /* say the input is 6 */
        number = number + number /* number is now 12 */
    
        /* third time through loop */
        scanf into number /* say the input is 7 */
        number = number + number /* number is now 14 */
    }
    See the problem yet? Had you actually walked through the code like they suggested, you'd see the problem.

    Quzah.
    Hope is the first step on the road to disappointment.

  13. #13
    Registered User caroundw5h's Avatar
    Join Date
    Oct 2003
    Posts
    751
    Quote Originally Posted by tzuchan
    Essentially, when you loop and run the scanf to num, you clear out anything that was originally stored in the num variable... that includes the sum of previous data(not that it actually sums it up...)

    so what will happen is that let's say the last value entered by the user is 300.0, it hits the num +=num line. expanding and substituting the values in get me num = 300.0 + 300.0;
    meaning num == 600.0...
    hit the divide part, assuming MAX == 30, avg = 600.0/30 == 20.0... Hardly the average of all values entered.

    There's also the fact that it was supposed to be a user defined amount of data entered, not a fixed amount...
    Thank you for breaking it down for me tzuchan - without the god complex like some members on this board. [cough]quzah[/cough] I'm still getting used to the intricacies of C so I appreaciate your knowledge you and walt share without the rancor.

    Maybe quzah you should concentrate on augmenting the amount of ppl you can help out and not your posts count. You obvioulsy need some time out from the message boards and since you are so impatient with ppl who don't spend ALL their time around the keyboard && your intelligence is so high. write some code to augment your posts. I have full faith in you
    Code:
    action = newbie ? sarcasm() : augment_posts();
    Last edited by caroundw5h; 04-02-2004 at 06:43 PM.

  14. #14
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by caroundw5h
    Thank you for breaking it down for me tzuchan - without the god complex like some members on this board. [cough]quzah[/cough]
    Ok, I'll bite. Let's look at the order of the posts:

    Tzuchan posts. You post the following reply:
    Quote Originally Posted by caroundw5h
    Yeah, I give up already. The code seems to work fine for me when i run? Tell me what the problem is.
    I reply:
    Quote Originally Posted by Quzah
    Code:
    for( x = 1; x <=4; x++ )
    {
        /* first time through loop */
        scanf into number /* say they input 5 */
        number = number + number /* number now is 10 */
    
        /* second time through loop */
        scanf into number /* say the input is 6 */
        number = number + number /* number is now 12 */
    
        /* third time through loop */
        scanf into number /* say the input is 7 */
        number = number + number /* number is now 14 */
    }
    See the problem yet? Had you actually walked through the code like they suggested, you'd see the problem.
    You reply:
    Quote Originally Posted by caroundw5h
    Maybe quzah you should concentrate on augmenting the amount of ppl you can help out and not your posts count. You obvioulsy need some time out from the message boards and since you are so impatient with ppl who don't spend ALL their time around the keyboard && your intelligence is so high. write some code to augment your posts. I have full faith in you
    Code:
    action = newbie ? sarcasm() : augment_posts();
    Perhaps if you had actually done as was suggested, instead of saying "I give up", you'd have figured it out. Since you didn't, I did it for you, which is what you really wanted in the first place. Next time put out some effort. It will save everyone some time. As for post count, I could care less. I've been here for every incarnation of this board, and I'll be here when you're long gone.

    I help people who make an effort. Give it a try some time.

    Quzah.
    Hope is the first step on the road to disappointment.

  15. #15
    Registered User caroundw5h's Avatar
    Join Date
    Oct 2003
    Posts
    751
    [yawn] [/yawn] I think we've just about exhauseted the possibilities of this coversation. Please don't post anymore on this thread.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help with this compiler error
    By Evangeline in forum C Programming
    Replies: 7
    Last Post: 04-05-2008, 09:27 AM
  2. Calculating next prime number
    By anilemon in forum C Programming
    Replies: 8
    Last Post: 04-17-2006, 10:38 AM
  3. Prime number program problem
    By Guti14 in forum C Programming
    Replies: 11
    Last Post: 08-06-2004, 04:25 AM
  4. parsing a number
    By juancardenas in forum C Programming
    Replies: 1
    Last Post: 02-19-2003, 01:10 PM
  5. Random Number problem in number guessing game...
    By -leech- in forum Windows Programming
    Replies: 8
    Last Post: 01-15-2002, 05:00 PM