I need to display 1, 1, 2, 3, 5, 8, 13, 21, 34, 55 … I “must” use the “for” …This is what I have but it only counts by two. I don’t want the answer…however can you please give me some clues on the proper way to add the prior two numbers to get the needed sum?

#include <iostream>

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


//
 int main()
{ 
	int count = 0; 

	for ( int count = 1; count <= 55; count = count + 2)
			cout << count << endl;
	
    
	return 0;
}   //end of main function
Member Avatar for iamthwee

Draw your flow chart first...

Have 2 variables outside the for loop. These two variables will contain the first 2 values. Display them. Inside the loop add these two variables which will result in third variable. Display the third variable's value. Once added and displayed, move second variable value to first variable, and move third variable value to second variable. The for loop condition should check for the last value of the third variable.

Hope this clarifies your query!!!!!

I need to display 1, 1, 2, 3, 5, 8, 13, 21, 34, 55 ... I "must" use the "for" ... This is what I have but it only counts by two. I don’t want the answer ... however can you please give me some clues on the proper way to add the prior two numbers to get the needed sum?

#include <iostream>

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


//
 int main()
{ 
    int count = 0; 

    for ( int count = 1; count <= 55; count = count + 2)
            cout << count << endl;


    return 0;
}   //end of main function

end quote.

Take a look at http://en.wikipedia.org/wiki/Fibonacci_number

eg code :

first = 0;
second = 1;
cout << first;
cout << second;

This should come before the for loop.

inside the for loop :

third = first + second;
count << third;
first = second;
second = third;

the for loop should be

for(;third<=55;)


check with your code with these changes.....

Member Avatar for iamthwee

If you learnt how to draw a flow chart you could then use that to convert into code.

Flow charts help to visualise how problems such as these are solved.

I still have errors please help?

#include <iostream>

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



int main()

 
  
 
{ 
 int num1 = 0;
 int num2 = 1;
   
	 cout << num1 << endl;
     cout << num2 << endl;

	for(int num = num1 + num2;)
	 cout << num << endl; 
	        num1 = num2;
	        num2 = num;
         
	   
	
    
	return 0;
}   //end of main function

some one please help

what are the errors. maybe you need to add braces { and } around lines 21, 22 and 23 to make them all part of the loop.

#include <iostream> using std::cout;using std::endl;   int main()     {  int num1 = 0; int num2 = 1;   	 cout << num1 << endl;     cout << num2 << endl; 	for(int num = num1 + num2;)	 cout << num << endl; 	        num1 = num2;	        num2 = num;         	   	    	return 0;}   //end of main function#include <iostream>

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



int main()

 
  
 
{ 
 int num1 = 0;
 int num2 = 1;
   
	 cout << num1 << endl;
     cout << num2 << endl;

	for(int num = num1 + num2;)
	 cout << num << endl; 
	        num1 = num2;
	        num2 = num;
         
	   
	
    
	return 0;
}   //end of main function
Member Avatar for iamthwee

It's almost impossible to help you any further without giving you the actual answer.

Have you drawn a flow chart yet like I said.

Yes I did...I am trying ...I been up all night. I im in oline college 2nd year so its difficult to get hel from the professor, Any way I figured it out this way, but I must use the for statement. Any clues?

#include <iostream>

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



int main()

 
  
 
{ 
 int num1 = 0;
 int num2 = 1;
   
	 cout << num1 << endl;
     cout << num2 << endl;

	for(int num = num1 + num2;)
	 cout << num << endl; 
	        num1 = num2;
	        num2 = num;
         
	   
	
    
	return 0;
}   //end of main function
Member Avatar for iamthwee

Yes I did

If you have a flow chart you can add it as an attachment for me to look at. (It doesn't have to be neat, just readable)

Try using the number of values in the sequence to drive the loop. Say you want the first 10 fibonacci numbers.

unsigned long a = <a's initial value>;
unsigned long b = <b's initial value>;

for ( int i = 0; i < 10; ++i ) {
  cout<< NextInSequence( a, b ) <<"\n";
}

shannonpaul:

Control loops come in three varieties: for, while, and do/while. Each loop has one or more keywords (telling what type of loop it is), a body (usuallly telling what to do each time through the loop), and a conditional (indicating when or how often or how many times the body should be done). These loops are used when you want chunks of your program to be repeated a specified number of times.

In order to be syntactically correct a for loop conditional is enclosed in parethesis immediately after the keyword for. The conditional of a for loop needs to know where to start, when to stop and how to change a given variable, in that order; though one, two or all three items may be empty. In additon there should be a semicolon between the first and second and between the second and third item of the conditional. That means if each part of the conditional is empty then there should still be 2 semicolons in the parenthesis.

The body of the for loop occurs after the closing parenthesis of the conditional. If the body of the for loop isn't contained in curly brackets, then only the first statement after the conditional is considered to be the body.

In the code as posted I only notice a single item and no semicolons in the parentheses after the keyword for. Thus, this is an error on several accounts.

for(int num = num1 + num2);

what you are doing here is creating an infinite cycle, that actually does nothing (i bet it doesn't even work)... what you want to do is:

for (int i=0;i<54;i++){
   /*procedure*/;
}

remember to open a for loop with curly braces '{' and to close it with '}'... if you write a for loop with ';' it will show you an error...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.