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 ,julia Lobur
Chapter:
Procedural Abstraction And Functions That Return A Value
Exercise:
Programming Projects
Question:4 | ISBN:9780321531346 | Edition: 7

Question

4. Write a program to gauge the rate of inflation for the past year. The program

asks for the price of an item (such as a hot dog or a one carat diamond)

both 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.3 for 5.3%.

TextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbook

Answer

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

double inflationMethod(double previousCost,double currentCost);


void 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'\n";
   cin>>option;
   system("pause");
}

double inflationMethod(double previousCost,double currentCost)
{  

	return ((currentCost - previousCost)/currentCost);
}

Output:

Enter previous year cost of the item: 120
Enter present year cost of the item:145
Rate of inflation: 0.172414%
To continue then enter 'Y' n

 

0 0

Discussions

Post the discussion to improve the above solution.