So im trying to display the largest value entered and the smallest value entered. i can get my code to compile and run the largest number if its greater than o and the smallest number if its less than 0. but i want to be able to cover all numbers entered. Positive and Negative. Does anyone have any sugestions.

#include <iostream>
#include <cstdlib>
using namespace std;

int main()
{
    
    //Declarations
    int intArray[10];
    double dbltotal = 0.0;
    double dblaverage = 0.0;
    double dblmax = 0.0;
    double dblmin = 0.0;
    
    //Input and Process
    for (int x=0; x<10; x++){
        cout << "Enter a number: ";
        cin >> intArray[x];
        dbltotal = dbltotal + intArray[x];
    
        if (intArray[x] > dblmax)
           dblmax = intArray[x];
           
        if (intArray[x] < dblmin)
           dblmin = intArray[x];
    }
    
    dblaverage = dbltotal / 10.0;
        
    //Output
    cout << "The sum of the numbers entered is " << dbltotal << endl;
    cout << "The average of the numbers entered is " << dblaverage <<endl;
    cout << "The max number entered was: " << dblmax << endl;
    cout << "The minimum number entered was: " << dblmin << endl;
    system ("pause");
    return 0;
}

i changed my values of dblmax and dblmin but if the user enter somthing smaller or larger then i wont be covered

#include <iostream>
#include <cstdlib>
using namespace std;

int main()
{
    
    //Declarations
    int intArray[10];
    double dbltotal = 0.0;
    double dblaverage = 0.0;
    double dblmax = -10000000000000000.0;
    double dblmin = 10000000000000000.0;
    
    //Input and Process
    for (int x=0; x<10; x++){
        cout << "Enter a number: ";
        cin >> intArray[x];
        dbltotal = dbltotal + intArray[x];
    
        if (intArray[x] > dblmax)
           dblmax = intArray[x];
           
        if (intArray[x] < dblmin)
           dblmin = intArray[x];
    }
    
    dblaverage = dbltotal / 10.0;
        
    //Output
    cout << "The sum of the numbers entered is " << dbltotal << endl;
    cout << "The average of the numbers entered is " << dblaverage <<endl;
    cout << "The max number entered was: " << dblmax << endl;
    cout << "The minimum number entered was: " << dblmin << endl;
    system ("pause");
    return 0;
}

Why don't you make both your max and min equal to the first value in the array? Originally you initialized them to zero. Well, how about when all numbers are less than zero?? Doesn't work. If you initialize them to the first value in the array then your program will work because then your starting value is part of the array and not just some made up number which may in fact be larger or smaller than all the numbers in your array. Sorry, I'm really tired so maybe my explanation sounds a little confusing but I think you can understand what I mean.

You could make the following modification, that is, removing the conditional statement when x == 0.

#include <iostream>
#include <cstdlib>
using namespace std;

int main()
{

    //Declarations
    int intArray[10];
    double dbltotal = 0.0;
    double dblaverage = 0.0;
    double dblmax = 0.0;
    double dblmin = 0.0;

   cout << "Enter a number: ";
   cin >> intArray[0];
   dbltotal = dbltotal + intArray[0];
   dblmax = intArray[0];           
   dblmin = intArray[0];

   for (int x=1; x<10; x++){
        cout << "Enter a number: ";
        cin >> intArray[x];
        dbltotal = dbltotal + intArray[x];

        if (intArray[x] > dblmax)
           dblmax = intArray[x];

        if (intArray[x] < dblmin)
           dblmin = intArray[x];
    }
    dblaverage = dbltotal / 10.0;

    //Output
    cout << "The sum of the numbers entered is " << dbltotal << endl;
    cout << "The average of the numbers entered is " << dblaverage <<endl;
    cout << "The max number entered was: " << dblmax << endl;
    cout << "The minimum number entered was: " << dblmin << endl;
    system ("pause");
    return 0;
}

Edited 3 Years Ago by Reverend Jim: Fixed formatting

This article has been dead for over six months. Start a new discussion instead.