Welcome to Dream.In.Code
Become a C++ Expert!

Join 149,934 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,573 people online right now. Registration is fast and FREE... Join Now!




LOOP HELP

 
Closed TopicStart new topic

LOOP HELP

james evans
4 Dec, 2007 - 10:23 PM
Post #1

New D.I.C Head
*

Joined: 4 Dec, 2007
Posts: 5


My Contributions
I have a problem with setting up my loop structure I think I did it correctly
but when I run the program it returns zer0's down the page instead of the Fibonacci numbers.
CODE
//Ch7AppE10.cpp – displays the first 10 Fibonacci numbers
//Created/revised by <your name> on <current date>

#include <iostream>

using std::cout;
using std::endl;

int main()
{
    //declare variables
    int Fibonaccinumbers = 0; //counter

  for (int count = 55; count >= 1; count = count + 1)
      cout << Fibonaccinumbers << endl;
  
  {
      count = count + 1;
  }  //end for

while (Fibonaccinumbers > 0)
{
    cout << Fibonaccinumbers << endl;
    count = count + 1;
}  //end while

    
    
    return 0;
}   //end of main function

please get back to me as soon as possible, while I give it another try blink.gif

User is offlineProfile CardPM
+Quote Post

james evans
RE: LOOP HELP
5 Dec, 2007 - 08:16 AM
Post #2

New D.I.C Head
*

Joined: 4 Dec, 2007
Posts: 5


My Contributions
I have a problem with setting up my loop structure I think I did it correctly
but when I run the program it returns zer0's down the page instead of the Fibonacci numbers.
CODE//Ch7AppE10.cpp – displays the first 10 Fibonacci numbers
//Created/revised by <your name> on <current date>

CODE
#include <iostream>

using std::cout;
using std::endl;

int main()
{
    //declare variables
    int Fibonaccinumbers = 0; //counter

  for (int count = 55; count >= 1; count = count + 1)
      cout << Fibonaccinumbers << endl;
  
  {
      count = count + 1;
  }  //end for

while (Fibonaccinumbers > 0)
{
    cout << Fibonaccinumbers << endl;
    count = count + 1;
}  //end while

    
    
    return 0;
}   //end of main function

please get back to me as soon as possible, while I give it another try
User is offlineProfile CardPM
+Quote Post

WolfCoder
RE: LOOP HELP
5 Dec, 2007 - 09:30 AM
Post #3

ギュウ~
Group Icon

Joined: 5 May, 2005
Posts: 3,718



Thanked: 8 times
Dream Kudos: 1450
My Contributions
Welcome to Dream In Code!
For us to help you, you need to post this in the correct forum.
User is online!Profile CardPM
+Quote Post

NickDMax
RE: LOOP HELP
5 Dec, 2007 - 09:51 AM
Post #4

2B||!2B
Group Icon

Joined: 18 Feb, 2007
Posts: 2,869



Thanked: 53 times
Dream Kudos: 550
My Contributions
...all your told it to do was print zeros...

Generally loops have 4 parts: Setup (initialization), looped code, the increment, exit condition. Now these are just names I am making up but here is the idea behind each part:

setup: do whatever you need to set up the conditions for the loop
loop: some code that you want to repeat.
increment: CHANGE SOMETHING, preferably so that at some point your exit condition will happen.
exit condition: how do we get OUT of the loop? Note this must be linked to the "increment" step.

lets take a look at one of your loops:
QUOTE
CODE
while (Fibonaccinumbers > 0)
{
    cout << Fibonaccinumbers << endl;
    count = count + 1;
}  //end while
Now the problem here is that you have an increment but it is not linked to your condition... the only thing that changes is count. -- also note that you are trying to display Fibonacci numbers but the variable Fibonaccinumbers does not change at all... which is why you just point '0' over and over.

So... in order for your program to work you have to THINK about the logic of the loop in your head.

to calculate the Fibonacci numbers you are going to need SOMETHING that does the following 1 + 1 = 2, 1 + 2 = 3, 2 + 3 = 5.

So a basic pseudo-code for this might be:
CODE

//First we need to set things up...
old = 1
new = 1
while (new < 100) { //Our exit condition is when we get above 100
    temp = new; //Save the current number
    new += old; //calculate the next number (This is our increment step -- note that it changes the variable used in the condition).
    old = temp; //ditch the old-old number and save the last number
    output new; //current Fibonacci number
}


I warn you that that code will not work exactly as-is in C/C++ but it should give you an idea of how to change your program to work.
User is offlineProfile CardPM
+Quote Post

Jayman
RE: LOOP HELP
5 Dec, 2007 - 01:20 PM
Post #5

Student of Life
Group Icon

Joined: 26 Dec, 2005
Posts: 7,317



Thanked: 66 times
Dream Kudos: 500
Expert In: Everything

My Contributions
Moved to C/C++ forum.
User is offlineProfile CardPM
+Quote Post

james evans
RE: LOOP HELP
5 Dec, 2007 - 06:25 PM
Post #6

New D.I.C Head
*

Joined: 4 Dec, 2007
Posts: 5


My Contributions

THANKS FOR YOUR HELP!!!!!!!!!!!!!!!!!!!!!!!!!

QUOTE(NickDMax @ 5 Dec, 2007 - 10:51 AM) *

...all your told it to do was print zeros...

Generally loops have 4 parts: Setup (initialization), looped code, the increment, exit condition. Now these are just names I am making up but here is the idea behind each part:

setup: do whatever you need to set up the conditions for the loop
loop: some code that you want to repeat.
increment: CHANGE SOMETHING, preferably so that at some point your exit condition will happen.
exit condition: how do we get OUT of the loop? Note this must be linked to the "increment" step.

lets take a look at one of your loops:
QUOTE
CODE
while (Fibonaccinumbers > 0)
{
    cout << Fibonaccinumbers << endl;
    count = count + 1;
}  //end while
Now the problem here is that you have an increment but it is not linked to your condition... the only thing that changes is count. -- also note that you are trying to display Fibonacci numbers but the variable Fibonaccinumbers does not change at all... which is why you just point '0' over and over.

So... in order for your program to work you have to THINK about the logic of the loop in your head.

to calculate the Fibonacci numbers you are going to need SOMETHING that does the following 1 + 1 = 2, 1 + 2 = 3, 2 + 3 = 5.

So a basic pseudo-code for this might be:
CODE

//First we need to set things up...
old = 1
new = 1
while (new < 100) { //Our exit condition is when we get above 100
    temp = new; //Save the current number
    new += old; //calculate the next number (This is our increment step -- note that it changes the variable used in the condition).
    old = temp; //ditch the old-old number and save the last number
    output new; //current Fibonacci number
}


I warn you that that code will not work exactly as-is in C/C++ but it should give you an idea of how to change your program to work.


User is offlineProfile CardPM
+Quote Post

Closed TopicStart new topic
Time is now: 1/8/09 03:45PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month