Write a function to calculate the rate of inflation for the past year. The function receives the current price of some item and its associated price one year ago as inputs. The item could be a kilogram of sugar or flour, for example. It then estimates the inflation rate as the difference in price divided by the year-ago price. The estimated inflation rate should be returned as a value of type double giving the rate in percent, for example, 5.3, if the inflation was estimated to be 5.3 percent.
#include<iostream>
#include<cmath>
using namespace std;
double inflationMethod(double previousCost,double currentCost)
{
return ((currentCost - previousCost)/currentCost);
}
int main()
{
double previousCost,currentCost,rate;
char option;
do {
cout<<"Enter previous year cost of the item: ";
cin>>previousCost;
cout<<"Enter present year cost of the item: ";
cin>>currentCost;
rate = inflationMethod(previousCost,currentCost);
cout<<"Rate of inflation:"<<rate*100<<"%"<<endl;
cout<<"To continue then enter 'Y': ";
cin>>option;
} while (option=='Y' || option=='y');
}
Comments
Leave a comment