简体   繁体   中英

Find the place of max in array recursively c++

I have used this function to find the maximum element It works for some cases but it got wrong place in other cases and this is the case i call function with.

int maxElement=maxi(names,noOfTeams,0,1,sum,0)

this is the function :

  int maxi(string names[],int sizee,int prev,int next,int scores[],int maxx)
{
    if (sizee ==1)return 0;
    if (scores[maxx]<scores[next]) maxx=next;
    if ((next+1)==sizee)return maxx;
    else return maxi(names,sizee,prev+1,next+1,scores,maxx);

}

Below could be a better approach:

Go through each element of the array, till there are elements left in the array which will be the base case. Then in each function call check whether element at current index is greater than the element found at maxIndex if so update the max index and check for next array element by calling function again and so on.

Please find code below for both finding max element and max index recursively:

#include <iostream>

using namespace std;

int findMax(int arr[], int size,int index, int max);
int findMaxIndex(int arr[], int size,int index, int maxIndex);

int main()
{
    int arr[] = {5,2,8,1,4};
    int len = sizeof(arr) / sizeof(int);

    cout << "Max is: " << findMax(arr, len, 0, arr[0]) << endl;
    cout << "Max Index is: " << findMaxIndex(arr, len, 0, 0) << endl;

    return 0;
}


int findMax(int arr[], int size, int index, int max)
{
    if (index == size)
        return max;

    if (arr[index] > max)
        max = arr[index];

    return findMax(arr, size, index + 1, max);
}

int findMaxIndex(int arr[], int size, int index, int maxIndex)
{
    if (index == size)
        return maxIndex;

    if (arr[index] > arr[maxIndex])
        maxIndex = index;

    return findMaxIndex(arr, size, index + 1, maxIndex);
}

You have couple of errors in your function.

  1. The line

     else if (scores[prev]<scores[next])maxx=next; 

    needs to be

     else if (scores[maxx]<scores[next])maxx=next; // ^^^^ 
  2. You are missing a return in the recursive call. Instead of

     else maxi(names,sizee,prev+1,next+1,scores,maxx); 

    it needs to be

     else return maxi(names,sizee,prev+1,next+1,scores,maxx); 

Also, the function can be simplified.

  1. The argument name is not used at all. It can be removed.
  2. The argument prev can be removed too.
  3. Couple of the checks you have can be combined into one.
  4. You don't need a chain of if-else-else statements.

Here's a simplified version.

int maxi(int sizee, int next, int scores[], int maxx)
{
   if ( sizee == next )
      return maxx;

   if (scores[maxx] < scores[next])
      maxx=next;

   return maxi(sizee, next+1, scores, maxx);
}

More importantly, it will be better to have an overload of the function:

int maxi(int sizee, int scores[]);

That should be the user facing function. The implementation of the user facing function can use the recursive function as an implementation detail.

int maxi(int sizee, int scores[])
{
   return maxi(sizee, 0, scores, 0);
}

See it working at http://ideone.com/chvtPA .

You need to compare scores[maxx] with scores[next] , aslo you dont need prev

Change your function to

int maxi(string names[],int sizee,int next,int scores[],int maxx)
{
    if (sizee ==1)
        return 0;
    if (scores[maxx]<scores[next])
        maxx=next;
    if ((next+1)==sizee)
        return maxx; 
    return maxi(names,sizee,next+1,scores,maxx);
    //You need to return, else the function will be called but that
    // value wont be returned.
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM