Ask your own question, for FREE!
Mathematics 16 Online
OpenStudy (lgbasallote):

Discrete Mathematics question inside

OpenStudy (lgbasallote):

suppose the code was this ``` main() { int n, i, a, b, c; printf("How many numbers in the series? "); scanf("%d", &n); a = 0; b = 1; for (i = 0; i < n; i++) { a = b; b = c; c = b + a; printf("%d ",c ); } }

OpenStudy (lgbasallote):

then the pseudocode will be ``` procedure fib(n : integer) ``` <Y/N>

OpenStudy (lgbasallote):

need some help here... @hartnn

hartnn (hartnn):

yes, there will be one input.

OpenStudy (lgbasallote):

then i suppose it continues ``` procedure fib (n : integer) for i = 2 to n a = b b = a c = b + a {c is the sum of the two previous terms} ``` is there something missing?

OpenStudy (lgbasallote):

ahh yes ``` procedure fib (n : integer) a = 0 b = 1 for i = 2 to n a = b b = a c = b + a {c is the sum of the two previous terms} ```

hartnn (hartnn):

yes. what are u trying to do with that procedure ?

OpenStudy (lgbasallote):

no idea

OpenStudy (lgbasallote):

``` procedure fib (n : integer) a = 0 {a is the first number of the series} b = 1 {b is the second number of the series} for i = 2 to n a = b b = a c = b + a {c is the sum of the two previous terms} ```

hartnn (hartnn):

are u only returning next fib number ? (then its incorrect) u realize that here u have to call the fib function again in for loop. like make it recursive ...

OpenStudy (lgbasallote):

what do you mean call again

hartnn (hartnn):

u wanna display only last fib number? then that procedure is correct! if want to display all fib numbers then u have to recursive call fib inside for loop

OpenStudy (lgbasallote):

recursive call?

hartnn (hartnn):

the code u wrote displays all

OpenStudy (lgbasallote):

are we talking about my code? or my pseudocode?

hartnn (hartnn):

code in 1st comment displays all fib numbers your pseudocode displays(or i should say returns) last fib number

OpenStudy (lgbasallote):

then how should i write it so that it displays all?

hartnn (hartnn):

see whether this makes sense procedure fib (n : integer) a = 0 b = 1 a = b b = a c = b + a display c a=b b=c procedure fib(n-1:integer) {c is the sum of the two previous terms}

hartnn (hartnn):

no it won't work.

OpenStudy (lgbasallote):

?

hartnn (hartnn):

because everytime , u will initialize a=0,b=1...that should not be done

OpenStudy (lgbasallote):

this is how a bubblesort algorithm looks like

OpenStudy (lgbasallote):

so i think what i did is fine without this "recursion"

hartnn (hartnn):

|dw:1350368608149:dw|

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!