fuctions

i am having trouble with functions


write a program that outputs inflation rates for two successive years and determines whether the inflation is increasing or decreasing.

Ask the user to input the current price of an item (currPrice), its price one year ago (oneYearPrice) and two years ago (twoYearPrice).

To calculate the inflation rate for a year, subtract the price of the item (for that year) from the price of the item one year ago and then divide the result by the price a year ago.

For example, the current inflation (currInf) would be oneYearPrice subtracted from currPrice and then divided by oneYearPrice. Similarly, last years inflation (prevInf) would be twoYearPrice subtracted from oneYearPrice and than divided by twoYearPrice.

Your program must contain at least the following functions: a function to calculate the results, and a functions to output the results. Use appropriate parameters (function arguments) to pass the information in and out of the functions. (Do not use global variables).

The point of the exercise is to learn more about information passing between functions. Notice that the point assignment for this exercise is purely on function declaration and data exchange among functions.

I have some hints for you:

Your input is the price of an item (let's say the price of a gallon of gas) for the last three years. For example, user will enter the current price of a gallon of regular gas today (currentyear), last year today (lastyear), and two years ago today(twoyearsago).

Once you collect all the input you need in the main() function, you will need to compute the inflation.

Notice that, you will need to compute inflation twice. Inflation from last year to today, and inflation from two years ago to last year. I would write a function called (computeInflation()) that takes two prices of an item as input and computes and returns the inflation. I would need to call computeInflation() function twice, for the first call I send current year and the last year price as input and get last years inflation. The second call I would send the lat year's and two year ago price as input and get the inflation of two years ago. At this point you have two inflation rates in the main() function.

You should have a second function that prints out a nice output. You will need to send the two inflation rates you computed as input. The function would output the inflation rates and also tell us if inflation is on the rise or declining.

need some guidance in fixing up my code.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#include <iostream>
using namespace std;

int computeInflation(int oneYearPrice, int twoYearPrice);
double	computeInflation(double oneYearPrice, double twoYearPrice);
double	computeInflation(double oneYearPrice, int twoYearPrice);
double	computeInflation(int oneYearPrice, double twoYearPrice);

int main()
{

int currPrice;
int oneYearPrice;
int twoYearPrice;
int prevInf;
int currInf;

    if ( currInf > prevInf )
    {
        cout << "The inflation rate is increasing" << endl;
    }
    else if (currInf < prevInf)
    {
        cout << "The inflation rate is decreasing" << endl;
    }
    else 
    {
        cout << "There is no change in the inflation rate" << endl;
    }

    system ("PAUSE");    
    return 0;

cout << "what is the current price" << endl;
cin >> currPrice;
cout << "what is the  price of one year ago" << endl;
cin >> oneYearPrice;
cout << "what is the  price of two years ago" << endl;
cin >> twoYearPrice;

currInf = (currPrice-oneYearPrice) / (oneYearPrice);

currInf = (currPrice-twoYearPrice) / (twoYearPrice);

 return currInf;

cout << "your total tax is $" << currInf << " per month" << endl;

 system("PAUSE");
 return 0;
} 
Last edited on
need some guidance in fixing up my code.

So what's wrong with your code? Please ask specific questions.
Your program must contain at least the following functions:

And where are these required functions?
how to create one function that will calculate the inflation twice for the current year and the previous year?
You already have the function prototype for computeInflation. Now you just need to implement it.

In fact you have 4 function prototypes of computeInflation. You should only need one. I'll let you figure out which one.
Lol, you're in Prof. Ali class at Baruch aren't you. :)

FYI I wouldn't have included any info after "I have some hints for you", since that information was written by Ali himself. I'm doing this assignment now, once I finish I'll try to come back and help.
Topic archived. No new replies allowed.