C++: The average output of my program doesn’t show properly

  c++

I’ve already had searched around the internet for some answers, but I can’t seem to find the answer. I guess this program would be easier if I use for loop but my professor ordered us to use the while loop statement.

#include <iostream>
using namespace std;

int main()
{
    float value[10];
    float average;
    float min;
    float max;
    int index1 = 0, index2 = 0, index3 = 0, index4 = 0, sum;

    while (index1 < 10)
    {
        cout << "Enter a value : ";
        cin >> value[index1];
        index1++;
    }
    while (index2 < 10)
    {
        sum += value[index2];
        index2++;
    }

    max = value[index3];
    while (index3 < 10)
    {
        if (max < value[index3])
        {
            max = value[index3];
        }
        index3++;
    }
    min = value[index4];
    while (index4 < 10)
    {
        if (min > value[index4])
        {
            min = value[index4];
        }
        index4++;
    }
    average = sum / 10;
    cout << "The average is        : " << average << "n";
    cout << "The largest value is  : " << max << "n";
    cout << "The smallest value is : " << min << "n";
}

Here’s how it looks when I run it.

Enter a value : 98
Enter a value : 45
Enter a value : 32
Enter a value : 21
Enter a value : 67
Enter a value : 54
Enter a value : 74
Enter a value : 25
Enter a value : 98
Enter a value : 33
The average is        : -2.00668e+008
The largest value is  : 98
The smallest value is : 21

Source: Windows Questions C++

LEAVE A COMMENT