120k views
5 votes
Consider the example below that uses a function to compute the amount that an online auction/sales website charges a customer who sells an item online.

#include
using namespace std;
/* Returns fee charged by ebay.com given the selling
price of fixed-price books, movies, music, or video-games.
Fee is $0.50 to list plus a % of the selling price:
13% for $50.00 or less
5% for $50.01 to $1000.00
2% for $1000.01 or more
Source: http://pages.ebay.com/help/sell/fees.html, 2012.
Note: double variables often are not used for dollars/cents,
but here the dollar fraction may extend past two decimal places.
*/
// Function determines eBay price given item selling price
double EbayFee(double sellPrice) {
const double BASE_LIST_FEE = 0.50; // Listing Fee
const double PERC_50_OR_LESS = 0.13; // % $50 or less
const double PERC_50_TO_1000 = 0.05; // % $50.01..$1000.00
const double PERC_1000_OR_MORE = 0.02; // % $1000.01 or more
double feeTotal; // Resulting eBay fee
feeTotal = BASE_LIST_FEE;
// Determine additional fee based on selling price
if (sellPrice <= 50.00) { // $50.00 or lower
feeTotal = feeTotal + (sellPrice * PERC_50_OR_LESS);
}
else if (sellPrice <= 1000.00) { // $50.01..$1000.00
feeTotal = feeTotal + (50 * PERC_50_OR_LESS )
+ ((sellPrice - 50) * PERC_50_TO_1000);
}
else { // $1000.01 and higher
feeTotal = feeTotal + (50 * PERC_50_OR_LESS)
+ ((1000 - 50) * PERC_50_TO_1000)
+ ((sellPrice - 1000) * PERC_1000_OR_MORE);
}
return feeTotal;
}
int main() {
double sellingPrice; // User defined selling price
cout << "Enter item selling price (Ex: 65.00): ";
cin >> sellingPrice;
cout << "eBay fee: $" << EbayFee(sellingPrice) << endl;
return 0;
}
1) Analyzing the eBay fee calculator.
a) For any call to EbayFee() function, how many assignment statements for the variable feeTotal will execute?
b) What does EbayFee() function return if its argument is 0.0 (show your answer in the form #.##)?
c) What does EbayFee() function return if its argument is 100.00 (show your answer in the form #.##)?
######################
// Function prompts user to enter positive non-zero number
int GetPositiveNumber() {
int userNum;
userNum = 0;
while (userNum <= 0) {
cout << "Enter a positive number (>0): " << endl;
cin >> userNum;
if (userNum <= 0) {
cout << "Invalid number." << endl;
}
}
return userNum;
}
// Function returns greatest common divisor of two inputs
int FindGCD(int aVal, int bVal) {
int numA;
int numB;
numA = aVal;
numB = bVal;
while (numA != numB) { // Euclid's algorithm
if (numB > numA) {
numB = numB - numA;
}
else {
numA = numA - numB;
}
}
return numA;
}
// Function returns least common multiple of two inputs
int FindLCM(int aVal, int bVal) {
int lcmVal;
lcmVal = abs(aVal * bVal) / FindGCD(aVal, bVal);
return lcmVal;
}
int main() {
int usrNumA;
int usrNumB;
int lcmResult;
cout << "Enter value for first input" << endl;
usrNumA = GetPositiveNumber();
cout << endl << "Enter value for second input" << endl;
usrNumB = GetPositiveNumber();
lcmResult = FindLCM(usrNumA, usrNumB);
cout << endl << "Least common multiple of " << usrNumA
<< " and " << usrNumB << " is " << lcmResult << endl;
return 0;
}
2)
a) Other than main(), which user-defined function calls another user-defined function? Just write the function name.
b)How many user-defined function calls exist in the program code?
3) Stack Frames
a) (T/F) After a function returns, its local variables keep their values, which serve as their initial values the next time the function is called.
b) (T/F) A return address indicates the value returned by the function.

User Nimbudew
by
7.6k points

1 Answer

6 votes

a) The function EbayFee() will execute either 2, 3 or 4 assignment statements for the variable feeTotal, depending on the value of its argument.

b) If the argument of EbayFee() function is 0.0, the function will return 0.50, which is the base listing fee.

c) If the argument of EbayFee() function is 100.00, the function will return 5.50, which is the total fee for selling an item at that price.

How many assignments in EbayFee()?

a) Only one assignment statement for the variable feeTotal will execute.

b) If the argument is 0.0, EbayFee() function will return 0.50.

c) If the argument is 100.00, EbayFee() function will return 5.50.

a) FindLCM() calls FindGCD().

b) There are three user-defined function calls in the program code: GetPositiveNumber(), FindGCD(), and FindLCM().

a) False. After a function returns, its local variables lose their values and are destroyed. The next time the function is called, the local variables will be initialized with their default values.

b) False. A return address is the memory address where the program should return after finishing the execution of the function. It does not indicate the value returned by the function, which is typically stored in a register or on the stack.

User Thomas Danecker
by
8.3k points