0

so im calculating the avergae of 5 numbers ive put in the array but when i compile i get a very large number
heres my code

#include <iostream>
using namespace std;

int calcAverage()
{
int sum=0;
int average;
int TESTVALS[5] = {2, 18, 1, 27, 16};
for(int i = 1; i < 5; i++)
sum+= TESTVALS[i];
average= sum/5;
cout<<"average";
}

int main()
{
  cout << "The average value" << calcAverage() << endl;
  system("pause");
  return 0;
}

and yes i need the calcAverage as a prototype. i can do it w.o the protoype but i am going to need it later on

2
Contributors
2
Replies
3
Views
6 Years
Discussion Span
Last Post by BryantFury
0

You forgot to add return average to your function--

int calcAverage()
{
int sum=0;
int average;
int TESTVALS[5] = {2, 18, 1, 27, 16};
for(int i = 1; i < 5; i++)
sum+= TESTVALS[i];
average= sum/5;
return average;
}

-- so its possible that due to the lack of the return statement the function was assigned some space of an int that wasn't initialized.

0

i rack my head for 2 hours over this xD

well it works now. thnx alot

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.