I have to find the largest item in an arry as a recursive function. Can't seem to get it right. Any ideas.

``````#include <iostream>
using namespace std;

int maxArray( int anArray[], int n );

int main()
{
int anArray[4] = { 1, 6, 8, 3 };
cout << "the largest number of the array is " << anArray << endl;

return 0;
}
int maxArray( int anArray[], int n  )
{
if( n == 0 )
{
cout << "empty array" << endl;
}
if( n == 1 )
{
return anArray[0];
}
else
{
anArray[n] = maxArray( anArray, n-1 );
}
if( anArray[n] > anArray[n] )
{
return anArray[n];
}
else
{
return anArray[n];
}
}``````
3
Contributors
3
Replies
4
Views
9 Years
Discussion Span
Last Post by Duoas

I just briefly glanced your code and noticed an error...

if( anArray[n] > anArray[n] )

Why are you comparing the same element to itself? And also why are you returning the same element in either case?

if( anArray[n] > anArray[n+1] )
return anArray[n]
else
return anArray[n+1]

However this is only assuming that you have some kind of rule for n (what is n supposed to be? the size of the argument? The last indice? It's not clear! )

The below logic is as such--

*declares a static variable to hold the current max argument.
*compares the argument with an indice in the array and replaces the static variable with a value only if it is greater
*process continues through the array recursively

``````#include <cstdlib>
#include <iostream>

using namespace std;

int maxArray( int anArray[], int n )
{
static int temp = anArray[0];

if(n == 1)
{
int beta = temp;
temp = 0;
return beta;
}
else if(n == 0)
{
cout << "Improper arg! size cannot be zero!" << endl;
return 0;
}
else
{
if(anArray[n - 1] > temp)
temp = anArray[n - 1];

return maxArray(anArray, n - 1);
}
}

int main(int argc, char *argv[])
{
int myArray[] = {1, 9, 2, 5, 10, 99, 54, 7, 2};

cout <<"Max value in myArray is: "<< maxArray(myArray, 9) << endl;

cin.get( );
return EXIT_SUCCESS;
}``````

Recursion gets people because it is something new --which translates to forgetting all the stuff you already know.

In order to determine the maximum number in a list, you need three things:
1. the list
2. the current maximum value
3. something to compare each element of the list with the current maximum

If I were to do it iteratively, I'd write something like

``````int max_in_list( int a[], int size )
{
int max = a[ 0 ];
for (int n = 1; n < size; n++)
if (a[ n ] > max) max = a[ n ];
return max;
}``````

(This presumes that a[] contains at least one element.)

To make it recursive, you have to do the same kind of thing. Or in other words, you have to remember the same information across every function call that you remember each time you go through the loop:
1. the current index into the array
2. the number of elements in the array
3. the current maximum value
And each time the function returns it should give the new information that we get out of the loop:
1. the new maximum value

You are very close. Just watch your fenceposts (for an n-element array, elements are numbered 0..n-1). And don't forget to calculate the new current maximum value (current max vs anArray[n-1]).

Hope this helps.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.