search
Log In
3 votes
582 views
Consider the following function:
void madeeasy (int n)
{
if (n < 0) return;
else
{
printf(n);
madeeasy (- -n);
madeeasy (n - -);
printf(n);
}
}
The sum of all values printed by madeeasy (5)_______

(I am getting -12 but given answer is 52)
in Programming 582 views
0
temination condition is on n<0 ;

52 is answer if termination condition is n<=0
1
Yes..even i got -12..there are lot of mistakes in ME mocks
0

@balchandar reddy san

Consider the following code:
int P = 0;
for (i = 1; i < 2n; i++)
{
for (j = 1, j<=n; j++)
{
if(j<i) P=P+1;
}
}
printf(“%d”, P);

What is the output printed by the above code in terms of n?

 

what about this i calculated ans as  (3*$n^{2}$-3*n)/2 but in answer they have given (3*$n^{2}$-n)/2

0

Yes..they have given the solution by assuming:

for (i = 1; i <= 2n; i++)

Even i got the same...

0
hmm.thanks!
0

@newdreamz a1-z0 one question, what do you feel is the answer for this https://gateoverflow.in/301271/me-mock-4,?

0
i was sure only with the first part .second part i did'nt get.
0
Too lengthy to solve!
0

But how you get 12 ??? @balchandar reddy san

please tell me the answer..

0

refer here: https://gateoverflow.in/301258/me-mock-4 for the answer given

3 Answers

3 votes
 
Best answer

Anwer is -12


selected by
1 vote

Case 1 : If termination condition is on n<0 ;

if (n < 0)

then answer will be -12

 

Case 2 : if termination condition is n<=0

if (n <= 0)

then answer will be  52

0 votes

Whenever the function decreases the recursive call value, it's better do follow a bottom up approach.

I'll rename madeeasy() to m() for simplicity.


m(0) runs like this:

print 0

m(-1); starting m(-1) is pointless, as it just makes us return.

m(-1)

print -2

Hence, Sum = -2

m(1) runs like this:

print 1

m(0); We know m(0) gives us sum -2.

m(0); We know m(0) gives us sum -2.

print -1

Hence, Sum = -4

Similarly, m(2) gives us sum: -6

m(3) gives us sum: -8

m(4) gives us sum: -10

m(5) will give us -12.

Related questions

0 votes
1 answer
1
644 views
#include<iostream> using namespace std; int i=0; void a() { i+=1; cout<<i<< ".hello"<<endl; a(); } int main() { a(); } For this above code the output is only upto → 64891.Hello Does this mean that that the stack can hold only 64891 recursive calls? (I am using dev c++)
asked Jun 2, 2019 in Programming Hirak 644 views
1 vote
2 answers
2
408 views
int zap(int n) { if (n<=1) then zap =1; else zap = zap(n-3)+zap(n-1); } then the call zap(6) gives the values of zap Give the proper explanation
asked Aug 28, 2018 in Programming shivam sharma 5 408 views
2 votes
2 answers
3
352 views
int f (int n){ if (n==0) return 0; if(n==1) return 1; else return f(n-1)+f(n-2); } Find the upper bound and lower bound to the number of function calls for input size 'n'?
asked Jul 8, 2018 in Programming parasghai28 352 views
1 vote
1 answer
4
437 views
void ab() { auto int a; static int s= 5; a = ++s; printf("%d%d",a,s); if(a<= 7) ab(); printf("%d%d",a,s); } void main() { ab(); } According to me answer should be- 667788887766 but the answer is - 667788887868. Please explain
asked Jul 8, 2018 in Programming parasghai28 437 views
...