In: Computer Science
C++ Funcion
For this lab you need to write a program that will read in two values from
a user and output the greatest common divisor (using Euclidean algorithm)
to a file. You will also need to demonstrate
using ostream and ostringstream by creating 2 functions to output your print heading: one that uses ostream and the other uses ostringstream.
Using ostream and ostringstream
Write two PrintHeader functions that will allow you to output to the screen and to an output file.The first should use ostream and should be called twice in main to output your print heading to the screen and to a file.The second one will use ostringstream and return a string – call it two times to output to the screen and to a file.
Greatest Common Divisor
In mathematics, the greatest common divisor (GCD) of two or more integers(when at least one of them is not zero)is the largest positive integer that
divides the numbers without a remainder.If one of them is zero then the larger value is the GCD.
Euclidean Algorithm
The Euclidean algorithm works by using successive long divisions swapping out the lowest value with the remainder and the largest value with the previous smallest value until the remainder is 0. The way it works is that you find the remainder of the larger number divided by the smaller number. If the remainder is not 0 then the larger number gets the smaller number, the smaller number gets the remainder and we divide again. The process continues until the remainder is 0.
For example:
Let’s say we want to find the GCD of
74 & 32.
We would first divide
74 and 32.
74 / 32 = 2 r 10
Next we take the smaller number (32) and divide it by the remainder (10).
32 / 10 = 3 r 2
Again we take the smaller number (10) and divide it by the new remainder(2).
We repeat this process until the remainder is 0.
10 / 2 = 5 r 0
Once the remainder is 0 we stop and our GCD is the last non-zero remainder, which in this case is 2.
For the GCD write a function to read in the two values, a function to calculate the GCD, and a function to output the results.
Have the code run 4 times.
Test with the following inputs:
74, 32
99, 30
48, 18
12, 0
Screen INPUT/OUTPUT-should be formatted as follows –
(Class heading should display 2xs)
Enter the first integer: 72
Enter the second integer: 32
Enter the first integer: 99
Enter the second integer: 30
...
Thank you for using my GCD
calculator!
---------------------------------------------------------------------------------------
OUTPUT File format -(Class heading should display 2xs)
The GCD for 72 & 32 = 8
The GCD for 99 & 30 = 3
1.Screen I/O
2.Output File
3.Header File
4.int Main -
documented according to the requirements
& printed from eclipse
5.
Functions (in order in which they are called)
-
documented according to the requirements
& printed from eclipse
// C++ program to calculate the gcd of 2 numbers using euclidean algorithm
#include <iostream>
#include <fstream>
#include <sstream>
using namespace std;
// function declaration
void printHeader(ostream &out, string str);
string printHeader(int n1, int n2, int gcd);
int main()
{
ofstream fout("gcd.txt"); // create and open an output file
int n1, n2, r;
// print the heading on console and file
printHeader(cout,"GCD of 2 numbers");
printHeader(fout,"GCD of 2 numbers");
// loop 4 times
for(int i=0;i<4;i++)
{
// input of 2 numbers
cout<<"Enter the first integer: ";
cin>>n1;
cout<<"Enter the second integer: ";
cin>>n2;
int a,b;
// get the larger and smaller number
if(n1 > n2)
{
a = n1;
b = n2;
}else
{
a = n2;
b = n1;
}
if(a == 0 && b == 0) // check if both are 0
{
r = 0;
}else
{
// check if one of them is zero
if(a == 0)
r = b;
else if(b == 0)
r = a;
else // both are non-zero
{
r = a%b;
// loop to calculate the gcd of a and b
while(r > 0)
{
a = b;
b = r;
r = a%b;
}
r = b;
}
}
// print the result
printHeader(cout,printHeader(n1,n2,r));
printHeader(fout,printHeader(n1,n2,r));
}
fout.close(); //close the file
return 0;
}
// function to print the string to output stream
void printHeader(ostream &out, string str)
{
out<<str<<endl;
}
// function to take input of two numbers and the resultant gcd
// return a string containing the numbers and the gcd
string printHeader(int n1, int n2, int gcd)
{
ostringstream ss;
ss<<"The GCD for "<<n1<<" & "<<n2<<" = "<<gcd;
return ss.str();
}
//end of program
Output:
Console:
Output file: