Question

In: Computer Science

IN C++ Write a program to gauge the rate of inflation for the past year. The...

IN C++

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%.

Enter last year and this year's prices, please.

Enter prices as doubles

2

2.5

25.00%

Do you wish to do another? y

Enter last year and this year's prices, please.

Enter prices as doubles

5000

5200

4.00%

Do you wish to do another? n

Solutions

Expert Solution

C++ Code:

#include <iostream>
#include <iomanip>

using namespace std;

// function declaration
double calcaulteRateOfInflation(double lastYearPrice, double currentYearPrice);

int main ()
{
// Variables declaration
char ch;
double lastYearPrice, currentYearPrice;
  
// Asking user input
cout << "Enter last year and this year's prices, please." << endl;
cout << "Enter prices as doubles" << endl;
cin >> lastYearPrice;
cin >> currentYearPrice;
  
// Calling function to compute the rate of inflation
double rateOfInflation = calcaulteRateOfInflation(lastYearPrice ,currentYearPrice);
  
// Displaying the rate of inflation in percentage with 2 decimal values
std::cout << std::fixed;
std::cout << std::setprecision(2);
cout << rateOfInflation << "%" << endl;
  
// Allow the user to repeat this calculation
cout << "Do you wish to do another? ";
cin >> ch;
if(ch == 'y' || ch == 'Y'){
main(); // calling main method so it repeats this calculation
}
  
return 0;
}

// Define a function to compute the rate of inflation.
double calcaulteRateOfInflation(double lastYearPrice, double currentYearPrice){
  
double rateOfInflation = currentYearPrice - lastYearPrice;
rateOfInflation = (rateOfInflation / lastYearPrice ) * 100;
  
return rateOfInflation;
}

Output:

Code Snapshots:


Related Solutions

The inflation rate over the past year was 4.2 percent. If an investment had a real...
The inflation rate over the past year was 4.2 percent. If an investment had a real return of 9.4 percent, what was the nominal return on the investment?
Write a program In C to compute internet charges according to a rate schedule. The rate...
Write a program In C to compute internet charges according to a rate schedule. The rate schedule is as follows: $0.08 per GB for usage between 0 and 40 GB, inclusive $0.07 per GB for usage between 41 GB and 70 GB, inclusive $0.05 per GB for usage between 71 GB and 110 GB, inclusive $0.04 per GB for usage greater than 110 GB Learning Objectives In this assignment, you will: Use a selection control structure Use a repetition control...
Write a C++ program that calculates mileage reimbursement for a salesperson at a rate of $...
Write a C++ program that calculates mileage reimbursement for a salesperson at a rate of $ 0.35 per mile. Your program should interact with the user in this manner: MILEAGE REIMBURSEMENT CALCULATOR Enter beginning odometer reading: 13505.2 Enter ending odometer reading     : 13810.6 You traveled 305.40 miles. At $ 0.35 per mile, your reimbursement is $ 106.89. The values in red color are inputs for the program. The values in blue color are results of expressions.
Q1 For this question, assume that expected inflation this year is equal to past year's inflation....
Q1 For this question, assume that expected inflation this year is equal to past year's inflation. Also assume that the unemployment rate has been equal to the natural rate of unemployment for some time. Given this information, we know that: A. the rate of inflation should be zero. B. the rate of inflation should neither increase nor decrease. C. the rate of inflation should steadily increase. D. the rate of inflation should steadily decrease. E. the natural rate of unemployment...
C++ write a program that asks the user to enter the hours and rate then calculate...
C++ write a program that asks the user to enter the hours and rate then calculate the gross pay for an employee, the program should test if the hours are regular (40), any hour more than 40 should be paid with the overtime rate: 1.5*rate. The program should ask repeatedly the user if he/she wants to continue: y or n, if the user types y, then the program should ask for the hours and rate for another employee then display...
Program in C: Write a program in C that reorders the elements in an array in...
Program in C: Write a program in C that reorders the elements in an array in ascending order from least to greatest. The array is {1,4,3,2,6,5,9,8,7,10}. You must use a swap function and a main function in the code. (Hint: Use void swap and swap)
Subject is C++ Write a program to compute internet charges according to a rate schedule. The...
Subject is C++ Write a program to compute internet charges according to a rate schedule. The rate schedule is as follows: $0.08 per GB for usage between 0 and 40 GB, inclusive $0.07 per GB for usage between 41 GB and 70 GB, inclusive $0.05 per GB for usage between 71 GB and 110 GB, inclusive $0.04 per GB for usage greater than 110 GB code must use these four functions, using these names (in addition to main): getData computeCharges...
Write a program in C (NOT C++ or C#) The program inputs 5 elements into each...
Write a program in C (NOT C++ or C#) The program inputs 5 elements into each of 2 integer arrays. Multiply corresponding array elements, that is, arrayOne[0] * arrayTwo[0], etc. Save the product into a third array called prodArray[ ]. Display the product array.
Write down the C++ Program
 Write a function, which accept three integer values as arguments find the largest of three and then return the largest value to main program. Write a main program which will call the function by passing three integer values and print the value returned by the function.?
-In C Programming- Write a program to display the total rainfall for a year. In addition,...
-In C Programming- Write a program to display the total rainfall for a year. In addition, display the average monthly rainfall, and the months with the lowest and highest rainfall. Create an array to hold the rainfall values. Create a 2nd parallel array (as a constant) to hold the abbreviated names of the months. I created my arrays to be 1 element bigger than needed, and then disregarded element [0] (so that my months went from [1] = "Jan" to...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT