Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

PART A. Write an interactive computer program that will find the greatest common

ID: 3543703 • Letter: P

Question


PART A.


Write an interactive computer program that will find the greatest common

divisor of two integers using Euclid's Algorithm. The program should be

independent of the order in which the two numbers are input.


EUCLID'S ALGORITHM


Divide the smaller number into the larger. If the remainder is not zero, replace

the original two numbers by the remainder and the smaller of the two numbers,

and repeat the division. Eventually the remainder will be zero, in which case

the smaller number is the greatest common divisor.


33 121


The following are the pseudo codes for the above algorithm.


Euclids Algorithm


Start

Allocate storage for variables (A, B, Remainder)

Output Enter two values // do not have to check the order of these two values

Input A, B

IF ((A != 0) and (B != 0)) // make sure you are not dividing anything by 0

Remainder = A % B

while (Remainder != 0)

A = B

B = Remainder

Remainder = A % B

end while loop // end the while loop

Output The greatest common divisior is

Output B


end If // end the if block

Stop cpsc1103 week 5 lecture notes page 2


33 121

121 33


Use the above pairs of values and show how many times the loop is executed for

each pair.



PART B.


Write a program to find the largest, smallest, and average values in a collection

of n numbers where the value of n will be the first data item read. Input the

values from file and output the results to a file.


sample input:

9 use this number for your loop

32

54

126

-6

55

69

231

1234

35


pseudo code

start

allocate storage for n, numberOfValues, smallest, largest, number

set up input and output streams ins, outs

open files

total = 0

input n from file

if (n>0) // n has to be greater than 0

numberOfValues = n

input number from file

total = total + number

largest = number

smallest = number

n = n-1

while (n > 0)

input number

total = total + number

if (number > largest)

largest = number

if (number < smallest)

smallest = number

n = n-1

end while

output largest

output smallest

output average is

output total/numberOfValues

end if

close files

stop



Explanation / Answer

// PROGRAM 1

#include<iostream>
using namespace std;
int main()
{
int A,B,Remainder;
cout <<"Enter value of A and B :";
cin >> A >> B;
cout << endl;
if(A!=0 && B!=0)// make sure you are not dividing anything by 0
{
Remainder = A % B;
while(Remainder != 0)
{
A = B;
B = Remainder;
Remainder = A % B;
}// end the while loop
cout << "The greatest common divisior is " << B << endl;
}// end the if block
return 0;
}

// PROGRAM 2

// PROGRAM 2

#include<iostream>
#include<fstream>
using namespace std;
int main()
{
int n,numberOfValues,smallest,largest,number;
ifstream infile("input.txt");
ofstream outfile("output.txt");
int total = 0;
infile >> n;
if (n>0) // n has to be greater than 0
{
numberOfValues = n;
infile >> number;
total = total + number;
largest = number;
smallest = number;
n = n-1;
while (n > 0)
{
infile >> number;
total = total + number;
if (number > largest)
largest = number;
if (number < smallest)
smallest = number;
n = n-1;
} //end while
cout << "largest is "<< largest << endl;
cout << "smallest is "<< smallest << endl;
double average = total/numberOfValues;
cout << "average is "<< average << endl;
outfile << "largest is "<< largest << endl;
outfile << "smallest is "<< smallest << endl;
average = total/numberOfValues;
outfile << "average is "<< average << endl;
} // end if
infile.close();
outfile.close();
return 0;
}