Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9155591
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T12:37:21+00:00 2026-06-17T12:37:21+00:00

I have this function: int max(int arr[], int size) { size–; if(size > 0)

  • 0

I have this function:

int max(int arr[], int size)
{
    size--;
    if(size > 0)
    {
        int max = max(arr, size);
        if(arr[max] > arr[size]) return max;
    }
    return size;
}

And of course it works. My question is – how does this work? Could anyone explain me this step by step? It’s Saturday so maybe somebody has a bit of time 😀 I especially mean these two lines in if block.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T12:37:22+00:00Added an answer on June 17, 2026 at 12:37 pm

    You code is as follows:

    1 int max(int arr[], int size){
    2     size--;
    3     if(size > 0){
    4         int max = max(arr, size);
    5         if(arr[max] > arr[size]) return max;
    6     }
    7     return size;
    8 }
    

    You call it by passing it array and the size (or length) of that array.

    The first important point the code hits is on Line 4, when it calls itself recursively. But notice that on Line 2, the size was decreased by one. Therefore, you can think of size as being an index referring to the element of the array being considered by the current call to the function.

    This means that eventually, the size of the array will be down to zero and we will be looking at the first element of the array. At this point Lines 3 through 6 are skipped, and 0 is returned.

    When one of the recursive calls returns, we’re left back at Line 4.

    For the first return, this means that int max = 0;.

    Now we are comparing the zeroth element with the first element. We return the index of whichever is greater. The next comparison will be between the second element and whichever of the first two was larger.

    This continues until all the recursive calls are returned, at which point the index of the greatest element is returned to the calling function.

    Note then, that return size; should be replaced with return 0 to increase clarity.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this function: int firstHeapArray (IntHeapArray h) { if(!emptyHeapArray(h)) return h.array[0]; } It's
I have this function: void func(int a, int b, char s, FILE *fp, int
I have this function: public bool IsValidProduct(int productTypeId) { bool isValid = false; if
I have this function signature I have to match typedef int (*lua_CFunction) (lua_State *L);//target
I have this user defined function. public partial class UserDefinedFunctions { static int i;
I have an inline member function defined under class MyClass int MyClass::myInlineFunction(); This function
I have a private function like this in my controller. private UserDetails GetUserDetails(int userid)
I have a constexpr function that looks something like this: constexpr int foo(int bar)
I have this function int APIENTRY _tWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPTSTR lpCmdLine, int nCmdShow)
I currently have this function: public double Max(double[] x, double[] y) { //Get min

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.