| CMPSC 101 - Functions Worksheet #2 | Name - |
1. Draw a console window and show the expected output as precisely as possible.
1.
int add(int num1, int num2);
int square(int num);
int main()
{
int num1 = 10;
int num2 = 15;
int result = 0;
result = add(square(num1), num2);
cout << result << endl;
return 0;
}// end of main
int add(int a, int b)
{
int sum = a + b;
return sum;
}// end of add
int square(int num)
{
return num * num;
}// end of square
2.
double computeDiscount(int num, double price, double rate);
double computeTax(double base);
int main()
{
double pricePerWidget = 19.99;
int numWidgets = 4;
const double CLEARANCE_DISCOUNT = 0.2;
double basePrice = 0.0;
double finalPrice = 0.0;
double discount = 0.0;
basePrice = numWidgets * pricePerWidget;
discount = computeDiscount(numWidgets, pricePerWidget, CLEARANCE_DISCOUNT);
finalPrice = basePrice - discount;
finalPrice = finalPrice + computeTax(finalPrice);
cout << finalPrice << endl;
return 0;
}// end of main
double computeDiscount(int num, double price, double rate)
{
double base = num * price;
return rate * base;
}// end of computeDiscount
double computeTax(double base)
{
return base * 0.06;
}// end of computeTax