Thread: Avg. Number Proggie

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    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.

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

  3. #3
    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++)

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

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