Thread: Average of user input in a loop problem

  1. #1
    Registered User
    Join Date
    Oct 2012
    Posts
    11

    Average of user input in a loop problem

    Anyone please help me know what I'm I doing wrong, I been trying to figure it out literally for the past 3 hours.


    Here's the problem:
    Write a C program to read positive integers, until the value -1 is read and print the average of the values.

    This is my code, the average is off for some reason:

    Code:
    #include <stdio.h>
    int main (void)
    {
     
           int num,sum=0;
           double avg;
     
           printf("Type a positive number");
           scanf("%d",&num);
          
          
           for(num;num>=-1;num--)
           {
           printf("%d\n",num);     
           sum=sum+num;
           }
           
           avg= sum/(num+1);   
           printf("\n\nThe average is:%d\n",avg);  
          
     
    getch();
    }

  2. #2
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    Integer arithmetic essentially truncates any fractional result. Try:
    Code:
    avg= (double)sum/(num+1);  // make sum a double to ensure division is done with floating-point math
    Now that I look at it, your loop control is completely screwed up too.
    Last edited by oogabooga; 10-02-2012 at 07:54 PM.
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  3. #3
    Registered User
    Join Date
    Oct 2012
    Posts
    11
    I tried it but still the average is not right...
    My loop control is screwed up? in where? ;o

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Your loop condition is wrong. Actually, the choice of a for loop is inappropriate here; use a while loop (or do while loop) instead.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,907
    "num" is -2 after exiting the 'for' loop - I'm assuming that this is not what you wanted

    Code:
    avg= sum/(num+1);
    = sum/(-2+1);
    = sum/-1;
    Fact - Beethoven wrote his first symphony in C

  6. #6
    Registered User
    Join Date
    Oct 2012
    Posts
    11
    How can I fix the value of num so it's what I typed into scanf?

    I just need to know that and the avg will work, anyone please help if you can.
    Last edited by tadm123; 10-03-2012 at 01:55 AM.

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    You shouldn't "fix" the value of num. Rather, you should be reading into num in the body of the loop, exiting the loop when -1 is read.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  8. #8
    Registered User
    Join Date
    Oct 2012
    Posts
    11
    I feel so stupid but isn't that's what I was doing? I made that for loop from n to -1 then it exited. Please give me a hint on the code in where should I correct it.

  9. #9
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    You want the user to input something again and again until he hits -1.So he is going to type something again and again....So you did well for using a loop.Inside the loop you should do all the procedure for the user..
    Every time,the problem says,you read the input and if it is -1,then you should exit the loop

  10. #10
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by tadm123
    I feel so stupid but isn't that's what I was doing?
    No, you read into num exactly once, even if -1 was not the first input.

    Quote Originally Posted by tadm123
    I made that for loop from n to -1 then it exited.
    Your requirements state:
    Write a C program to read positive integers, until the value -1 is read and print the average of the values.
    It does not state:
    Write a C program to read a positive integer. Loop from that positive integer until -1, decrementing the value on each iteration.

    Therefore, you must keep reading in the body of the loop. Failure to do so is definitely a mistake.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  11. #11
    Registered User
    Join Date
    Oct 2012
    Posts
    11
    I see, I misunderstood the question then. I thought it was the 2nd option ^


    So using arrays is necessary then to solve this problem ?

  12. #12
    Registered User
    Join Date
    May 2010
    Posts
    4,633
    No, an array is not necessary. Just use your two variables, num and sum similar to what you did in your first post. Just make the changes to your actual loop as suggested. Use a while loop, put your input into that loop. Be sure you exit the loop before you "add" your sentinel (-1) to your sum. Compute the average after you exit the loop.


    Jim

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Beginners problem with C - User input, average etc
    By glasvegas in forum C Programming
    Replies: 3
    Last Post: 03-23-2011, 03:25 PM
  2. Average of user inputs (in a loop)
    By SilentPirate007 in forum C Programming
    Replies: 13
    Last Post: 03-08-2010, 06:46 PM
  3. User input how many times to run average
    By kgatihi in forum C++ Programming
    Replies: 2
    Last Post: 10-23-2009, 02:05 PM
  4. User input while loop is running
    By two31d in forum C++ Programming
    Replies: 7
    Last Post: 11-30-2005, 05:28 PM
  5. user input loop
    By asudave2302 in forum C++ Programming
    Replies: 11
    Last Post: 12-21-2004, 09:45 PM