For my computer programming course I need to write a program that asks the user how many numbers to display and then displays that many Fibonacci numbers. The program should validate the input.
Examples:
How many Fibonacci numbers do you want to display? 5
0,1,1,2,3
How many Fibonacci numbers do you want to display? 0
The program cannot display 0 Fibonacci numbers.
Please help me get started. We have just started learning about loops and files. So I think it should use loops, but I do not know how?
OK, I know it's not a good idea to give complete solutions, but just this once! I am fickle and I dont like complex looking main's, but I also realize that you may not have done classes yet, so if this solution is overkill then forgive me, and just keep it till later (or submit it anyway for possible extra brownie points). Here is the main().
#include<iostream>
#include"Fibo.h"
usingnamespace std;
int main(int argc, char* argv[])
{
int numFibs = 0;
Fibo fibGen;
// Ask user for number of Fibs reqd.
cout << "How many Fibonacci numbers do you want to display? ";
cin >> numFibs;
if (numFibs < 1)
{
// Trying to compute 0 or less than 0 Fibs has NO meaning.
cout << "The program cannot display 0 Fibonacci numbers." << endl;
}
else
{
// OK - let's do it!
// Unless you are a mathematician doing Number Theory research the need for more than 70
// Fibs is a mute point, the idea is to show that you can infact generate Fibo numbers!
if(numFibs > 70) numFibs = 70;
for(int i=0; i<numFibs; ++i)
{
cout << fibGen.next();
if (i<numFibs-1) cout << ",";
}
}
}
Here is the header file:
1 2 3 4 5 6 7 8 9 10 11 12
#pragma once
class Fibo
{
public:
Fibo(void);
~Fibo(void);
unsignedlonglong next(void);
private:
unsignedlonglong a[4];
};
And, last but not least the member functions for the class
Like I said - probably overkill, but it gives you a nice looking main() :-p
I also noted that you you dont have a requirement for an upper limit for the number of Fibs, so I set a ceiling of 70. Requests for more than 70 are just set to 70, a comment in the code says why. Enjoy!
//Exercise 1 PS6
//Dylan Metz
#include <iostream.h>
int main()
{
int num;
int count=1;
int fib1=0;
int fib2=1;
cout << "How many Fibonacci #'s do you want to display? ";
cin>>num;
while (num<1 || num>70)
{
cout << "Please enter a number in the range of 1-70.";
cin>>num;
}
while (num>=1 && num<=70)
{
if (num==1)
{
cout<<fib1;
}
elseif (num==2)
{
cout<<fib2;
}
else
{
cout<<fib1+fib2;
}
count++;
}
return 0;
}
This is what the program needs to do:
user input:
How many Fibonacci #'s do you want to display?
saurabh qupta, what you posted was almost exactly what I needed. Thank-you!
I am curious if i can get the same result using #include <iostream>? The course i'm taking is an introduction to C++ programing and the only #include we have went over are iostream.h, math.h, and iomanip.h.
I need to use:
cout
cin
and a loop.
But thank-you very much. I really appercaite your advice.
Also if it helps the book we are using is Starting out with C++: from control structures through objects. By Tony Gaddis.
//Exercise 1 PS6
//Dylan Metz
#include <iostream.h>
int main()
{
int num;
int count=1;
int fib1=0;
int fib2=1;
int fib0;
cout << "How many Fibonacci #'s do you want to display? ";
cin>>num;
while (num<1 || num>70)
{
cout << "Please enter a number in the range of 1-70.";
cin>>num;
}
while (count<=num)
{
if (count==1)
{
cout<<fib1;
}
elseif (count==2)
{
cout<<fib2;
}
else
{
fib0=fib1+fib2;
cout<<fib0;
fib1=fib2;
fib2=fib0;
}
count++;
}
return 0;
}
Also how can i save the output to a text document?
//Exercise 1 PS6
//Dylan Metz
#include <iostream.h>
#include <fstream.h>
#include <string.h>
int main()
{
int num;
int count=1;
int fib1=0;
int fib2=1;
int fib0;
int tem;
char comma=',';
cout << "How many Fibonacci #'s do you want to display? ";
cin>>num;
while (num<1 || num>70)
{
cout << "Please enter a number in the range of 1-70.";
cin>>num;
}
while (count<=num)
{
if (count==1)
{
cout<<fib1;
}
elseif (count==2)
{
cout<<comma<<fib2;
}
else
{
fib0=fib1+fib2;
cout<<comma<<fib0;
fib1=fib2;
fib2=fib0;
}
count++;
}
#include <iostream>
usingnamespace std;
int main()
{
int num;
cout<<"How many Fibonacci #'s do you want to display?";
cin>>num;
if (num>=1 && num <= 70)
{
int a = 0;
int b = 1;
int sum;
for (int i=0; i<num; i++)
{
cout<<a<<", ";
sum=a+b;
a=b;
b=sum;
}
}
else
cout<<"Please enter a number in the range of 1-70.";
return 0;
}
EDIT: If you want to write this output to a file, you will have to do this:
(I also realized that there was a problem with the comma being printed 1 more time than it should be at the end and fixed it in this code)
#include <iostream>
#include <fstream>
usingnamespace std;
int main()
{
int num;
ofstream myfile;
cout<<"How many Fibonacci #'s do you want to display?";
cin>>num;
if (num>=1 && num <= 70)
{
int a = 0;
int b = 1;
int sum;
myfile.open ("output.txt");
for (int i=0; i<num; i++)
{
cout<<a;
myfile<<a;
if(i<num-1)
{
cout<<", ";
myfile<<", ";
}
sum=a+b;
a=b;
b=sum;
}
}
else
cout<<"Please enter a number in the range of 1-70.";
myfile.close();
return 0;
}