Solved

# Comparing 2 little codes with same result, how to understand it?

Posted on 2011-10-14
Medium Priority
383 Views
I can't understand this 2 little pieces of code, can someone explain why both codes return "720"?

I think all I need is someone to explain or comment this 2 codes so I can understand.
``````-------- CODE NUMBER 1 RETURN 720 -----------

#include <stdio.h>
int main(){
int i, resultado = (13 == 13);
for( i = 0; i < 6; i++)
return 0;
}

-------- CODE NUMBER 2 RETURN 720 ALSO -----------

#include <stdio.h>
int calcula(int x){
if ( x == 0)
return 1;
else
return x * calcula(x-1);
}
int main(){
printf("El resultado es %d", calcula(6));
return 0;
}
``````
0
• 4
• 4
• 3
• +2

LVL 7

Expert Comment

ID: 36972197
Both functions return 0, not 720.  Do you mean they both print 720 for "el resultado"?
0

LVL 84

Expert Comment

ID: 36972218
#include <stdio.h>
int main(){
int i, resultado = (13 == 13);  /* 13 is equal to 13 therefore returns 0  (true) */
for( i = 0; i < 6; i++)
resultado = resultado * (i+1);  /* 0 times anything is 0 */
printf("El resultado es %i", resultado); /* %f is a double you need an int */  /* change the %d to %i */
return 0;
}

-------- CODE NUMBER 2 RETURN 720 ALSO -----------

#include <stdio.h>
int calcula(int x){   /* is 6 == 0 no return false 1   */
if ( x == 0)
return 1;
else
return x * calcula(x-1);   /* 1 x ((1-1) =0) = 0 */
}
int main(){
printf("El resultado es %i", calcula(6));
return 0;
}

you should get 0 both times
0

LVL 7

Expert Comment

ID: 36972227
First, resultado is initialized to 1 via the seemingly pointless comparison (13 == 13).

Both calculate the factorial of 6, one iteratively and the other recursively.

The first is simple enough, all numbers 1 to 6 are multiplied.
You can think of the second like this: the factorial of any number, n, is equal to n * the factorial of n - 1.  I.e., n! = n(n - 1)!

0

LVL 2

Author Comment

ID: 36972229
When I copy and paste every code to DevC++ both provide same message "El resultado es: 720".
0

LVL 7

Expert Comment

ID: 36972239
well, any number > 1.  :)
0

LVL 84

Expert Comment

ID: 36972289
Technically, the codes Don't return 720 but both programs Do print it to the screen...
0

LVL 84

Expert Comment

ID: 36972315
I copied and pasted your code and for both I got 0 as the answer as shown
here
.
how you get 6! = 720 I have no idea.
0

LVL 2

Author Comment

ID: 36972346
Slimfinger:

I think you may be right, now could you please explain how I understand the following sentences:

"13==13" can I say that if it is true=1?

"return 1" return value

"return 0" return 0 value? or does not return a value?
0

LVL 84

Expert Comment

ID: 36972364
I copied both little programs and compiled them with gcc and when I ran them, they both printed "El resultado es: 720" to the screen.  The return code of '0' is for no error, good return.  ??
0

LVL 7

Expert Comment

ID: 36972365
One thing at a time!  lol!
My advice would be to take your time, delete that code above, and look up some explanations on recursion, etc., on Google.
0

LVL 84

Expert Comment

ID: 36972448
if a program runs without an error the default return code is 0, programmers set the return code other than 0 to indicate where the program failed or just a 1
0 = true
1 = false
you can define true and false
#undef true
#define true 1
#undef false
#define false 0
as for return codes

DOS error codes are 0-255 and when tested using the 'errorlevel' syntax mean anything above or including the specified value, so the following matches 2 and above to the first goto, 1 to the second and 0 (success) to the final one!

IF errorlevel 2 goto CRS
IF errorlevel 1 goto DLR
IF errorlevel 0 goto STR

gcc and VC++ True is 1 and False is 0
Dev C++ has them reversed

a good discussion is located here
0

LVL 8

Accepted Solution

ssnkumar earned 500 total points
ID: 36972806
-------- CODE NUMBER 1 RETURN 720 -----------

