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

  • SEARCH
  • Home
  • 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 6325457
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T16:53:57+00:00 2026-05-24T16:53:57+00:00

The output of the below program is 24. But I could not understand the

  • 0

The output of the below program is 24. But I could not understand the behaviour of the function f1(parameters).

There is a recursive call of f1 in m1 nad m2. Considering m1 and m2 holding the function f1’s stack. m1 stack will contain:

1]0,12,a 2]0,6,a 3]0,3,a 4]0,1,a 5]0,0,a

And m2 stack will contain:

1]13,12,a 2]20,6,a 3]24,3,a 4]26,1,a 5]27,0,a

What m1 and m2 values hold? please explain this behaviour of the recursive function.

#include <stdio.h>
main()
{
 int i, n, m, b, x[25];
 int f1(int, int, int j[25]);
 for(i=0;i<25;i++) x[i] = i;
 i=0; m = 24;
 b=f1(i, m, x);
 printf("res %d\n",b);
}

int f1( int p, int q, int a[25])
{
 int m1,m2;
 if (q==0)
  return(a[p]);
 else
 { 
  m1 = f1 (p, q/2, a);
  m2 = f1(p+q/2+1,q/2,a);
  if(m1<m2)
   return (m2);
  else
   return(m1);
 }
}
  • 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-05-24T16:54:00+00:00Added an answer on May 24, 2026 at 4:54 pm

    Well, there are two points to understanding the code:

    1. Uncovering the actual algorithm among the heaps of terrible C and
    2. understanding the recursive function itself.

    What always helped me when trying to understand such code was to:

    1. Clean up the C code. In this case it means:

      1. Mentally tracing the initialization and rewriting to static initialization, so you see what the values look like:

        int x[25] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12,
        13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24 };

      2. Getting rid of the useless assignment to i and x so you have the initial call in plain sight.

    2. Rewrite the function to mathematical notation, pseudocode or even human laguage.

      f(p, q, array) = larger of f(p, q/2, array) and f(p+q/2+1, q/2, array)
      

      if you continue a little bit further in using human language, you’ll clearly see what is was supposed to do.

    Now I said “was supposed to do” on purpose. The (p, q/2) and (p+q/2+1, q/2) look like the first and second half… except they are not.

    The code is supposed to return 24, but it’s totally wrong and the stacks quoted in the question actually prove it. The m2 stack contains as last point “f1(27, 0, a)” and that invocation is going to do a[27], but the array only has 25 elements! (by pure chance the memory above was probably 0-initialized, so it did return the 24, but if it was initialized by some debug pattern instead (I’ve seen 0xdeadbeef, 0xa5a5a5a5 and 0xcccccccc), it would return that).

    In C it’s by design (and C++ uses them everywhere) easiest to use half-open intervals. [start, one-past-end) or start+length, which convert to each other nicely. However here the function gets (start, length-1) and treats it inconsistently inside.

    As a student, you have to be able to understand such code, because you’ll meet lots of crappy unreadable bug ridden code that only works by accident in the wild. But if you ever present something like this, you will rightfully fail the exam.

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

Sidebar

Related Questions

In below program i am getting output as false .But as per my understanding
My LINQ query is not producing the expected output below. Basically, it's the sum
I'm loading Json into an NSDictionary and the output is below. I'm just not
I am newbie to C. I googled alot regarding the output of below code.But
I am new to Java. I executed the below program successfully but I don't
I've read Statics in Java are not inherited. I've a small program below which
I'm not a software person, but I could really use some advice. I'm writing
How can I get the code below to output node names that I define?
Ive a HABTM relationship where the output is like below, what i would like
I'm having some problems trying to get the code below to output the data

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.