• edited by
1,485 views
0 0 votes
rec(int x)
{
    static int f;
    if(x==1)
    return 1;
    else
    f+=x*rec(x-1);
    return(f);
}

the value returned by rec(5) is_____________

2 Answers

Best answer
2 2 votes
initially  f should be initialized to 0

so f=0

function f=f+x*rec(x-1)

rec(1)=1

f(2)=0+2*rec(1)=2

f(3)=2+3*rec(2)=8

f(4)=8+4*rec(3)=40

f(5)=40+5*rec(4)=240

s0 f returns 240
• selected by
3 3 votes
rec(5)-> f=f+(5*rec(4))=240

rec(4)->f=f+(4*rec(3))=40

rec(3)->f=f+(3*rec(2))=8

rec(2)->f=f+(2*rec(1))=2

rec(1)=1

so answer of rec(5) is 240
Position:
Show:

Related questions

1 1 vote
1 1 answer
130
130 views
khanshahima4 asked 4 days ago
130 views
1. If precedence tells me which operator has priority, and associativity tells me how to handle operators at the same level, then why do I need a separate concept called ...
13 13 votes
1 1 answer
1.2k
1.2k views
GO Classes asked Jul 2
1,234 views
Assume:$\texttt{short = 2 bytes}$, $\texttt{int = 4 bytes}$, $\texttt{char = 1 byte}$$\texttt{int}$ needs $4$-byte alignment and the final structure size is rounded to a ...
16 16 votes
2 2 answers
662
662 views
GO Classes asked Jul 2
662 views
Assume:struct Student { double gpa; }; struct Student alice; struct Student *sptr = &alice;Which of the following correctly assigns $\texttt{4.0}$ to $\texttt{alice.gpa}$...
9 9 votes
1 1 answer
490
490 views
GO Classes asked Jul 2
490 views
Consider the following code idea from the source:typedef struct { double x, y; } Point; void reset(Point p) { p.x = p.y = 0; } int main() { Point a = {12.0, 42.0}; Point ...