SHARE
SPREAD
HELP

The Tradition of Sharing

Help your friends and juniors by posting answers to the questions that you know. Also post questions that are not available.


To start with, Sr2Jr’s first step is to reduce the expenses related to education. To achieve this goal Sr2Jr organized the textbook’s question and answers. Sr2Jr is community based and need your support to fill the question and answers. The question and answers posted will be available free of cost to all.

 

#
Authors:
Walter Savitch ,kenrick Mock
Chapter:
Function Basics
Exercise:
Programming Projects
Question:3 | ISBN:9780132846813 | Edition: 5

Question

Enhance your program from the previous exercise by having it also print out the estimated price of the item in one and in two years from the time of the calculation. The increase in cost over one year is estimated as the inflation rate times the price at the start of the year. Define a second function to determine the estimated cost of an item in a specified number of years, given the current price of the item and the inflation rate as arguments.


TextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbook

Answer

Program Code:

#include<iostream>
#include<cmath>
using namespace std;

//declare the method to calculate inflation
double calcInflation(double previousCost,double currentCost);
//declare the method to calculate cost after inflation
double estimateInflationCost(double currentCost, double inflationRate);

int main()
{
    //declare the variables
    double previousCost, currentCost, inflationRate, increasedCost;
    char option;
    //loop to iterate till the user choose to continue
    do
    {
        //prompt and read cost of the item in the previous year
        cout<<"Enter previous year cost of the item: ";
        cin>>previousCost;
        //prompt and read cost of the item in the current year
        cout<<"Enter present year cost of the item: ";
        cin>>currentCost;
        //call method to calculate cost of inflation
        inflationRate = calcInflation(previousCost,currentCost);
        //diaply cost of inflation
        cout<<"Rate of inflation: "<<inflationRate*100<<"%"<<endl;
        //call method to calculate and display cost of item after one year due to inflation
        increasedCost = estimateInflationCost(currentCost, inflationRate);
        cout<<"Cost of the item after one year due to inflation: "<<increasedCost<<endl;
        //call method to calculate and display cost of item after two year due to inflation
        increasedCost = estimateInflationCost(increasedCost, inflationRate);
        cout<<"Cost of the item after two years due to inflation: "<<increasedCost<<endl;
        //prompt and read the user choice to continue or not
        cout<<"To continue then enter 'Y':";
        cin>>option;
    }while((option=='Y')||(option=='y'));
   
    return 0;
}

//define the method to calculate inflation
double calcInflation(double previousCost,double currentCost)
{  
    //calculate and return the inflation rate
    return ((currentCost - previousCost)/currentCost);
}

//define the method to calculate cost after inflation
double estimateInflationCost(double currentCost, double inflationRate)
{
    //calculate and return the increase in cost due to inflation after one year 
    return currentCost+(currentCost*inflationRate);
}

Sample output:

0 0

Discussions

Post the discussion to improve the above solution.