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

Recommended Answers

All 2 Replies

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.

i rack my head for 2 hours over this xD

well it works now. thnx alot

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.