Exercise 1 (15 points) Let a and b be integers so that either a and b is nonzero
ID: 675483 • Letter: E
Question
Exercise 1 (15 points)
Let a and b be integers so that either a and b is nonzero. The greatest common divisor, written gcd(a,b), of a and b is the largest positive integer that divides both a and b. Your program must contain a function that takes as input two integers and return the gcd of integers.
Grading scheme: a main function to call gcd function and display the final outcome - 5 points; a function to calculate gcd - 5 points; handling the condition when the number is zero correctly - 5 points.
Exercise 2 (15 points) Write a program that outputs inflation rates for two successive years and whether the inflation is increasing or decreasing. Ask the user to input the current price of an item and its price one year and two years ago. To calculate the inflation rate for a year, subtract the price of the item for that year from the price of the item one year ago and then divide the result by the price a year ago. Your program must contain at least the following functions: a function to get the input, a function to calculate the results, and a function to output the results. Use appropriate parameters to pass the information in and out of the function. Do not use any global variables.
Grading scheme: a function to get the input - 5 points; a function to calculate the results - 5 points; a function to display the output and a main function to call the three functions above correctly - 5 points.
Exercise 3 (20 points) Write a program that prints the day number of the year, given the date in the form month-day-year. For example, if the input is 1-1-2006, the day number is 1; if the input is 12-25-2006, the day number is 359. The program should check for a leap year. A year is a leap year if it is divisible by 4, but not divisible by 100. For example, 1992 and 2008 are divisible by 4, but not by 100. A year that is divisible by 100 is a leap year if it is also divisible by 400. For example, 1600 and 2000 are divisible by 400. However, 1800 is not a leap year because 1800 is not divisible by 400.
Grading scheme: a function to check for leap year - 5 points; a function to calculate number of days in month - 5 points a function to calculate the day number -5 points a main function to call the three functions above correctly - 5 points
Explanation / Answer
Exercise:2
#include <iostream>
using namespace std;
void getinp(double* pc, double* p1, double* p2);
void inflationRate(double pc, double p1, double p2, double* rate1, double* rate2);
void showresult(double rate1, double rate2);
#include <iostream>
using namespace std;
void getinp(double* pc, double* p1, double* p2);
void inflationRate(double pc, double p1, double p2, double* rate1, double* rate2);
void showresult(double rate1, double rate2);
int main ()
{
double pc = 0, p1 = 0, p2 = 0, ra1 = 0, ra2 = 0;
getinp(&pc, &p1, &p2);
inflationRate(pc, p1, p2, &ra1, &ra2);
showresult(ra1, ra2);
cin.clear();
cin.ignore(255, ' ');
cin.get();
return 0;
}
void getinp( double* pc, double* p1, double* p2)
{
cout << " please enter the current price" <<endl;
cin >> *pc;
cout << " please enter the the price a year ago" <<endl;
cin >> *p1;
cout << " please enter the price two years ago" <<endl;
cin >> *p2;
}
void inflationRate(double pc, double p1, double p2, double* rate1, double* rate2)
{
*rate1 = ( p1-p2)/p2;
*rate2 = ( pc-p1)/p1;
}
void showresult(double rate1, double rate2)
{
if (rate1 < rate2)
cout << "The inflation rate is increasing" << endl;
else if (rate1 > rate2)
cout << "The inflation rate is decreasing" << endl;
else
cout << "There is no change in the inflation rate" << endl;
cout << "The rate for last year:" << rate1 << endl;
cout << "The rate for this year:" << rate2 << endl;
}
ex 1:
count = input ("NUMBERS YOU WANT TO CALCULATE FOR GCD? ")
for i in range(0, count):
number = input("ENTER THE NUMBER : ")
numbers.append(number)
numbers_sorted = sorted(numbers)
print 'NUMBERS SORTED IN INCREASING ORDER ',numbers_sorted
gcd = numbers_sorted[0]
for i in range(1, count):
divisor = gcd
dividend = numbers_sorted[i]
remainder = dividend % divisor
if remainder == 0 :
gcd = divisor
else :
while not remainder == 0 :
dividend_one = divisor
divisor_one = remainder
remainder = dividend_one % divisor_one
gcd = divisor_one
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.