0
Hi, I’m solving an ex. basic of c++ where it asks to do a function that takes as parameter a vector of n° real and that reads from the keyboard n n° real, store them in the vector and calculate its average. the function should return to average.
Anyway, my problem is, how to make the user allocate the vector size, WITHOUT USING dynamic memory.
#include <iostream>
using namespace std;
float VetorCalculaMedia(int n, float vet[])
{
float soma = 0;
for( int i=0; i<n; i++)
{
cout << " Type the array elements: " << endl;
cin >> vet[i];
}
for (int i=0; i<n; i++)
{
soma += vet[i];
}
return soma/n;
}
int main ()
{
int n;
cout << "type the array size: " << endl;
cin >> n;
float vet[n];
**cout << "The media is: " << VetorCalculaMedia(n, vet[n]) << endl;**
return 0;
}
on the bold line the error is 'Cannot Convert float to float*'
When you do
vet[n]
is referring to an element of the vector, in case the (n+1)-th element since the index leaves zero. Its function expects to receive a vector and not an element of the vector. Use:cout << "The media is: " << VetorCalculaMedia(n, vet) << endl;
.– anonimo
Thank you very much!!
– Esteves