Storm
Storm

Reputation: 5

C++ Array average not working right

So im using C++ and I cant figure out what i'm doing wrong.
To find the average you just add the numbers in the array and dived buy the number of numbers in the array.
I fell like my problem lies in the array but I'm not sure because I'm getting this as the average result.

So i input this

2
3
4
5
6

Output

The average number is -1.71799e+008

The correct average of the numbers is 4 so im not to sure where my mistake is. Any help or hints would be very helpful. I have also set up my average line from what i have seen here on stack overflow but still no luck.

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

void main ()
{
    //Variables
    double average;
    double sum = 0;
    int numabove = 0;
    int i, array[5]; 
    int n = 5;
    //array
    cout << "Input 5 values.." << endl;
    for( i = 0; i < 5; ++i )
        cin >> array[5];
    for (int i = 0; i < 5; i++)
        //average
        average = ((float(array[i])/n));
    cout <<"The average number is " << average << "\n" << endl;
    //loop
    // I will do the loop after the average is working. Because
    // i need the average to work so i can do the loop which use's the loop below
    cout << "The numbers above average are: " << numabove << endl;
    system("PAUSE");
}

Upvotes: 0

Views: 240

Answers (4)

SleuthEye
SleuthEye

Reputation: 14579

In your for loop reading values, you should have:

for( i = 0; i < 5; ++i )
  cin >> array[i];

Otherwise, you're just reading all values out-of-bound at index 5 (cin >> array[5]).

Then, later algorithm should sum all values, not overwriting the average variable 5 times:

average = 0;
for (int i = 0; i < 5; i++)
  //average
  average += ((float(array[i])/n));

Upvotes: 2

4pie0
4pie0

Reputation: 29724

You read all numbers into

cin >> array[5];

which doesn't exist and produces undefined behavior. Correct this to:

   for( i = 0; i < 5; ++i )
    cin >> array[i];

and then compute average correctly:

double average = 0;
for ( int i = 0; i < 5; ++i) {
   sum += array[ i];
}
average = sum / n;

Maybe you can also take a look at std::vector which is C++ dynamic array and is quite flexible and easy to use. With this in hand you can write:

std::vector<int> v(5);
for( i = 0; i < 5; ++i )
   std::cin >> v[i];
double sum = std::accumulate( v.begin(), v.end(), 0.0);
double mean = sum / v.size();

Upvotes: 2

tiridactil
tiridactil

Reputation: 389

you always input the number in the same spot

the loop should be

for( i = 0; i < 5; ++i )
cin >> array[i];

also, the index 5 is out of range, valid indexes are {0, 1, 2, 3, 4}

finally you are not summing the whole array

the result you are getting is because the data of the array is not initialized

the quickest solution is probably

int sum = 0; //initialize it really important
for( i = 0; i < 5; ++i ) 
{ 
    int num; 
    cin >> num; 
    sum += num; 
}
double average = double(sum) / 5.0;

Upvotes: 1

mtadd
mtadd

Reputation: 2555

for (int i = 0; i < 5; i++) {
   sum += array[i];
}
average = sum / n;

Upvotes: 0

Related Questions