![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
|
|
#1 |
|
Newbie
Join Date: Aug 2005
Location: Orlando
Posts: 11
Rep Power: 0
![]() |
complete newbie question
Hey everyone. I recently decided to teach myself C programming by going through a college textbook and doing every single practice problem in the book. I'm still very new and prone to making lots of mistakes like this one:
I'm trying to write a program to print the first 10 digits of a Fibonacci sequence. For those that don't know, a Fibonacci sequence is one where each number is the sum of the previous two numbers. Basically, I want the program to print the following 0 1 1 2 3 5 8 13 21 34 (using mathematics, not just a blanket printf statement ). Now here's the catch... I've read a bit ahead and I know that this program could be more easily accomplished with a for loop, however, in an effort to learn things in the proper order, I've elected to do it the hard way. Here's my code so far.#include <stdio.h>
#include <stdlib.h>
int main(void)
{
/*Local Definitions*/
int fib1 = 0;
int fib2 = 1;
int fib3 = 1;
int temp = 0;
/*Statements*/
printf("%2d", fib1);
printf("%2d", fib2);
printf("%2d", fib3);
temp = fib1 + fib2;
fib1 = fib2;
fib2 = fib3;
fib3 = temp;
printf("%2d", fib3);
printf("\n");
system("PAUSE");
return 0;
}Now this if just for the first few numbers in the sequence, but I'll add on when the program is working properly. Can anyone offer any help? Thanks in advance. melee28 |
|
|
|
|
|
#2 |
|
Hobbyist Programmer
Join Date: Aug 2005
Posts: 137
Rep Power: 4
![]() |
You need a loop to calculate Fibonacci numbers or any sequence of numbers for that matter. BTW, I think the first two Fibonacci numbers are 1 and 1. What book are you using to study C?
|
|
|
|
|
|
#3 |
|
Newbie
Join Date: Aug 2005
Location: Orlando
Posts: 11
Rep Power: 0
![]() |
aznluvsmc- thanks for the quick reply. I'm using Computer Science: A Structured Programming Approach Using C as my text. In regards to the code, you're sort-of right about the first two numbers of a Fibonacci sequence being 1 and 1. By definition, they can be anything you want. For example, if you wanted one that started with 16 and 41, then it would be 16 41 57 98 and so on. There aren't any hard and fast rules as to what you start with. As far as needing a loop to do the program, I think you're wrong there. Only because the book has this practice problem in the back of Chapter 3... loops aren't discussed until chapter 5.
Thanks though, I need all the help I can get. :o melee28 |
|
|
|
|
|
#4 | |
|
Professional Programmer
Join Date: May 2005
Location: Bad Nauheim, Germany
Posts: 436
Rep Power: 4
![]() |
Quote:
. Come to think of it, "Structure and Interpretation of Computer Programs", SICP, a standard work at MIT, jumps right in with recursion (and this is a classic problem that can be very elegantly solved with recursion, making it simple to mathematically prove the validity through full-induction). It uses LISP/SCHEME however, but is otherwise[ ] an excellent textbook!Were you specifically directed not to use constructs that have not yet been "discussed"?
__________________
-Steven "Is this a piece of your brain?" - Basil Fawlty Last edited by stevengs; Aug 24th, 2005 at 1:00 AM. |
|
|
|
|
|
|
#5 |
|
Newbie
Join Date: Aug 2005
Location: Orlando
Posts: 11
Rep Power: 0
![]() |
stevengs- I wasn't directed to do anything really, this is for my own self education, not for school or anything. Recursion hasn't been discussed in the text yet and while I'm not opposed to using recursion or iteration, I guess I don't understand why it's necessary in this case. I know it's easier to use recursion or loops, it's just not the point of my "mental exercise". If my (limited) understanding is correct, nearly everything that can be done with a loop can also be done without one (though in a more complicated manner.) I know it's masochistic of me, but I want to see if it can be done and how.
melee28 |
|
|
|
|
|
#6 | |
|
Professional Programmer
Join Date: May 2005
Location: Bad Nauheim, Germany
Posts: 436
Rep Power: 4
![]() |
Quote:
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
int fib1 = 0; // This is the initialization
int fib2 = 1; // phase, where you initalize the
int fib3 = 1; // variables. This must only be
int temp = 0; // performed once in this program.
printf("%2d", fib1);
printf("%2d", fib2);
printf("%2d", fib3);
//****************************************
// The following must be performed over and over until
// you reach the fibonacci number you're targeting.
temp = fib1 + fib2;
fib1 = fib2;
fib2 = fib3;
fib3 = temp;
printf("%2d", fib3);
printf("\n");
//*****************************************
system("PAUSE");
return 0;
}Basically you must copy and paste the section marked 6 times.
...
temp = fib1 + fib2;
fib1 = fib2;
fib2 = fib3;
fib3 = temp;
printf("%2d", fib3);
printf("\n");
//*****************
temp = fib1 + fib2;
fib1 = fib2;
fib2 = fib3;
fib3 = temp;
printf("%2d", fib3);
printf("\n");
//*****************
temp = fib1 + fib2;
fib1 = fib2;
fib2 = fib3;
fib3 = temp;
printf("%2d", fib3);
printf("\n");
//*****************
...It is basically always the same... Wetten( initialize ), Lather and Rinse( excecute ), Repeat as necessary! The trick is usually in deciding when 'necessary' no longer applies. Apparently, the author wishes you to ask yourself, "There must be a more intelligent way to perform this repetitive and mundane task?", before "SHAZAM!"-ing you with loops or recursion.
__________________
-Steven "Is this a piece of your brain?" - Basil Fawlty Last edited by stevengs; Aug 24th, 2005 at 1:42 AM. |
|
|
|
|
|
|
#7 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
Steven is absolutely correct. Further, you will learn nothing useful by pursuing a non-iterative or non-recursive technique, beyond the ability to cut and paste. The ability of the microprocessor to operate usefully lies in its ability to depart from a series of consecutive operations and begin elsewhere, whether the elsewhere is ahead or behind.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code. Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers |
|
|
|
|
|
#8 |
|
Newbie
Join Date: Aug 2005
Location: Orlando
Posts: 11
Rep Power: 0
![]() |
So then what you guys are saying is...that the code I provided should work (albeit only for the first four numbers)? The problem I'm having is...it doesn't. I went ahead and copy/pasted for clarity, but the results I'm getting are as follows:
0 1 1 1 2 2 3 4 5 7 So the problem seems to lie in some fundamental aspect that I missed. Here is the updated code. #include <stdio.h>
#include <stdlib.h>
int main(void)
{
/*Local Definitions*/
int fib1 = 0;
int fib2 = 1;
int fib3 = 1;
int temp = 0;
/*Statements*/
printf("%2d", fib1);
printf("%2d", fib2);
printf("%2d", fib3);
temp = fib1 + fib2;
fib1 = fib2;
fib2 = fib3;
fib3 = temp;
printf("%2d", fib3);
temp = fib1 + fib2;
fib1 = fib2;
fib2 = fib3;
fib3 = temp;
printf("%2d", fib3);
temp = fib1 + fib2;
fib1 = fib2;
fib2 = fib3;
fib3 = temp;
printf("%2d", fib3);
temp = fib1 + fib2;
fib1 = fib2;
fib2 = fib3;
fib3 = temp;
printf("%2d", fib3);
temp = fib1 + fib2;
fib1 = fib2;
fib2 = fib3;
fib3 = temp;
printf("%2d", fib3);
temp = fib1 + fib2;
fib1 = fib2;
fib2 = fib3;
fib3 = temp;
printf("%2d", fib3);
temp = fib1 + fib2;
fib1 = fib2;
fib2 = fib3;
fib3 = temp;
printf("%2d", fib3);
printf("\n");
system("PAUSE");
return 0;
}I guess my question is....Where did I screw up? melee28 p.s. I know this isn't effective or pretty coding..just bear with me while I learn. |
|
|
|
|
|
#9 |
|
Professional Programmer
Join Date: May 2005
Location: Bad Nauheim, Germany
Posts: 436
Rep Power: 4
![]() |
[PHP]
#include <stdio.h> #include <stdlib.h> int main( void ) { /*Local Definitions*/ int fib1 = 0, fib2 = 1, temp = 0; printf( "%4d%4d", fib1, fib2 ); // Print the two givens fib1 = fib2; fib2 = temp; temp = fib1 + fib2; printf( "%4d", temp ); // 0 1 1 fib1 = fib2; fib2 = temp; temp = fib1 + fib2; printf( "%4d", temp ); // 0 1 1 2 fib1 = fib2; fib2 = temp; temp = fib1 + fib2; printf( "%4d", temp ); // 0 1 1 2 3 fib1 = fib2; fib2 = temp; temp = fib1 + fib2; printf( "%4d", temp ); // 0 1 1 2 3 5 fib1 = fib2; fib2 = temp; temp = fib1 + fib2; printf( "%4d", temp ); // 0 1 1 2 3 5 8 fib1 = fib2; fib2 = temp; temp = fib1 + fib2; printf( "%4d", temp ); // 0 1 1 2 3 5 8 13 fib1 = fib2; fib2 = temp; temp = fib1 + fib2; printf( "%4d", temp ); // 0 1 1 2 3 5 8 13 21 fib1 = fib2; fib2 = temp; temp = fib1 + fib2; printf( "%4d", temp ); // 0 1 1 2 3 5 8 13 21 34 printf( "\n" ); system( "PAUSE" ); return 0; }[/PHP] you have at least one variable too many (unnecessary) and your are printing all three before 'figuring' the first one! you can do it with two variables too: [PHP] int main( void ) { /*Local Definitions*/ int fib1 = 0, fib2 = 1; printf( "%4d%4d", fib1, fib2 ); fib2 += fib1; fib1 = fib2 - fib1; printf( "%4d", fib2 ); fib2 += fib1; fib1 = fib2 - fib1; printf( "%4d", fib2 ); fib2 += fib1; fib1 = fib2 - fib1; printf( "%4d", fib2 ); fib2 += fib1; fib1 = fib2 - fib1; printf( "%4d", fib2 ); fib2 += fib1; fib1 = fib2 - fib1; printf( "%4d", fib2 ); fib2 += fib1; fib1 = fib2 - fib1; printf( "%4d", fib2 ); fib2 += fib1; fib1 = fib2 - fib1; printf( "%4d", fib2 ); printf( "\n" ); system( "PAUSE" ); return 0; }[/PHP] can it be done with a single variable?
__________________
-Steven "Is this a piece of your brain?" - Basil Fawlty Last edited by stevengs; Aug 24th, 2005 at 2:11 PM. |
|
|
|
|
|
#10 | |
|
Expert Programmer
Join Date: May 2005
Location: East Lansing, MI
Posts: 663
Rep Power: 4
![]() |
Quote:
Also, calculate fib3 before you advance the rest of them. EDIT: I guess stevengs posted while i was reviewing your code so it is the same thing #include <stdio.h>
#include <stdlib.h>
int main(void)
{
/*Local Definitions*/
int fib1 = 0;
int fib2 = 1;
int fib3 = 1;
int temp = 0;
/*Statements*/
printf("%2d", fib1); //0
printf("%2d", fib2); //1
// printf("%2d", fib3); //don't print this yet
fib3 = fib1 + fib2; //fib3 = 0 + 1 = 1
fib1 = fib2; //fib1 = 1
fib2 = fib3; // fib2 = 1
printf("%2d", fib3); //now you can print it: 1
fib3 = fib1 + fib2; //fib3 = 1 + 1 = 2
fib1 = fib2; //fib1 = 1
fib2 = fib3; // fib2 = 2
printf("%2d", fib3); //2
fib3 = fib1 + fib2; //fib3 = 1 + 2 = 3
fib1 = fib2; //fib1 = 2
fib2 = fib3; // fib2 = 3
printf("%2d", fib3); //3
fib3 = fib1 + fib2; //fib3 = 2+ 3 = 5
fib1 = fib2; //fib1 = 3
fib2 = fib3; // fib2 = 5
printf("%2d", fib3); //5
fib3 = fib1 + fib2;
fib1 = fib2;
fib2 = fib3;
printf("%2d", fib3);
fib3 = fib1 + fib2;
fib1 = fib2;
fib2 = fib3;
printf("%2d", fib3);
fib3 = fib1 + fib2;
fib1 = fib2;
fib2 = fib3;
printf("%2d", fib3);
printf("\n");
system("PAUSE");
return 0;
} |
|
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|