forked from IMACULGY/CPPExamples
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInflationRates.cpp
More file actions
70 lines (59 loc) · 1.91 KB
/
InflationRates.cpp
File metadata and controls
70 lines (59 loc) · 1.91 KB
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
/*
Write a program that outputs inflation rates for two successive years and
whether the inflation is increasing or decreasing. Ask the user to input the
current price of an item and its price one year and two years ago. 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. Your program must contain at least the following
functions: a function to get the input, a function to calculate the results, and
a function to output the results. Use appropriate parameters to pass the
information in and out of the function. Do not use any global variables.
You may name the program as you please, for example, "InflationRates.cpp"
Please make sure the program compiles and runs as it should!
*/
#include<iostream>
#include<vector>
using namespace std;
vector<int> current_price(){
// get the requiered prices
vector<int> res;
int tmp;
cout << "What is the current price?" << endl;
cin >> tmp;
res.push_back(tmp);
cout << "What was the price one year ago?" << endl;
cin >> tmp;
res.push_back(tmp);
cout << "What was the price two years ago?" << endl;
cin >> tmp;
res.push_back(tmp);
return res;
}
vector<float> inflation_rate(vector<int> prices){
// calculate the inflation rate
vector<float> inf_rate;
for (int i=0; i < prices.size() - 1; i++)
{
float temp;
temp = (prices[i] - prices[i+1])/static_cast<float>(prices[i+1]);
inf_rate.push_back(temp);
}
return inf_rate;
}
void display_inflation_rate(vector<float> rates)
{
// display the inflation rates
cout << "The infations rate are (from most recent to oldest):" << endl;;
for (const auto i:rates)
cout << ' ' << i;
cout << endl;
}
int main()
{
// conbine all the functions called above
vector<int> price = {};
vector<float> inf_rate = {};
price = current_price();
inf_rate = inflation_rate(price);
display_inflation_rate(inf_rate);
}