I need to use recursion to find the largest int in an array. What I have so far is
#include <iostream>
using std::cout;
using std::endl;
int maxArray(int anArray[], int size);
/**
*The main method
*
*@param myArray[] The array we are searching
*
*@param sizeOfArray The size of the array
*
*@param largestNumber The largest number in the array
*/
int main(){
int myArray[] = { 1, 6, 8, 3 };
int sizeOfArray = sizeof(myArray)/sizeof(*myArray);
int largestNumber = maxArray(myArray, sizeOfArray);
cout<< "the largest number of the array is " << largestNumber << endl;
return 0;
}//end main
/**
*This method finds the largest int in an array
*
*@param anArry The array of integers
*
*@param size The size of the array
*
*@return The largest number in the array
*/
int maxArray(int anArray[], int size){
if(size == 0){
cout << "empty array" << endl;
}
if(size == 1 ){
return anArray[0];
}else{
return maxArray(anArray, size-1);
}
}//end maxArray
So I can get down to finding the first item in the array, but I cant figure out how I would go about getting to the 2nd, 3rd, 4th, etc items in the array using a recursive function and then once I get down to the one int how would I go about comparing that int to another int in the array. Im confused, any suggestions on how I can get over my hump of at least getting to the next element in the array. Id appriciate any suggestions/help/examples from other programs or any sites that would guide me in the right direction. And no I dont want you to do this for me, I just want a hint or suggestion so I can get unstuck. Thanks.