Thread: read n numbers and find their total, min, max, average

  1. #1
    Registered User
    Join Date
    Mar 2011
    Posts
    254

    read n numbers and find their total, min, max, average

    Hi

    I was trying to write a code to read n numbers and find total, min, max, average for those n numbers. There is clearly something wrong with the code which I have failed to understand. Please help me. I'm new to these loops.

    Best regards
    Jackson


    Code:
    #include <iostream>
    #include <cstdlib>
    
    using namespace std;
    
    int main()
    
    {
        int i, n;
        float number, total=0, min=500, max=0, average;
    
        cout << "How many numbers are there?: ";
        cin >> n;
    
        for (i=0; i<n; i+=1)
    
            {
    
            cout << "Enter the number: ";
            cin >> n;
    
            total = total + n;
    
            if (n < min)
            {
                min = n;
            }
    
            if (n > max)
            {
                max = n;
            }
    
            }
    
        cout << "Total is: " << total << endl;
        cout << "Minimum is: " << min << endl;
        cout << "Maximum is: " << max << endl;
        cout << "Average is: " << total/n << endl;
    
        system("pause");
    
    }
    Output:
    Code:
    How many numbers are there?: 9
    Enter the number: 1
    Total is: 1
    Minimum is: 1
    Maximum is: 1
    Average is: 1
    Press any key to continue . . .
    I'm an outright beginner. Using Win XP Pro and Code::Blocks. Be nice to me, please.

  2. #2
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    You're using the same variable 'n' for two different things. You'll want to use a separate variable for the value the user inputs.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  3. #3
    Registered User
    Join Date
    Mar 2011
    Posts
    254
    Thanks a lot, iMalc. I see the problem now.
    I'm an outright beginner. Using Win XP Pro and Code::Blocks. Be nice to me, please.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. find & print the average of the odd numbers
    By zozo995 in forum C++ Programming
    Replies: 1
    Last Post: 10-27-2010, 10:31 PM
  2. function to find average
    By nynicue in forum C Programming
    Replies: 4
    Last Post: 03-24-2009, 05:30 PM
  3. average of total using array
    By tmoney$ in forum C Programming
    Replies: 2
    Last Post: 05-13-2003, 05:27 PM
  4. Replies: 4
    Last Post: 03-03-2003, 03:52 PM
  5. How can I find the average in the ARRAYS?
    By Nate2430 in forum C++ Programming
    Replies: 2
    Last Post: 11-19-2001, 11:21 AM