Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

#include <iostream> using namespace std; const double STATE_SALE_TAX = 0.02; con

ID: 668370 • Letter: #

Question

#include <iostream>
using namespace std;
const double STATE_SALE_TAX = 0.02;
const double COUNTRY_SALE_TAX = 0.04;
int main ()
{
double amtPurch;
double stateTax;
double countryTax;
cout << "How much did you purchase?";
cin >> amtPurch;
stateTax = STATE_SALE_TAX * amtPurch;
countryTax= COUNTRY_SALE_TAX * amtPurch;
cout<<"Amount of purchase "<<amtPurch<<endl;
cout<< "State sales tax: "<<stateTax<<endl;
cout << "Country sales tax: " << countryTax<<endl;
cout << "Total sale : "<<stateTax + countryTax + amtPurch;
return 0;
}

How do you re-write this code using Modules

Explanation / Answer

I have modularized your program as follows. I have tried to keep it very simple. At first, find the whole program and then I explain where I ahve replaced the statements with the functions.

//Modular Program

#include <iostream>
using namespace std;
const double STATE_SALE_TAX = 0.02;
const double COUNTRY_SALE_TAX = 0.04;

//Function Declarations
// return_type function_name (arguments_return_types)

double getPurchaseAmt();
double calStateTax(double);
double calCountryTax(double);                  
void Display(double, double, double);
double calTotalSale(double, double, double);

int main ()
{
double amtPurch;
double stateTax;
double countryTax;

//Function Calls
amtPurch = getPurchaseAmt();
                                      
stateTax = calStateTax(amtPurch);       
countryTax= calCountryTax(amtPurch);

Display(amtPurch, stateTax, countryTax);

return 0;
}

//Function definitions
// Need to follow the same prototype as at the time of declarations

double getPurchaseAmt()              
{
   double amt;
   cout << "How much did you purchase?";
   cin >> amt;
   return amt;
}

double calStateTax(double amtPurch)
{
   return STATE_SALE_TAX * amtPurch;
}

double calCountryTax(double amtPurch)
{
   return COUNTRY_SALE_TAX * amtPurch;
}

void Display(double amtPurch, double stateTax, double countryTax)
{
   cout<<"Amount of purchase "<<amtPurch<<endl;
   cout<< "State sales tax: "<<stateTax<<endl;
   cout << "Country sales tax: " << countryTax<<endl;
   cout << "Total sale : "<< calTotalSale(amtPurch, stateTax, countryTax);
}

double calTotalSale(double amtPurch, double stateTax, double countryTax)
{
   return stateTax + countryTax + amtPurch;
}

While writing modules, you need to write three things:

1. Function Declaration (also called as prototype)
2. Function Definition
3. Function Call

Function declaration is written at the top of the program before main function such that when you call a function from the main function it knows the signature of the function. Signature of the function include the return type of the function- what it returns, name of the function and at last what type of arguments it receives.

You need to follow the same signature while writing function definition or calling the function. Once you have written the function declarations at the top, you can define the functions anywhere, in any order except inside main.

You can also skip the function declaration part if you write the definitions itself at the top, as it will also tell the compiler the signature of the functions.

Now lets see, which statements from your program resulted in to modules and how.

First, you wanted to get the purchase amount from user. For that, you wrote the following statements

cout << "How much did you purchase?";
cin >> amtPurch;

I replaced it with the function
double getPurchaseAmt();
This is the function declaration given at the top where double is the return type as you want to have a double value.
Following is the definition where I simply return the double value got as input from the user.
double getPurchaseAmt()              
{
   double amt;
   cout << "How much did you purchase?";
   cin >> amt;
   return amt;
}

I have called this function from the main using the statement below:
amtPurch = getPurchaseAmt();
As getPurchaseAmt function returns a double value, i need a variable of double type (amtPurch in this case) to collect the value returned.

Similarly, I wrote the functions for other tasks in your program.
stateTax = STATE_SALE_TAX * amtPurch;
it has been replaced by the function calStateTax. Its declaration is as follows
double calStateTax(double);
Its declaration tells us that it returns a value of double type and also recieves a double type value. Function defintion is as follows:
double calStateTax(double amtPurch)
{
   return STATE_SALE_TAX * amtPurch;
}

it simply receives the amtPurch as an argument, calculate the stateTax using the same method as in your statement and returns the result.
stateTax = calStateTax(amtPurch);
The returned result is collected in the variable stateTax.

Similar function is written for calculating country tax.
double calCountryTax(double);

Next, all the cout statements have been replaced by the function Display
cout<<"Amount of purchase "<<amtPurch<<endl;
cout<< "State sales tax: "<<stateTax<<endl;
cout << "Country sales tax: " << countryTax<<endl;
cout << "Total sale : "<<stateTax + countryTax + amtPurch;

These statements have been replaced by
Display(amtPurch, stateTax, countryTax);

Display functions's declaration is as follows:
void Display(double, double, double);
Now, in this case, return type is void as we dont want to return any value. we simply want to display the three double values received as arguments. Function definition is:
void Display(double amtPurch, double stateTax, double countryTax)
{
   cout<<"Amount of purchase "<<amtPurch<<endl;
   cout<< "State sales tax: "<<stateTax<<endl;
   cout << "Country sales tax: " << countryTax<<endl;
   cout << "Total sale : "<< calTotalSale(amtPurch, stateTax, countryTax);
}

Notice in the last cout statement, I have replaced another subtask with a function
You wanted to print the total sale which is a sum of three values written as
cout << "Total sale : "<<stateTax + countryTax + amtPurch;

So, to calculate the total sale, I wrote a function calTotalSale(), as sum of three double values will be double value itself, the function returns a double which can be printed by calling the function directly in the cout statement as follows:
cout << "Total sale : "<< calTotalSale(amtPurch, stateTax, countryTax);

Its declaration is as follows:
double calTotalSale(double, double, double);
Receives three double values and returns a double value.
Definition is as follows:
double calTotalSale(double amtPurch, double stateTax, double countryTax)
{
   return stateTax + countryTax + amtPurch;
}

Hope, I was able to explain it and make you uderstand. Its not very difficult and you will get it with a little practice. Try refactoring simple programs on your own.