QUESTION 1 (35 MARKS)
Write a function named "reduce" that takes two positive integer arguments, call them "num" and "denom", treats
them as the numerator and denominator of a fraction, and reduces the fraction. That is to say, each of the two
arguments will be modified by dividing it by the greatest common divisor of the two integers. The function should
return the value 0 (to indicate failure to reduce) if either of the two arguments is zero or negative, and should
return the value 1 otherwise.
Answer Code:
#include <iostream>
using namespace std;
long long aga(long long a, long long b)
{
if (a % b == 0)
return b;
if (b % a == 0)
return a;
if (a > b)
return aga(a % b, b);
else
return aga(a, b % a);
}
C2 General
, int reduce(long long& num, long long& denom)
{
long long agaFrac;
if ((agaFrac = aga(num, denom)) != 1)
{
num /= agaFrac;
denom /= agaFrac;
return 1;
}
else
{
return 0;
}
}
int main()
{
long long num, denom;
cout << "Please enter numerator: ";
cin >> num;
cout << "Please enter denominator: ";
cin >> denom;
if (reduce(num, denom))
{
cout << "The Reduced Fraction is " << num << "/" << denom;
}
else
{
cout << "The Fraction " << num << "/" << denom << " cannot be reduced";
}
}
Reduce Fraction Compiler screens
C2 General
, Cannot be Reduced Compiler screens
QUESTION 2 (30 MARKS)
A parking garage charges a R12.00 minimum fee to park for up to three hours. The garage charges an additional
R0.90 per hour for each hour or part thereof in excess of three hours. The maximum charge for any given 24-hour
period is R20.00. Assume that no car parks for longer than 24 hours at a time. Write a program that will calculate
and print the parking charges for each of 3 customers who parked their cars in this garage yesterday. You should
enter the hours parked for each customer. Your program should print the results in a neat tabular format and
should calculate and print the total of yesterday's receipts. The program should use the function calculateCharges
to determine the charge for each customer. Your outputs should appear in the following format:
C2 General
, Answer Code:
#include <iostream>
float calculateCharges( float hours );
float charge;
int main()
{
int customer;
float one;
float two;
float three = 0;
float hours;
for( customer = 1; customer <= 3; customer++ ) {
printf( "Please Enter customer %d parking hours: ", customer );
scanf( "%f", &hours );
if( customer == 1 )
one = hours;
else if( customer == 2 )
two = hours;
else
three = hours;
}
printf( "%s%10s%12s", "Car", "Hours", "Charge" );
printf( "\n%d%12.1f%12.1f", 1, one, calculateCharges( one ) );
printf( "\n%d%12.1f%12.1f", 2, two, calculateCharges( two ) );
printf( "\n%d%12.1f%12.1f", 3, three, calculateCharges( three ) );
printf( "\n%s%8.1f%12.1f", "TOTAL", one + two + three, calculateCharges( one ) + calculateCharges( two
) + calculateCharges( three ) );
}
float calculateCharges( float hours )
{
int h = hours;
charge = 12.0;
if( hours > 0 ) {
if( hours <= 3 )
return charge;
else if( hours <= 24 ) {
while( h > 3 ) {
charge += .90;
h--;
if( charge >= 20 )
charge = 20;
}
return charge;
}
}
C2 General