Hi everyone, I'm stuck with some problems here with my code.
My quest is to create a function that takes an int vector as in-parameter and return the average of the numbers within the vector. Also the parameter
n should hold the number of elements within the vector.
I'm getting errors like "Expected primary expression before "int" and "Too few arguments to function "float Medel(int v[], int n).
#include <iostream>
#include <iomanip>
using namespace std;
float Medelv;
float Medel(int v[], int n)
{
Medelv = v[] / sizeof (v);
n = sizeof (v);
cout<<n<<endl;
return Medelv;
}
int main()
{
int vektor[10] = {1, 3, 5, 6, 7, 8, 22, 34, 55, 78};
Medel(vektor);
cout<<Medelv<<endl;
}
Would really apprectiate some help guys, this is my first attempt ever.
Regards
#include <iostream>
usingnamespace std;
float Medel(int v[], int n)
{
// missing code
// needs to be added here
}
int main()
{
int vektor[10] = {1, 3, 5, 6, 7, 8, 22, 34, 55, 78};
float average = Medel(vektor, 10);
cout << average << endl;
}
Inside the function, declare a float variable to store the total.
Set the initial value to zero.
float total = 0;
Loop through the array v, from 0 to n-1
for (int i=0; i<n; ++i)
add each element to the total.
After the loop has ended, calculate the average, it is total / n.
return that value from the function at the end.