![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#11 | |
|
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;
} |
|
|
|
|
|
|
#12 | |
|
Professional Programmer
Join Date: May 2005
Location: Bad Nauheim, Germany
Posts: 436
Rep Power: 4
![]() |
Quote:
__________________
-Steven "Is this a piece of your brain?" - Basil Fawlty |
|
|
|
|
|
|
#13 | |
|
Expert Programmer
Join Date: May 2005
Location: East Lansing, MI
Posts: 663
Rep Power: 4
![]() |
Quote:
but nothing yet. You should start a thread in the suggestions forum and tell them about your idea, maybe add a <t> or <tab> or something that to insert a tab in your code. |
|
|
|
|
|
|
#14 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
The main problem is that the OP's code has tabs in it. If you cut and paste into your own editor, add some stuff, and cut and paste it back, the tabs remain, and garfle things up. My editor is set to convert tabs to the appropriate number of spaces, but if I don't save the file in the interim (or press the tab key myself), that doesn't happen. Ugly repaste. OP's that don't have their editor set to replace tabs with spaces should be beat with an uncooked noodle and hung with a new rope
.
__________________
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 |
|
|
|
|
|
#15 |
|
Expert Programmer
|
a [tab] function would rock. when i press teab it selects the post button >
![]()
__________________
Join us at #programmingforums @ irc.freenode.net! My software never has bugs. It just develops random features.
|
|
|
|
|
|
#16 | |
|
Professional Programmer
Join Date: May 2005
Location: Bad Nauheim, Germany
Posts: 436
Rep Power: 4
![]() |
Quote:
careful talkin' 'bout beatin's and noodles and hung in the same sentence ![]() It'd probably be more trouble than it's worth with the tab if there isn't one already there. I will just use spaces .. np.
__________________
-Steven "Is this a piece of your brain?" - Basil Fawlty |
|
|
|
|
|
|
#17 |
|
I eat cake for breakfast.
![]() ![]() ![]() ![]() Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 9
![]() |
Open Notepad/KWrite/vim/whatever, type a tab, copy it to the clipboard/Klipper/whoever, and paste. Problem solved.
|
|
|
|
|
|
#18 |
|
Hobbyist Programmer
Join Date: Aug 2005
Posts: 137
Rep Power: 4
![]() |
Just as a note, I'm reading C Primer Plus Fifth Edition by Stephen Prata (Sams Publishing) and they also ask you to write a program to calculate Fibonacci numbers. They specifically put this in the chapter on loops.
|
|
|
|
|
|
#19 |
|
Expert Programmer
|
So does the python tutorial.
It is best to use a loop to do it.
__________________
Join us at #programmingforums @ irc.freenode.net! My software never has bugs. It just develops random features.
|
|
|
|
|
|
#20 |
|
Expert Programmer
Join Date: May 2005
Location: East Lansing, MI
Posts: 663
Rep Power: 4
![]() |
the fastest way to calculate Fib's numbers is to use a recursive funtion:
int fib(int a, int b) {
if (b > 100000)
return b;
std::cout<<b<<" ";
return (fib(b,a+b));
}
int main() {
fib(0,1);
return 0;
} |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|