Ask your own question, for FREE!
Computer Science 9 Online
OpenStudy (lgbasallote):

can someone explain this code for me? it's a c code...

OpenStudy (lgbasallote):

#include<stdio.h> #include<conio.h> #include<stdlib.h> main() { int x=0,y=1,a,b,c; printf("Enter limit: \t"); scanf("%d",&a); system("cls"); if(a==0) { printf("%d\n",x); } else { printf("%d %d ",x,y); } for(b=2;b<=a;b++) { c=x+y; printf(" %d",c); x=y; y=c; } getch(); }

OpenStudy (anonymous):

It's the fibonnaci algorithm in O(a) complexity, it calculates and prints all the fibonnaci terms up to a

OpenStudy (lgbasallote):

i need the code explained..i mean why these codes can make the fibonacci

OpenStudy (anonymous):

because, we know the recursive fibonnaci function as F0=0, F1=1 Fn=Fn-1+Fn-2.F0=x=0,F1=y=0, if a=0 then your guy wants to output F0, so x, if not, you normally output (x,y)=(0,1).When you enter the loop, you add the two numbers x,y to make c(the next fibonnaci term) and then you give x the value c and y the value x,where c=Fn and x=Fn-1, so that in the next loop, when you want to calculate Fn+1 it would be equal to x+y=Fn+Fn-1.It's a bit confusing, you can do it in O(a) memory in a very simple way but this is why this algorithm works

OpenStudy (anonymous):

F1=y=1 *

OpenStudy (lgbasallote):

uhh maybe i should make my question more specific... int x=0,y=1,a,b,c; what does that mean first?

OpenStudy (anonymous):

that's how you initialize variables... Fa is the maximum fibonnaci you will calculate, b is the counter and c is Fn.X is F0 and y is F1 at the beginning.I think i already explained that, anyway

OpenStudy (lgbasallote):

what do you mean Fa, Fn.X, F0 and F1 o.O

OpenStudy (anonymous):

Fa-> the fibonnaci term at position a, F0 the fibonnaci term at position 0, Fn the fibonnaci term at position n and F1 the fibonnaci term at position 1.

OpenStudy (lgbasallote):

can we not use these F lol...can we use the variables here..itll make me analyze it a lot easier sorry so why is x = 0 and y = 1?

OpenStudy (anonymous):

i cannot explain you how these variables work if i cannot use fibonnaci terms, x is the 0th term of fibonnaci sequence and y is the 1st, as we enter the procedure x and y progress, x becomes x+y ,(b+1)th fibonnaci term and y becomes x, the bth fibonnaci term.. that's all.. swaping between terms

OpenStudy (lgbasallote):

lol i just meant let's use a, b, c, d, x and y..not FX thingies..that's possible right?

OpenStudy (lgbasallote):

a is the limit right? it'sthe number of terms in the series? what does b mean?

OpenStudy (anonymous):

b is a counter, and no it''s not possible to explain a mathematical problem without using some basic math, the FX thingie is the whole point in here...

OpenStudy (lgbasallote):

hmm okay..why b=2? this means the sequence will start at the 2nd term right?

OpenStudy (anonymous):

yes that is correct, and that happens between, if a = 0 you just print 0 and exit, if not you just print the first two terms 0,1 and then enter the loop to calculate the 2nd term, you don't need to calculate anything double and that is why b starts from 2

OpenStudy (lgbasallote):

then b<=a because a is the limit and b++ because increment... c = x+y does this mean next term = previous term +current term?

OpenStudy (anonymous):

this means that the current term equals to the previous term + the previous previous term, it is quite clear if you check the swapping thing after printf it's not that hard..Reread my posts if you want to g2g...

OpenStudy (lgbasallote):

hmm okay thanks

Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!
Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!