Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Aug 24th, 2005, 1:46 PM   #11
OpenLoop
Expert Programmer
 
OpenLoop's Avatar
 
Join Date: May 2005
Location: East Lansing, MI
Posts: 663
Rep Power: 4 OpenLoop is on a distinguished road
Quote:
Originally Posted by melee28
I guess my question is....Where did I screw up?
You have too many printf() in your code. You should not print fib3 before you calculated it.
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;
}
OpenLoop is offline   Reply With Quote
Old Aug 24th, 2005, 1:59 PM   #12
stevengs
Professional Programmer
 
stevengs's Avatar
 
Join Date: May 2005
Location: Bad Nauheim, Germany
Posts: 436
Rep Power: 4 stevengs is on a distinguished road
Quote:
Originally Posted by OpenLoop
I guess stevengs posted while i was reviewing your code so it is the same thing
yep. ya know...I have the same problem with alignment. Is there an escape sequence for tabs in the CODE construct or others for that matter? I hate lining everything up with spaces.
__________________
-Steven
"Is this a piece of your brain?" - Basil Fawlty
stevengs is offline   Reply With Quote
Old Aug 24th, 2005, 2:07 PM   #13
OpenLoop
Expert Programmer
 
OpenLoop's Avatar
 
Join Date: May 2005
Location: East Lansing, MI
Posts: 663
Rep Power: 4 OpenLoop is on a distinguished road
Quote:
Originally Posted by stevengs
Is there an escape sequence for tabs in the CODE construct or others for that matter? I hate lining everything up with spaces.
No, they had a discussion about the tab key itself in the suggestions forum but it seems to be a browser dependent issue. Big_k wanted to put a button that adds 8 spaces everythime you click it 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.
OpenLoop is offline   Reply With Quote
Old Aug 24th, 2005, 2:09 PM   #14
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
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
DaWei is offline   Reply With Quote
Old Aug 24th, 2005, 2:13 PM   #15
coldDeath
Expert Programmer
 
coldDeath's Avatar
 
Join Date: Aug 2005
Location: UK
Posts: 862
Rep Power: 4 coldDeath is on a distinguished road
Send a message via AIM to coldDeath Send a message via Yahoo to coldDeath
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.
coldDeath is offline   Reply With Quote
Old Aug 24th, 2005, 3:24 PM   #16
stevengs
Professional Programmer
 
stevengs's Avatar
 
Join Date: May 2005
Location: Bad Nauheim, Germany
Posts: 436
Rep Power: 4 stevengs is on a distinguished road
Quote:
Originally Posted by DaWei
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 .

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
stevengs is offline   Reply With Quote
Old Aug 24th, 2005, 10:03 PM   #17
Ooble
I eat cake for breakfast.
 
Ooble's Avatar
 
Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 9 Ooble is on a distinguished road
Open Notepad/KWrite/vim/whatever, type a tab, copy it to the clipboard/Klipper/whoever, and paste. Problem solved.
__________________
Me :: You :: Them
Ooble is offline   Reply With Quote
Old Aug 25th, 2005, 12:02 AM   #18
aznluvsmc
Hobbyist Programmer
 
Join Date: Aug 2005
Posts: 137
Rep Power: 4 aznluvsmc is on a distinguished road
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.
aznluvsmc is offline   Reply With Quote
Old Aug 25th, 2005, 1:04 AM   #19
coldDeath
Expert Programmer
 
coldDeath's Avatar
 
Join Date: Aug 2005
Location: UK
Posts: 862
Rep Power: 4 coldDeath is on a distinguished road
Send a message via AIM to coldDeath Send a message via Yahoo to coldDeath
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.
coldDeath is offline   Reply With Quote
Old Aug 25th, 2005, 11:20 AM   #20
OpenLoop
Expert Programmer
 
OpenLoop's Avatar
 
Join Date: May 2005
Location: East Lansing, MI
Posts: 663
Rep Power: 4 OpenLoop is on a distinguished road
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;
}
OpenLoop is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 5:23 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC