Hi there. I need some help with a program i am making for a class. It is a statistics program for vectors/arrays. One of the functions i need to make is one that will calculate the average of the array. I have made a program before on finding the average of 3 numbers, but i just dont know where to et started for one that could have any number of things to add. My real problem is this: Im not sure how to add all parts of the array up at once. I tried a nested for loop, a regular for loop, etc.
I forgot - vectors DOUBLE in size when they need more room.
Try this:
1 2 3 4 5 6 7 8 9 10
vector<int> numbers;
int sum = 0;
int numberOfElements = 0;for (int i = 0; i < numbers.size(); i++)
{
sum += numbers[i];
numberOfElements++;
}
double average = sum / numberOfElements;
Also, try using this in your code after you declare your "name" vector: name.reserve(size)
That will reserve just enough elements so that you don't have your vector getting too big.