#include <iostream>
#include <fstream>
using namespace std;
int main()
{
char end_program;
//-------------------------- Functions -----------------------------//
int fibonacci(int number_par);
///----------------------- Main Program ----------------------------//
do
{
//------------ Step 1 - Generate Sequence & Write to File -----------//
ofstream out_stream;
ifstream in_stream;
out_stream.open("Fibonacci.dat");
// If File Opening Fails //
if (out_stream.fail())
{
cout<<"Output file Fibonacci opening failed.\n";
exit(1);
}
int number;
int Fib=0;
unsigned N;
cout<<"Please enter a positive integer.\n";
cin>>number;
do
{
if (cin.fail() || number <= 0)
{
cout<<"Invalid Input";
cin.clear();
cin.ignore(1000,'\n');
continue;
}
break;
}while (true);
N = 1;
cout<<"You entered: "<<number<<endl;
while (N<=number)
{
//*********** Account for Scripting Error ************//
//*********** Write in rows of 4 **************//
//Determine how many rows there will be
int row;
{
if(number%4==0)
row=number/4;
else
row=(number/4)+1;
}
//Write in rows
for (int b=1; b<=row;)
{
for(int n=1; n<=4;n++)
{
Fib = fibonacci(N);
out_stream<<Fib<<" ";
cout<<Fib<<" ";
N++;
if (N > number)
break;
}
out_stream<<endl;
cout<<endl;
b++;
}
//*********** Write file on screen **********//
}
//************** Count numbers *************//
out_stream.close();
cout<<"Would you like to quit? Press Q to quit, or any other key to continue"<<endl;
cin>>end_program;
} while (end_program!='q' && end_program!='Q');
return (0);
}
//------------------------ Function definitions ----------------------
int fibonacci(int number_par)
{
if(number_par<=1)
return (number_par);
else
return (fibonacci(number_par-1)+fibonacci(number_par-2));
}
Fibonacci Program
Page 1 of 11 Replies - 1623 Views - Last Post: 24 October 2008 - 12:24 AM
#1
Fibonacci Program
Posted 23 October 2008 - 10:33 PM
I'm having trouble with the first part of my fibonacci program. I was able to write a function to actually calculate the numbers, but we must write the numbers into a file (and on the screen) in rows of 4. The way I wrote it works, but I don't think is 'correct'. Also, we have to account for overflow, and I have no clue how to do that is this instance. We are not allowed to use arrays.
Replies To: Fibonacci Program
#2
Re: Fibonacci Program
Posted 24 October 2008 - 12:24 AM
I think you made it bit complex...
I consider that number is number of values from Fibonacci series. so if number is five, you print.
1 1 2 3
5
I hope this will help you.
for (int b=1; b<=number;b++)
{
Fib = fibonacci(B)/>;
out_stream<<Fib<<"\t";
cout<<Fib<<"\t";
}
out_stream<<endl;
cout<<endl;
}
I consider that number is number of values from Fibonacci series. so if number is five, you print.
1 1 2 3
5
I hope this will help you.
Page 1 of 1

New Topic/Question
Reply



MultiQuote


|