Write a program that outputs inflation rates for two successive years and whethe
ID: 3563879 • Letter: W
Question
Write a program that outputs inflation rates for two successive years and whether the inflation is increasing or decreasing. Ask the user to input the current price of an item and its price one year and two years ago. To calculate the inflation rate for a year, subtract the price of the item for that year from the price of the item one year ago and then divide the result by the price a year ago. Your program must contain at least the following functions: a function to get the input, a function to calculate the results, and a function to output the results. Use appropriate parameters to pass the information in and out of the function. Do not use any global variables.
Step 0: Write a void function (that takes no input parameters) called greetUser(). This function couts program greeting header (ex. welcome to inflation calculator etc.). We will call this function at the very beginning of our program.
Step 1: Get the necessary input from user. In your main() function get the current price (C) of an item, it's previous year's (P) price and also its price two years ago (T). Your function will use cin to get these pricies.
Step 2: Write a function (computeInflation) that would compute inflation from a given pair of price. The input to this function is the price of an item for some year and a year before that. For example, if you call these input parameters currentPrice and previousPrice, the function should compute and return inflation as (currentPrice - previousPrice) / previousPrice. Note that this is not a void function. This function take two input parameters and returns the inflation rate.
Step 3: Call this function (computeInflation) twice. The first time to compute current year's inflation (inf1). To compute current year's inflation, we will need to send in C and P as input parameters to computeInflation(). To compute last year's inflation (inf2), we will send in P and T to computeInflation().
Step 4: Looking at the inflation data (inf1 and inf2) computed for the last two years, decide if the inflation is on the rise, decline or unchanged. if inf1 > inf2, the inflation is on the rise, if inf2 > inf1, then inflation is on decline, no change in inflation otherwise.
No need to use header files (*.h) or multiple cpp files in this assignment, but make sure to use a prototype of greetUser() and computeInflation() functions at the top of the cpp file.
Here are some test cases:
T -> the price of the item two years ago.
P -> the price of the item last year.
C -> the current price of the item.
CASE I:
T=$1.00
P=$2.00
C=$4.00
Result: No Change
CASE II:
T=$1.00
P=$2.00
C=$5.00
Result: Inflation on the rise.
CASE III:
T=$1.00
P=$2.00
C=$3.00
Result: Inflation is on decline.
Explanation / Answer
#include using namespace std; void getinp(double* pc, double* p1, double* p2); void inflationRate(double pc, double p1, double p2, double* rate1, double* rate2); void showresult(double rate1, double rate2); #include using namespace std; void getinp(double* pc, double* p1, double* p2); void inflationRate(double pc, double p1, double p2, double* rate1, double* rate2); void showresult(double rate1, double rate2); int main () { double pc = 0, p1 = 0, p2 = 0, ra1 = 0, ra2 = 0; getinp(&pc, &p1, &p2); inflationRate(pc, p1, p2, &ra1, &ra2); showresult(ra1, ra2); cin.clear(); cin.ignore(255, ' '); cin.get(); return 0; } void getinp( double* pc, double* p1, double* p2) { coutRelated Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.