Answer:
Here is the C++ program:
#include<iostream> //to use input output functions
#include<cmath> //to use math functions
using namespace std; //to identify objects cin cout
double InflationRate(float oldCPI,float newCPI); //function prototype
int main() { //start of main function
float oldCPI,newCPI,rate; //declares two float variables for the old and consumer price indices
char choice; //stores user choice to continue
double sum=0.0; // computes the sum of inflation rates
int count = 0; //to count the number of inflation rates computed
double average = 0.0; //to compute the average inflation rate
do { //this loop continues to execute and ask user to enter cpis
cout << "Enter the old and new consumer price indices: "; //prompts user to enter new and old cpi
cin>>oldCPI>>newCPI; // Read in two float values for the cpi
rate = InflationRate(oldCPI,newCPI); //call the function InflationRate with the two cpis
cout << "Inflation rate is: "<< rate<< endl; //displays the result
sum = sum + rate; //computes sum of inflation rates
count++; //counts number of computed inflation rates
cout<<"Try again? (Y or y): "; //prompts if user wants to continue
cin>>choice; //reads choice of user y or Y
}while((choice=='Y')||(choice=='y')); //the loop continues to repeat as long as user enters small or capital y
average = sum/count; //computes the average inflation rate
cout<<"Average rate is: "<<average;} //displays the average inflation rate
double InflationRate(float oldCPI,float newCPI){ // function that takes two arguments oldCPI and new CPI and computes the inflation rate
return (newCPI - oldCPI) / oldCPI * 100;} //formula to compute inflation rate
Step-by-step explanation:
1.
/*This program computes the inflation rate using two consumer price indexes. It takes as parameters, the previous price index and current price index and computes inflation rate by the formula:
(newCPI- oldCPI) / oldCPI and multiply the result by 100 */
#include <iostream> //to use input output functions
using namespace std; //to identify objects cin cout
2.
float oldCPI, newCPI; //declare two float variables for old and new consumer price index
cout << "Enter the old and new consumer price indices: "; //prompts user to enter values for old and new cpi
cin>>oldCPI>>newCPI; //read in two float values for the cpi and store them in the variables oldCPI and newCPI
double result = InflationRate(oldCPI,newCPI); //call the function InflationRate with the two cpis
cout << "Inflation rate is: " << result<< endl; //prints the result
3. and 4.
The complete program is given in the Answer section
For example if user enters 238.343 as old cpi and 238.250 as new cpi then the program works as follows:
oldCPI =238.343
newCPI = 238.250
Now the function InflationRate is called by passing these two parameter and this function returns :
return (newCPI - oldCPI) / oldCPI * 100;}
this becomes
(newCPI - oldCPI) / oldCPI * 100
(238.250 - 238.343) / 238.343 * 100
-0.093/238.343 * 100
- 0.00039 * 100
= -0.039 0204
Hence
result = -0.039 0204
Next, sum = sum + rate; statement computes the sum as:
sum = 0.0 + (-0.039 0204)
sum = -0.039 0204
count++ increases the count by 1
count = 1
Next user is asked if he/she wants to continues. If user presses y or Y then user is asked again to enter values for new and old cpi. Suppose user enters the following values:
oldCPI =238.250
newCPI = 237.852
Now the function InflationRate is called by passing these two parameter and this function returns :
return (237.852- 238.250) / 238.250* 100;}
this becomes
(newCPI - oldCPI) / oldCPI * 100
(237.852- 238.250) / 238.250* 100
−0.398/238.250* 100
- 0.001670491 * 100
= -0.167049
Hence
result = -0.167049
Next, sum = sum + rate; statement computes the sum as:
sum = -0.039 0204 + (-0.167049)
sum = -0.206069
count++ increases the count by 1
count = 2
Now lets say the user enters n or any other key when asked if user wants to continue. So the loop breaks and program moves to the statement:
average = sum/count;
This becomes:
average = -0.206069/2
average = −0.103034
Hence
Average rate is: −0.103034