objective: To learn to use reference parameters and value parameters. To learn t
ID: 3833378 • Letter: O
Question
objective: To learn to use reference parameters and value parameters. To learn to write value- returning functions and void functions and use functions to modularize programs. Tasks Consider the definition ofthe function main: int main double unit Price int items; double totalcost return 0; The variables unitPrice, items, and totalcost are the local variables of the function main. Write the definition of function inputDatathatprompts the user toenter the unit price for one item and the numberofitems for a purchase to update the variables unitPrice and items ofthe function main. The unit price and the number of items must be non-negative numbers. If the user enters an invalid value, display an error message and ask the user to reenter the data until the input isvalid. 2 write a function getTotalcost that takes two parameters the unit price for one item and the number of items for a purchase. The function retums the total cost, including sales tax, forthat many items at the specified unit price. Assume the sales tax rate is9%. 3 Write the definition ofa function outputData that prints on the screen the unit price for one item, the number of items, and the total cost. 4 Make function calls in the main function to input data, process data, and output data.Explanation / Answer
#include<iostream>
using namespace std;
void inputData();
double getTotalCost(double price, int item);
void outputData(double price, int item);
int main(){
double unitPrice;
int items;
double totalcost;
inputData();
return 0;
}
void inputData(){
double price;
int item;
cout<<"Enter the unit price of one item:"<<endl;
cin>>price;
cout<<"Enter the number of item for purchase:"<<endl;
cin>>item;
if(price<0 || item<0){
cout<<"invalid data entered "<<endl;
inputData();
}
if(price>=0 && item>=0){
double totalcost = getTotalCost(price,item);
outputData(price,item);
}
}
double getTotalCost(double price, int item){
double totalcost= price*item+ price*item*0.09;
return totalcost;
}
void outputData(double price, int item){
cout<<"price of item: "<<price<<endl;
cout<<"number of item: "<<item<<endl;
cout<<"total cost of item: "<<getTotalCost(price,item);
}
here i am creating 3 functions that is performing their respective task regarding calculating the cost of item.
first we declare all the three functions and calculating the price of item.
void inputData(){
double price;
int item;
cout<<"Enter the unit price of one item:"<<endl;
cin>>price;
cout<<"Enter the number of item for purchase:"<<endl;
cin>>item;
if(price<0 || item<0){
cout<<"invalid data entered "<<endl;
inputData();
}
if(price>=0 && item>=0){
double totalcost = getTotalCost(price,item);
outputData(price,item);
}
}
this function is used to get the data or input the data from user and calling the getTotalCost() function to calculate the total price of item
double getTotalCost(double price, int item){
double totalcost= price*item+ price*item*0.09;
return totalcost;
}
and passing the values to the outputData() function for printing the price of item, number of item and total cost of item
void outputData(double price, int item){
cout<<"price of item: "<<price<<endl;
cout<<"number of item: "<<item<<endl;
cout<<"total cost of item: "<<getTotalCost(price,item);
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.