Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Need help writing this program for Java/C++. Application I\'m using is Bloodshed

ID: 3560056 • Letter: N

Question

Need help writing this program for Java/C++. Application I'm using is Bloodshed Dev-C++.

Program #1 is completed with code and output shown below. ONLY NEED HELP WITH WRITING PROGRAM #2

Details are as follows:

Program #1

Calculate and Display inflation rate for two years.

Write a program that displays inflation rates for two successive years and whether the inflation is increasing or decreasing. The user should input (cin) the current year (2013) price of an item, year 2012 and year 2011 price of the same item. To calculate the inflation rate for a year (say 2013), subtract the price of the item for that year (2013) from the price of the item one year (2012) ago and then divide the result by the price a year (2012) ago. If the result is positive, then there was inflation, otherwise, it is negavitve inflation (deflation).

Your program must contain at the following functions: a function (Input) to get input, a function (CalcInflation) to calculate inflation results, and a function (Output) to output the results (3 years price, and inflation rate for year 2013). Declare appropriate variable names and use appropriate parameters to pass information in and out of the function.

Code for Program #1

#include<iostream>
using namespace std;

double Input(int year)
{
double value;
cout <<"Enter price if item in " << year << " : ";
cin >>value;
cout << endl;
return value;
}
double CalcInflation(double p1, double p2)
{
return (p1-p2)/p1;
}
void Output(int y1,int y2,int y3,double p1,double p2, double p3, double p4,double p)
{
cout <<"price if item in year " << y1 << " is "<< p1 << endl;
cout <<"price if item in year " << y2 << " is "<< p2 << endl;
cout <<"price if item in year " << y3 << " is "<< p3 << endl;
cout <<"Inflation in year " << y2 << " is "<< p4 << endl;
cout <<"Inflation in year " << y3 << " is "<< p << endl;
}

int main()
{
double p1 = Input(2011);
double p2 = Input(2012);
double p3 = Input(2013);
Output(2011,2012,2013,p1,p2,p3,CalcInflation(p1,p2),CalcInflation(p2,p3));
system("pause");
return 0;
}

Output for Program #1

/*
Enter price if item in 2011 : 34

Enter price if item in 2012 : 17

Enter price if item in 2013 : 15

price if item in year 2011 is 34
price if item in year 2012 is 17
price if item in year 2013 is 15
Inflation in year 2012 is 0.5
Inflation in year 2013 is 0.117647
Press any key to continue . . .
*/

Program #2

Modify program 1 as follows:

Using looping control structure, the program should run three times to call all three functions one at a time to input prices of three items for three years (2013, 2012, and 2011), function Inflation to calculate inflation for year 2013, and function Output (3 years price, and inflation rate for year 2013) to display the results.

Finally, the function Output should write the input data and calculated results to an output data file inflation.dat

Explanation / Answer

#include<iostream>
#include<iomanip>
#include<string>
using namespace std;

// The function prototypes are put before the main function
double getDoubleInput(string message);
double inflationRate(double oldPrice, double newPrice);
void printPercent2d(double value);

int main()
{
    double price1; // The price of the item two years ago
    double price2; // The price of the item one year ago
    double price3; // The current price of the item
    double rate1; // The inflatin rate for the first two years
    double rate2; // The inflation rate for the second two years
   
    // Ask the user to input the prices.
    //
    // Note that by using the getIntInput function we can write one
    // line of code to get the user input. The function takes care
    // of getting and checking the input so we don't need to repeat
    // this code.
    price1 = getDoubleInput("Enter the price from two years ago:");
    price2 = getDoubleInput("Enter the price from one year ago:");
    price3 = getDoubleInput("Enter the current price:");

    // Add some space for nice output formatting
    cout << endl;

    // Calculate the inflation rate from two years ago to one year ago,
    // and print the rate
    //
    // Note that the variables we use as actual parameters to the function
    // do not need to have the same names as the function's formal parameters
    // The names of the formal parameters are just for use within the function
    rate1 = inflationRate(price1, price2);
   
    // I first print a descriptive message to the console output, then I call
    // the printPercent2d function. I cannot put the function call into the
    // line that prints to console output because the function's return type is
    // void.
    cout << "The inflation rate from two years ago to one year ago is: ";
    printPercent2d(rate1);   
   
    // Calculate the inflation rate from oneyear agoto the current year,
    // and print the rate
    rate2 = inflationRate(price2, price3);
   
    cout << "The inflation rate from one year ago to now is: ";
    printPercent2d(rate2);
   
    // Add some space for nice output formatting
    cout << endl;
   
    // Determine if the inflation rate is increasing or decreasing
    if ( rate1 < rate2 )
    {
        cout << "The inflation rate is increasing" << endl;
    }
    else if (rate1 > rate2)
    {
        cout << "The inflation rate is decreasing" << endl;
    }
    else
    {
        cout << "There is no change in the inflation rate" << endl;
    }

       
    return 0;

} // end of main function


//
// This function displayes the given message and returns the integer
// the user inputs. It checks for proper user input and keeps looping
// until the user enters an integer.
//
// Note that this function can be reused in any program where we need
// to get integer input
//
double getDoubleInput(string message)
{
   
    double num; // the user input
   
    // Print the message and get the user input
    cout << message << " ";
    cin >> num;
   
    // If the user did not input an integer, keep asking for input
    // until they do input an integer
    while ( !cin )
    {
        cin.clear();
        cin.ignore(5000, ' ');
       
        cout << "Please enter a double: ";
        cin >> num;
    }
   
    // return the user input
    return num;
   
} // end getIntInput function


//
// This function calculates the inflation rate given the prices of two
// items. The oldPrice parameter is the previous value of the item and
// the newPrice parameter is the current (or newer) price of the item.
//
double inflationRate(double oldPrice, double newPrice)
{
    double rate; // The inflation rate
   
    // The inflation rate is the new price minus the old price, divided
    // by the old price
    // We don't have to worry about integer division because at least one
    // (in this case all) of the numbers involved in the division are doubles
    rate = (newPrice - oldPrice) / oldPrice;
   
    // return the inflation rate
    return rate;
   

} // end inflationRate function


//
// This function prints the given value as a percentage with two
// decimal places.
//
// This function is void type because it does not need to return anything.
// Its work is to print the given value in the proper format.
//
// Note that just like the getDoubleInput function, this function is very
// generic and could be used by many different programs.
//
void printPercent2d(double value)
{
    cout << fixed << setprecision(2);
    cout << (value * 100) << "%" << endl;
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote