Write a program that gauges the rate of inflation over the past year. The progra
ID: 3830153 • Letter: W
Question
Write a program that gauges the rate of inflation over the past year. The program asks for the price of an item (such as a hot dog or a 1-carat diamond) both from one year ago and today. It estimates the inflation rate as the difference in price divided by the year-ago price. Your program should allow the user to repeat this calculation as often as the user wishes. Define a function to compute the rate of inflation. The inflation rate should be a value of type double giving the rate as a percent, for example 5.32 for 5.32 percent. The inflation rate must be displayed to exactly two digits after the decimal point. Your program must use a function to compute the rate of inflation. A program which does not use a function will be awarded a score of zero, even if all tests pass. The presence of this function will be checked by the instructor and the TAs. You are only permitted to use the iostream library. Additionally, the program must make good use of comments in the code, per the discussions we have been having in class. Again, the presence of comments (and thier good use) will be checked by the instructor and the TAs. A session should look exactly like the following example (including whitespace and formatting note that there is no whitespace at the end of each of these lines and each printed line has a newline at the end), with all manners of different numbers for inputs and the output: Enter the old price (or zero to quit): 450 Enter the new price: 500 The inflation rate is 11.11% Enter the old price (or zero to quit): 30 Enter the new price 55 The inflation rate is 83.33% Enter the old price (or zero to quit) 9000 Enter the new price: 8750 The inflation rate is 2.78% Enter the old price (or zero to quit) 60 Enter the new price 600Explanation / Answer
//Program for Inflation rate using Function
#include <iostream>
#include <stdio.h>
#include <iomanip>
#include <stdlib.h>
using namespace std;
// Function
double infl_rate(double old_price,double new_price);
int main()
{
// Variable Declaration
double old_price, new_price;
float i;
// Get Input value
cout<<" Enter the old price(or Zero to Quit):"<<endl;
cin>>old_price;
if(old_price==0)
{
cout<<" Program Quits";
exit(0);
}
cout<<" Enter the new price:"<<endl;
cin>>new_price;
// Inflation rate Function call
cout<<setprecision(4);
cout<<" The Inflation rate is:"<<infl_rate(old_price,new_price)<<"%";
return 0;
}
// Inflation Function
double infl_rate(double old_price,double new_price)
{
return ((new_price - old_price) / old_price)*100;
}
-----------------------------------------------------------------------------------------------------------------------------------------------------------
OUTPUT
Enter the old price(or Zero to Quit):
450
Enter the new price:
500
The Inflation rate is:11.11%
--------------------------------------------------------------------------------------------------------------------------------------
Enter the old price(or Zero to Quit):
30
Enter the new price:
55
The Inflation rate is:83.33%
---------------------------------------------------------------------------------------------------------------------------------------------
Enter the old price(or Zero to Quit):
0
Program Quits
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.