MCC - Marquez - CIS162AB - C++ Level I
P06_ex An Example of call-by-reference![]()
The purpose of this C++ programming example is to familiarize students with designing and developing call-by-reference functions as well as using pointers as parameters. Pointers are special variables used to store memory addresses. The language C implements call-by-reference using pointers. Both pointers and call-by-reference parameters pass the memory address of the argument.
In addition, this program is used to demonstrate how structure charts depict modular programs. The affiliated structure chart for P06-ex and a flowchart of a single module is also included.Sample Output for P06_ex: P06_ex - Juan Marquez TR 1:00pm Enter a value between $5 and $15.00 for the price: 12.50 Enter a value between 1 and 50 for the quantity: 30 Price: 12.50 Quantity: 30 Subtotal: 367.50 Sales Tax: 18.38 at 0.05 Shipping: 10.00 Total Due: 395.88 Net Price: 13.20 Thank you! Press any key to continue Source Code: //P06_ex Call-by-reference Functions - Juan Marquez TR 1:00pm /* This program is used by customers to determine what the cost of their order would be based on the price and quantity ordered. The input values are validated. The cost must be between $5 and $15.00 inclusive. The quantity must be between 1 and 50 inclusive. If more than 25 items are ordered, the customer receives the discount. */ #include <iostream> // cin, cout using namespace std; // Function Prototypes void inputPrice(double& price); //call-by-reference void inputQuantity(int& quantity); //call-by-reference void calcCost(double price, int quantity, double& subtotal); //mixture void calcTax(double subtotal, double taxRate, double *taxAmount); //pointer //Declare global constants const double DISCOUNT_RATE = 0.98; void main() { //Declare local constants const double SHIPPING = 10.00, TAX_RATE = 0.05; //declare local variables int quantity; double price, subtotal, salesTax, total, netPrice; // Set the decimal point to 2 positions cout.setf(ios::fixed); cout.setf(ios::showpoint); cout.precision(2); cout << "P06_ex - Juan Marquez TR 1:00pm \n\n"; //Get the input inputPrice(price); //call-by-reference inputQuantity(quantity); //call-by-reference //Process the order. //After the two functions, we have a valid value for price and quantity. //So we can now perform the calculations. calcCost(price, quantity, subtotal); //mixture calcTax(subtotal, TAX_RATE, &salesTax); //Use & to send the address //of salesTax to taxAmount. total = subtotal + salesTax + SHIPPING; netPrice = total / quantity; //Display the results cout << endl << "Price: \t" << price << endl << "Quantity: \t" << quantity << endl << "Subtotal: \t" << subtotal << endl << "Sales Tax:\t" << salesTax << " at " << TAX_RATE << endl << "Shipping: \t" << SHIPPING << endl << "Total Due:\t" << total << endl << "Net Price:\t" << netPrice << endl << endl << "\nThank you!\n\n"; return; } // end of main //Function Definitions //Declare local variables in functions where needed. void inputPrice(double& price) { do { cout << "Enter a value between $5 and $15.00 for the price: "; cin >> price; } while (price < 5 || price > 15); cout << endl; return; } void inputQuantity(int& quantity) { quantity = 0; while (quantity < 1 || quantity > 50) { cout << "Enter a value between 1 and 50 for the quantity: "; cin >> quantity; } return; } void calcCost(double price, int quantity, double& subtotal) { //DISCOUNT_RATE is a global variable if (quantity > 25) subtotal = quantity * (price * DISCOUNT_RATE); else subtotal = quantity * price; return; } //Use * to declare pointer. //With pointers we also use the * to reference the value stored at // the location the pointer points to. void calcTax(double subtotal, double taxRate, double *taxAmount) { *taxAmount = subtotal * taxRate; return; } //end of program Structure Chart for P06_ex below:Flowchart of module inputPrice in P06_ex:
![]()
Revised: 11/01/2010 - www.mesacc.edu/~marquez/cis162ab/p06_ex_call_by_ref.html