Question

In: Computer Science

C++ Funcion For this lab you need to write a program that will read in two...

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

Solutions

Expert Solution

// 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:


Related Solutions

For this lab, you will write a C++ program that will calculate the matrix inverse of...
For this lab, you will write a C++ program that will calculate the matrix inverse of a matrix no bigger than 10x10. I will guarantee that the matrix will be invertible and that you will not have a divide by 0 problem. For this program, you are required to use the modified Gaussian elimination algorithm. Your program should ask for the size (number of rows only) of a matrix. It will then read the matrix, calculate the inverse, and print...
For this lab, you will write a C++ program that will calculate the matrix inverse of...
For this lab, you will write a C++ program that will calculate the matrix inverse of a matrix no bigger than 10x10. I will guarantee that the matrix will be invertible and that you will not have a divide by 0 problem. For this program, you are required to use the modified Gaussian elimination algorithm. Your program should ask for the size (number of rows only) of a matrix. It will then read the matrix, calculate the inverse, and print...
For this week’s lab assignment, you will write a program called lab9.c. You will write a...
For this week’s lab assignment, you will write a program called lab9.c. You will write a program so that it contains two functions, one for each conversion. The program will work the same way and will produce the same exact output. The two prototypes should be the following: int btod(int size, char inputBin[size]); int dtob(int inputDec); The algorithm for the main() function should be the following: 1. Declare needed variables 2. Prompt user to enter a binary number 3. Use...
Write a C++ die roller program. We need you to write a program that will roll...
Write a C++ die roller program. We need you to write a program that will roll dice for them, but not just six-sided dice. Your program needs to be able to take an input in for form nDx or ndx where the d is the letter d or D. n may or not be present. If it is, it represents the number of dice. If not, assume it is a 1. x may or may not be present. If it...
Lab 1 Write a program in the C/C++ programming language to input and add two fractions...
Lab 1 Write a program in the C/C++ programming language to input and add two fractions each represented as a numerator and denominator. Do not use classes or structures. Print your result (which is also represented as a numerator/denominator) to standard out. If you get done early, try to simplify your result with the least common denominator. The following equation can be used to add fractions: a/b + c/d = (a*d + b*c)/(b*d) Example: 1/2 + 1/4 = ( 1(4)...
write a c++ program to read two matrices with any size. Your program should have at...
write a c++ program to read two matrices with any size. Your program should have at least the following functions Main() Read a Matrix Add two matrices Subtract two matrices multiply two matrices display a matrice
I need specific codes for this C program assignment. Thank you! C program question: Write a...
I need specific codes for this C program assignment. Thank you! C program question: Write a small C program connect.c that: 1. Initializes an array id of N elements with the value of the index of the array. 2. Reads from the keyboard or the command line a set of two integer numbers (p and q) until it encounters EOF or CTL - D 3. Given the two numbers, your program should connect them by going through the array and...
in c++ you need to write a program that maintains the gradebook for a specific course....
in c++ you need to write a program that maintains the gradebook for a specific course. For each student registered in this course, the program keeps track of the student’s name, identification number (id), four exam grades, and final grade. The program performs several functionalities such as: enrolling a student in the course, dropping a student from the course, uploading student’s exam grades, displaying the grades of a specific student, displaying the grades of all students in the course, and...
Coding in C++: For this verse, you need to write a program that asks the user...
Coding in C++: For this verse, you need to write a program that asks the user to input the inventory id number (integer) and the price (float) for 5 inventory items. Once the 5 inventory items are input from the user, output the results to the screen. Please ensure you use meaningful names for your variables. If your variables are not named meaningfully, points will be deducted.
Write a C or C++ program using the fork() system call function. You will need to...
Write a C or C++ program using the fork() system call function. You will need to create 3 processes – each process will perform a simple task. Firstly, create an integer "counter" initialized to a random value between 1 and 100. Print this number to the console. This can be done by: Including the stdio.h and stdlib.h libraries Using the rand() function to generate your randomly generated number The main thread consists of the parent process. Your job is to...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT