the purpose of this task is to find the average of an array but not within the main, I have to call a function to do the sum and show the average.
I though my code was sound but it just returns " the average is 011014F1"
I have tried a few different ways of doing the function but I've gone wrong somewhere, maybe everywhere!
Just a heads up, im just starting out with programing.
Heres my code:
#include <iostream>
#include <vector>
using namespace std;
void printArray(int theArray[], int sizeOfarray);
float average(float numbers[], float size, float arrayAverage);
int main()
{
int array[10] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
printArray(array, 10);
cout << "The average is: " << average << endl;
return 0;
}
void printArray(int theArray[], int sizeOfarray)
{
for (int x = 0; x < sizeOfarray; x++)
{
cout << theArray[x] << endl;
}
}
float average(float numbers[], float size, float arrayAverage)
{
double sum = 0.0;
for (int x = 0; x < size; x++)
{
sum += numbers[x];
arrayAverage = sum / size;
}
return (arrayAverage);
}
I had the float average function initially set as a float with int for 'numbers', 'size' and 'arrayAverage' but thought i would change them all to float so they dont clash. like converting an int to a float etc..
As i said im new to this so my logic is not really there but i think im n the right tracks.
Any idea why its returning 011014F1 and numbers like that instead of just the average of 1-10?
Any tips much appreciated!
averagefunction. You have to invoke it with parameters in order for it to work (how else would it know what array is it meant to use?)coutyou wantaverage(array, 10)else, as you discovered, it prints the function pointer.std::valarrayis about the worst thing of the entire C++ standard library, right next tostd::vector<bool>. I've never seen it recommended (or used) anywhere, and the fact that it's in the standard library is more of a historical accident. See e.g. Josuttis' "The C++ Standard Library" book for details.std::vectorbefore arrays. Arrays are more work when passing to and from functions.