Programming Forums
User Name Password Register
 

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

Reply
 
Thread Tools Display Modes
Old Aug 23rd, 2005, 11:12 PM   #1
melee28
Newbie
 
melee28's Avatar
 
Join Date: Aug 2005
Location: Orlando
Posts: 11
Rep Power: 0 melee28 is on a distinguished road
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
melee28 is offline   Reply With Quote
Old Aug 23rd, 2005, 11:27 PM   #2
aznluvsmc
Hobbyist Programmer
 
Join Date: Aug 2005
Posts: 137
Rep Power: 3 aznluvsmc is on a distinguished road
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?
aznluvsmc is offline   Reply With Quote
Old Aug 23rd, 2005, 11:41 PM   #3
melee28
Newbie
 
melee28's Avatar
 
Join Date: Aug 2005
Location: Orlando
Posts: 11
Rep Power: 0 melee28 is on a distinguished road
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
melee28 is offline   Reply With Quote
Old Aug 24th, 2005, 12:43 AM   #4
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 melee28
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
If loops (iteration) was not discussed, was recursion discussed? Otherwise I doubt you are going to be able to do anything except for something along th lines of printing out a static array . 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.
stevengs is offline   Reply With Quote
Old Aug 24th, 2005, 12:58 AM   #5
melee28
Newbie
 
melee28's Avatar
 
Join Date: Aug 2005
Location: Orlando
Posts: 11
Rep Power: 0 melee28 is on a distinguished road
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
melee28 is offline   Reply With Quote
Old Aug 24th, 2005, 1:26 AM   #6
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 melee28
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
By "directed", I meant directed by the book's author. They usually explain what approach they want you to use. If you do not use iteration or recursion, any program you write will essentially be a series of print statements that is only able to show a finite set of numbers. So in your program:
#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.
stevengs is offline   Reply With Quote
Old Aug 24th, 2005, 6:06 AM   #7
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
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
DaWei is offline   Reply With Quote
Old Aug 24th, 2005, 1:16 PM   #8
melee28
Newbie
 
melee28's Avatar
 
Join Date: Aug 2005
Location: Orlando
Posts: 11
Rep Power: 0 melee28 is on a distinguished road
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.
melee28 is offline   Reply With Quote
Old Aug 24th, 2005, 1:39 PM   #9
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
Sadly, your expectations don't matter a dam' to the machine, only what you tell it to do. Did you consider running the numbers, yourself, as below, and seeing what happens?
  printf("%2d", fib1);   // print 0     Output:	0
  printf("%2d", fib2);	 // print 1                 0 1
  printf("%2d", fib3);	 // print 1                 0 1 1
  temp = fib1 + fib2;	// temp = 1
  fib1 = fib2;             // fib1 = 1
  fib2 = fib3;             // fib2 = 1
  fib3 = temp;		 // fib3 = temp = 1
  
  
  printf("%2d", fib3);  // print fib3 (1)          0 1 1 1
  
  temp = fib1 + fib2;	// temp = 2
  fib1 = fib2;             // fib1 = 1
  fib2 = fib3;             // fib2 = 1
  fib3 = temp;           // fib3 = 2
  
  
  printf("%2d", fib3);  // print fib3 = 2          0 1 1 1 2
  
  temp = fib1 + fib2;	// temp = 2
  fib1 = fib2;             // fib1 = 1
  fib2 = fib3;             // fib2 = 2
  fib3 = temp;           // fib3 = 2
  
  
  printf("%2d", fib3);  // print fib3 (2)           0 1 1 1 2 2
__________________
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, 1:43 PM   #10
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
[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.
stevengs 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 10:35 PM.

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