# Nested Loop Simulation

Discussion in 'C' started by imported_Kuiva, Mar 10, 2013.

1. ### imported_KuivaNew Member

Joined:
Mar 10, 2013
Messages:
2
0
Trophy Points:
0
Code:
```#include <stdio.h>
#include <conio.h>

int A(int x)
{
x++;
return(x+1);
}

int B(int y,int z)
{
int t, i;

t=1;
for(i=1;i<=y;i++)
t*=i;

for(i=1;i<=z; i++)
t+=i;
return(t);
}

main()
{
int m,n,x;

m=2;
n=4;

printf("%d\n", B(m,n)+B(n,m));
printf("%d\n",A(B(m,n)));

m=3;
n=A(n);
m=B(n,m);
printf("%d\n%d\n%d", n,m,B(n,m));

getch();
return 0;

}
```
by simulation, i am able to get the values for the first two printf.

and on the 3rd printf,how come m=726 and B(n,m)=264261?
i run the code because i do not have the idea how to simulate to get the values for m and B(n,m)

please! somebody explain it.this will help me pass my exam. Nested Loop Simulation

2. ### hobbyistNew Member

Joined:
Jan 7, 2012
Messages:
141
0
Trophy Points:
0
The first printfs call by value so they don't change n or m when they return. After that,
you've assigned both n and m values

Code:
```m=3;
n=A(n);
m=B(n,m);
```
n = A(4) = 6
m = B(6, 3) = 726

Code:
`printf("%d\n%d\n%d", n,m,B(n,m));`
here your parameter list to printf looks like
6, 726, B(6, 726)

from the B definition

Code:
```B(6, 726)

t=1
for(i=1;i<=6;++i)
1 x 1 =   1
1 x 2 =   2
2 x 3 =   6
6 x 4 =  24
24 x 5 = 120
120 x 6 = 720

t now equals 720

for(i=1; i<=726; ++i)
720 + 1 = 721
721 + 2 = 723
723 + 3 = 726
...
```
I don't know the value of t with each iteration, but it's obvious that t will grow a bit

Joined:
Mar 10, 2013
Messages:
2