### Recursive Function Average of the sum of Vectors element***

Hi there, first time posting and asking about c++ in this forum. I hope you can explain to me what I did wrong in my code.

My professor asked me to build a program with recursive functions and the programs must return the average of the sum of the all of the vectors elements which are numbers.

Here's my code

#include <iostream>
using namespace std;
int getSuma(int v[], int currentPos, int suma)

int main ()
{
int v[] = {10,4,5,2,8,1};
int x;

for(x = 0; x <6 ; x++){
cout << v[x]<< endl;
}
cout << "The average of the sum is"<< getSuma(v, v[0], 0) << endl;

system(" pause" );
}

int getSuma(int v[], int currentPos, int suma)
{
int x;
for( x = 0; x < 6; x++)
{
if (currentPos <6) {
suma = 0;
suma+= v[currentPos];

}
else
return suma;
}
}

What I did wrong? My code dont debug. Any help or solution will be enormously thanked. :)
`cout << "The average of the sum is"<< getSuma(v, v[0], 0) << endl;`

The second parameter should be 0, you're passing v[0] which is 10.

Also, you have an else with no if, and no base case - you should probably pass the size as well.
Last edited on
Your function isnt even recursive, it just counts the stuff in a loop. To better understand recursion maybe you should look at http://www.cplusplus.com/forum/articles/2231/
Hi @LB I changed what you said but that didnt fixed my code. Any guess?
Did you fix what @K0T4K0t4 said?
I think your program should contain only one recursive function that will calculate the sum of all elements. Its declaration can look as

int sum( int *, int );

where the first parameter defines an array and the second parameter defines the number of elements in the array.
So far I've done this...but still I dont know what Im doing wrong.
Can anyone please tell me?
#include <iostream>
using namespace std;
int getsuma(int v[], int currentPos);

void main ()
{
int v[] = {10,4,5,2,8,1};
int x;

for(x = 0; x <6 ; x++){
cout << v[x]<< endl;
// getsuma(0, v[0]);
}
cout << "The average of the sum is"<< getsuma(0, v[0])<< endl;
system(" pause" );
}

int getsuma(int v[], int currentPos){

if (currentPos <6) {
int suma;
suma= suma +v[currentPos];
suma = suma/6;
}
return (suma);

}
All what you need was already said to you. So please at least read what others wrote and try to understand that yourself.
Hi! I'm still working in the code I made this. So far I can get the average of the add (sum) but its suppose to give me back the number 5 but gives me a decimal number.

#include <iostream>
using namespace std;
double getsuma(int v[], int currentPos, double suma);

int main ()
{
int v[] = {10,4,5,2,8,1};
int x=0;
for(x = 0; x <6 ; x++)
{
cout<< v[x] <<endl;
}
cout << " Promedio: " << getsuma(v, 0, 0) <<endl;

system ("Pause");
}

double getsuma(int v[], int currentPos, double suma){

if (currentPos < 6) {
suma = suma + v[currentPos];
currentPos++;
suma = getsuma(v,currentPos, suma);

}

suma= suma /6;

return(suma);
}
Topic archived. No new replies allowed.