Hi you guys I hope you are having a great day so far. I have a question. I am supposed to write a program that starts with the number 1 and adds that number to the last number. My example shows me starting at 0 but not counting it, then the next number is 1 and then the next is one. those two numbers are added together and then the sum of those are added back to the last number. For example the numbers would be 0, 1, 1, then 1+1 is 2 then the 2 is added to the 1 and then I have 3 then the 3 and 2 are added together and then I get 5 and so on. I have started but the program does nothing. This is what I have so far.
int _tmain( )
{
int previous1; // variable for the first number
int previous2; // variable for the second number
int current; // varible to store the current number
int counter; //loop control varible
I'm trying to get the series to count from 0 to 25 starting at 1. The numbers I need are 0, 1, 1, 2, 3, 5, 8, 13, and so on. I thought the loop would continue to count for me. Am I not supposed to do this?
When the program runs its supposed to have the following:
The first 25 numbers in the Fibonacci series:
Fibonacci Series #1 = 0
Fibonacci Series #2 = 1
Fibonacci Series #3 = 1
Fibonacci Series #4 = 2
Fibonacci Series #5 = 3
and the program is supposed to keep counting the numbers added together until it gets to Fibonacci Series # 25 = 46368.
I am trying to loop it to get the program to keep running.
that for statement is invalid.The second thingy (can't think of the name) in a for statement is called every time the for code comes to an end (repeat if it returns true and don't repeat if it returns false). Since previous2 = 1 is your second thingy, previous2 will be set equal to 1 every time the for code is ran.
1 2 3 4 5 6 7
for( previous1 = 1; previous2 = 1; counter++ )
{ //even though it was previously set equal to 2, previous2 will be reset equal to 1 before the code below is ran
current = previous2 + previous1; //1+1
previous1 = previous2; //1
previous2 = current; //2
counter++;
}
Also, cin >> previous1 and cin >> previous have no use. Get rid of them. Third, counter isn't set equal to anything so that needs to be fixed(see above). Lastly, you might want to add a cout (see above). Try my suggestions and tell me if they work (I didn't test them out before replying)
#include "stdafx.h"
#include <iostream>
#include <string>
#include <iomanip>
#include <cmath>
usingnamespace std;
int _tmain( )
{
int previous;
int previous1; // variable for the first number
int previous2; // variable for the second number
int current; // varible to store the current number
int counter; //loop control varible
previous=0;
previous1 = 1;
previous2 = 1;
cout << "The first 25 numbers in the Fibonacci Series:" << endl;
cout << "Fibonacci Series #1 = " << previous << endl;
for (counter = 1;counter <= 25;counter++)
{
current = previous2 + previous1;
cout<<"Fibonacci series #2=" <<previous + previous1 << endl;
previous1 = previous2;
previous2 = current;
}
system ("pause");
return 0;
}
Second, cout<<"Fibonacci series #2=" <<previous + previous1 << endl;
It comes out with #2 because your telling it to. Just replace the 2 with <<counter + 1<< and then try it.
cout<<"Fibonacci series #"<<counter+1<<"=" <<previous + previous1 << endl;
Niven, I figured it out last night but, again I wanted to thank you for helping me. I hope someday I will be as good as you guys are. I guess some people just pick this stuff up but I am only on my 6th or 7th week. I have to tell you that I am taking networking too and I really don't get it. But anyway have a great day. By the way how long have you been at this?
I've been at it for some months now (maybe like 6 or 7) and am not really all too good. What I know I've learned purely from internet tutorials and googling specific topics so my education in programming isn't what it should've been. I just know the basics (anything I make that's even a little bit complicated is made through me trying to come up with creative solutions using what I know) of regular c++ and some, but even less, of opengl (a graphics api), but all that's not important. Anyway, good luck with programming and I hope you get better than me someday.