Wednesday, May 26, 2010

Fibonacci

Alright, I've just received my first email coming from Von Espiritu of Quezon City.


Sir,

My college professor wants us to make a program in C that will take an input and make a Fibonacci until it reaches the number input-ed integer. I hope you might help me in my problem because it is our last exercise, indeed a make or break activity for my summer class.

Thanks,
Von Espiritu

Alright, First of all. Von, I am really glad that you emailed me. You're my first email sender by the way. And I am really  grateful to share my solution with you. My college professor also assigned us an activity for fibonacci but it's coded using java. But hey, We programmers don't contain ourselves on a programming languages. We explore more and more. And as long as you have the idea and you know the syntax. You basically can do anything! So cheers to that Von!



Alright. We are going to right this on C. The very first programming language I've learned. I'll just give you the body though, and explain the codes line by line.



Oh yeah. What is Fibonacci? It is a series of numbers where when you add the first two previous numbers, you'll get the third number. so on and so fort.

Here's a fibonacci sequence.


0,1,1,2,3,5,8,13,21,34,55,89,144.....

See that the first two numbers is 0 and 1. The third number is calculated by adding the first two previous numbers. so 0 + 1 = 1. So thus the 1 + 1 =2 and 2 + 3 = 5.

Okay, now that we have an idea about it. Let's build the logic. Remember, It's always a good practice to plan first and build the logic, the manner on how you'll solve a specific problem before you engage in coding. I know it's somewhat a drag but it pays.

So. The program wants us to take an input. and do a fibonacci sequence until the number of the integer series reaches the inputed integer.

Example

Input: 5
Output: 0,1,1,2,3

or

Input: 8
Output: 0,1,1,2,3,5,8,13

Now, to do it. We need a to declare a variable to hold for the input number.a counter for the for loop which we will label "ctr" and an array that will hold the fibonacci series which is "out". For the input say we label a variable "num". The num will take the input and will serve as the fibonacci number perimeter for our for loop. Also, the num will serve as the perimeter of the number of outputs.

int num,ctr,out[999];
 Declares num,ctr,out. note that in declaring an array in C. It should be followed by braces to indicate the index size of an array. There are many kinds of array but in this post, we'll just use single dimensional array.

printf("Input number:");
 Print the display.

out[0] = 0;
out[1] = 1;
Now. Why did we pre-declare the value for out[0] and out[1]?
Simple. Because it is a constant value in any fibonacci sequence.

scanf("%d", &num);
Input number of course.

for(ctr = 2; ctr <= num; ctr++){
out[ctr] = out[ctr-1] + out[ctr-2];

Now this for loop. the out[ctr] is the current array which we will calculate the value and to calculate the value for that, we need to know the first two previous numbers before it. Thus, out[ctr-1] and out[ctr-2].

for(ctr = 0 ; ctr<= num; ctr++){
printf("Out[ctr] = %d", num);

}

Since we have filled the array with fibonacci series. We need to output it.

There! You're done. You can now see the series of fibonacci numbers! There you go Von. I hope you passed your summer class activity with flying colors and hoping to hear from you soon.

PS: Heres the whole code.

int num,ctr,out[999];
printf("Input number:");
out[0] = 0;
out[1] = 1;
scanf("%d", &num);
for(ctr = 2; ctr <= num; ctr++){
out[ctr] = out[ctr-1] + out[ctr-2];
}
for(ctr = 0 ; ctr<= num; ctr++){
printf("Out[ctr] = %d", num);
}

Good luck and Godspeed!

[/code]

No comments: