Software Design Using C++
Review of Functions and Parameters
Question 1
Write a reasonable test program for the C++ function shown below.
If you need to review how to use C++ functions first,
go to the Complex Functions section of these web pages.
/* Given: NumGrade A numeric grade (whole number).
Task: To find the corresponding letter grade.
Return: This letter grade in the function name.
*/
char LetterGrade(int NumGrade)
{
if (NumGrade > 100)
return 'I';
else if (NumGrade >= 90)
return 'A';
else if (NumGrade >= 80)
return 'B';
else if (NumGrade >= 70)
return 'C';
else if (NumGrade >= 60)
return 'D';
else if (NumGrade >= 0)
return 'F';
else
return 'I';
}
|
Click on NEXT to see hints and possible answers but
do try to answer the problem on your own first!
Question 2
The following program uses functions, but passes values into and out of the functions
in global variables. This is almost always a bad design. For one thing, it means that
these functions are not easily portable to a new programming project.
For another, it makes it possible for you or someone else on your programming team to accidentally
change the value of a global variable in one location in the program and thus mess up what value
it should have in a different section of the program. Global variables are dangerous!
Rewrite this program
so that it uses parameters and/or return values and eliminates all use of global variables.
If you need to review how to use C++ functions first,
go to the Complex Functions section of these web pages.
/* Filename: function.cpp
Author: Br. David Carlson
Date: December 21,2003
This is a test program that totals the cost for buying a list of items.
The price and number of units to buy for each item are hard-coded in this
test program. Tax is not added, but the program does give a discount of $5
if the cost for all units of any particular item is over $80. The price,
number of units, and costs for each item and the total cost are printed on
the screen.
*/
#include <iostream>
using namespace std;
// Function prototypes:
void CalculateCost(void);
void Results(void);
// Global variables (a bad design idea):
float Price, Cost, TotalCost;
int NumPurchased;
int main(void)
{
TotalCost = 0.0;
Price = 3.89;
NumPurchased = 2;
Results();
Price = 30.21;
NumPurchased = 3;
Results();
cout << "Total cost: " << TotalCost << endl;
return 0;
}
|
/* Given: Price Global variable for the unit price of an item to be purchased.
NumPurchased Global variable for the number of this item to be purchased.
TotalCost Global variable for the total cost of all items purchased thus far.
Task: To find the cost of purchasing NumPurchased units of this item and
to print this cost on the screen, together with Price and NumPurchased.
Also updates TotalCost to include the cost of purchasing these new items.
Return: TotalCost Global variable containing the updated total cost of all items
purchased thus far.
*/
void Results(void)
{
CalculateCost();
TotalCost = TotalCost + Cost;
cout << "Unit price: " << Price << " Number purchased: "
<< NumPurchased << " Cost: " << Cost << endl << endl;
}
|
/* Given: Price Global variable for the unit price of an item to be purchased.
NumPurchased Global variable for the number of this item to be purchased.
Task: To find the cost of purchasing NumPurchased items at unit price Price.
A discount of $5 is given if the cost of the NumPurchased items is over $80.
Return: Cost Global variable containing this cost.
*/
void CalculateCost(void)
{
Cost = Price * NumPurchased;
if (Cost > 80.0)
Cost = Cost - 5.0;
}
|
Click on NEXT to see possible answers. A new browser
window will be used to display the new page so that you can continue to refer to the above code
in the old browser window.
You can go back to the overall review listing: Review of Introductory Topics.
Back to the main page for Software Design Using C++
|