#include <stdio.h>
int main()
{
int i, resultado = (13 == 13); ==> (13 == 13) is a comparison operator. This returns 1 if it is equal and 0 otherwise. In this case, both sides are 13 and hence they are equal - So, it returns 1. So, resultado gets the value 1.

for( i = 0; i < 6; i++)
==> When the value of i=0 - resultado =   1 * (0+1) = 1
==> When the value of i=1 - resultado =   1 * (1+1) = 2
==> When the value of i=2 - resultado =   2 * (2+1) = 6
==> When the value of i=3 - resultado =   6 * (3+1) = 24
==> When the value of i=4 - resultado =  24 * (4+1) = 120
==> When the value of i=5 - resultado = 120 * (5+1) = 720

printf("El resultado es %d", resultado);  ==> So, this prints 720

return 0;
}

-------- CODE NUMBER 2 RETURN 720 ALSO -----------

#include <stdio.h>
int calcula(int x)      ==> main() calls this with the value of 6.
{
==> When x is 6, it returns 6 * calcula(5)   -- call# 1
==> When x is 5, it returns 5 * calcula(4)   -- call# 2
==> When x is 4, it returns 4 * calcula(3)   -- call# 3
==> When x is 3, it returns 3 * calcula(2)   -- call# 4
==> When x is 2, it returns 2 * calcula(1)   -- call# 5
==> When x is 1, it returns 1 * calcula(0)   -- call# 6
==> calcula(0) returns 1.
==> Now it returns 1 to the call where calcula() was called with 0.
That is call# 6 in the above steps = 1 * calcula(0) = 1*1 = 1
==> This returns to the call where calcula() was called with 1.
That is call# 5 in the above steps = 2 * calcula(1) = 2*1 = 2
==> This returns to the call where calcula() was called with 2.
That is call# 4 in the above steps = 3 * calcula(2) = 3*2 = 6
==> This returns to the call where calcula() was called with 3.
That is call# 3 in the above steps = 4 * calcula(3) = 4*6 = 24
==> This returns to the call where calcula() was called with 4.
That is call# 2 in the above steps = 5 * calcula(4) = 5*24 = 120
==> This returns to the call where calcula() was called with 5.
That is call# 1 in the above steps = 6 * calcula(5) = 6*120 = 720
if ( x == 0)
return 1;
else
return x * calcula(x-1);
}
int main()
{
printf("El resultado es %d", calcula(6));  ==> calcula(6) goes into a recursive call and returns 720, as explained above and gets printed.
return 0;
}`
0

LVL 2

Author Comment

ID: 36973373
ssnkumar:

In this call I dont understand why you received 24
That is call# 3 in the above steps = 4 * calcula(3) = 4*6 = 24

what I am doing (replacing) is :

return x * calcula(x-1); i am replacing with this formula x*x-1 is this correct?

2*2-1 = 2*2 =2 this is ok
3*3-1 = 3*2 =6 this is ok
4*4-1 = 4*3 =12 this should be 24, why?
0

LVL 84

Expert Comment

ID: 36974381
no it uses the value of the previous calculation

2*1=2
2*3=6
6*4=24
24*5=120
120*6=720
0

LVL 8

Expert Comment

ID: 36977856
> In this call I dont understand why you received 24
>    That is call# 3 in the above steps = 4 * calcula(3) = 4*6 = 24
calcula(3) is the value that we got in the previous step.
That is:
3 * calcula(2) = 3*2 = 6
In the code that you have posted, at line# 19 we have:
return x * calcula(x-1);

Now when you do: calcula(4)
it becomes:
4 * calcula(3)

And we know that calcula(3) is 6.
So, the value becomes
4 * 6 = 24.
0

## Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If you havenâ€™t already, I encourage you to read the first article (http://www.experts-exchange.com/articles/18680/An-Introduction-to-R-Programming-and-R-Studio.html) in my series to gain a basic foundation of R and R Studio.Â  You will also find the â€¦
Looking for a way to avoid searching through large data sets for data that doesn't exist? A Bloom Filter might be what you need. This data structure is a probabilistic filter that allows you to avoid unnecessary searches when you know the data definâ€¦
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.
###### Suggested Courses
Course of the Month14 days, 7 hours left to enroll

#### 839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.