ECE 270 Assignment 4 TERM: W 09 Assigned: 2/11/09 Due: 2/20/09 In this assignmen
ID: 3609113 • Letter: E
Question
ECE 270 Assignment 4 TERM: W 09Assigned: 2/11/09
Due: 2/20/09
In this assignment, you will use open/create/read/write files fordata input and data output,
and write and call functions.
Function 1: GCD. This function will return the greatest commondevisor (GCD) of two
integers. In mathematics, GCD is the largest positive integer thatdivides both numbers
without remainder. A commonly used algorithm for GCD is Euclideanalgorithm:
Given two natural numbers a and b: check if b is zero; if yes, a isthe gcd. If not, repeat
the process using, respectively, b, and the remainder afterdividing a by b. The remainder
after dividing a by b is usually written as a mod b.
The implementation of this algorithm can be recursively:
gcd(a, b) = a, if b = 0;
gcd(a, b) = gcd(b, a % b), otherwise
There are a number of ways to implement them in C/C++. In recursiveapproach, the
function can be
function gcd(a, b)
if b = 0 return a
else return gcd(b, a mod b)
We can code the above as
int gcd(int a, int b)
{
return ( b == 0 ? a : gcd(b, a % b) );
}
In iterative approach, it can be
function gcd(a, b)
while b 0
t := b
b := a mod b
a := t
return a
The original algorithm is expressed as
function gcd(a, b)
if a = 0 return b
while b 0
if a > b
a := a b
else
b := b a
return a
(Try this site for testing your answer:http://gcd.awardspace.com/index.php
Also, see Prob. 6 on Page 378 (textbook): finding the greatestcommon divisor)
Function 2: Fibonacci number. In mathematics, the Fibonacci numbersare a sequence
of numbers named after Leonardo of Pisa, known as Fibonacci. Thefirst number of the
sequence is 0, the second number is 1, and each subsequent numberis equal to the sum of
the previous two numbers of the sequence itself, yielding thesequence 0, 1, 1, 2, 3, 5, 8,
etc. In mathematical terms, it is defined by the followingrecurrence relation:
In Function 2, let’s call this function fib(int n). The inputto the function is variable n,
and the function returns an integer. For examples, given n as 0,fib(0) returns 0; and
given n as 4, fib(4) returns 3.
(Also see Exercise 6.6 – Prob. 1 on Page 355 (textbook):Fibonacci sequence)
In main( ) function, you need to do the following:
1. open a text file, “a4.txt”. (on VLT, use Save targetwith suffix .txt)
2. read two numbers at a time, and output (cout) these two numbersand the GCD of
them, in one single line, with numbers separated by one space
3. continue to read from a4.txt until the end of the file.
4. create a file called “a4-2.txt”
5. produce a sequence of 20 Fibonacci numbers and output thesenumbers on screen
and write these numbers into “a4-2.txt”
Note: In step 2, main ( ) function needs to call function 1 (gcd).In step 5, main( )
function needs to call function 2 (fib).
Example of a4.txt
43 5
56 4
15 5
90 30
8 2
The output for this example would be:
43 5 1
56 4 4
Example of a4-2.txt should look like this, if eight numbers in Fibsequence,
0 1 1 2 3 5 8 13 21
Notice that your program is to produce 20 Fib numbers
using namespace std;
using std::cout;
using std::cin;
using std::endl;
//int factorial (int num);
unsigned long fibonacci( unsigned long );
int GCD(int a, int b);
int main ()
{
int n=5,i=0;
unsigned long k=3;
//cout<<"factorial for given data :"<<factorial(n)<<endl<<endl;
cout<<"Fibonacci :"<<fibonacci(k)<<endl ;
cout<<"GCD = "<<GCD(n,k)<<endl;
ifstream in("a4.txt");
ofstream out("a4-2.doc");
while(in.peek()!=EOF){
in>>n;
in>>k;
cout<<n<<" "<<k<<" "<<"GCD("<<n<<","<<k<<")="<<GCD(n,k)<<endl;
}
while (i<=20){
out<<fibonacci(i)<<" ";
i=i+1;
}
system ("pause");
return 0;
}
Explanation / Answer
please rate - thanks #include #include int fib(int); int gcd(int a, int b); int main() { int first,second,next,i,fibn; int num1,num2; int n=20; ifstream in; ofstream output; in.open("a4.txt"); //open file if(in.fail()) //is it ok? { coutRelated Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.