Greetings programming community,

How can I get this recursive function that finds the average of an array from any range using specific parameters to work? The parameters are (int a[], int x, int y). The x parameter means begin at and the y means end at.

int k; //no global variables

int average(int A[], int s, int e) {

    int x = e+1;

    if(s == e) return (k += A[s])/x ;
    k += A[s];
    average(A, s+1, e);
}




int main() {

    int A[5] = {5,7,8,10,2};
    cout << average(A,0,4) << endl;

    return 0;
}

What is the purpose of the 3rd parameter 'e'? Also, in your average() function, you don't return the results of the recursive call. IE, line 9 should be return average(A, s+1, e);

When I edited the code I didn't edit the parameters. s means start, e means end.

This function may require more than one base case. I know if start and end are both equal to one another then the average is obviously that number at that index..
if(s == e) return A[s] or A[e]. I'm not sure how to calculate the total or average without using a global variable. Every time I recursively call the function, the value will be lost.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.