In C language, Write a program that deals with inflation, which is essentially t
ID: 3666796 • Letter: I
Question
In C language, Write a program that deals with inflation, which is essentially the rising cost of general goods over time. That is, the price of goods, such as a packet of peanuts, goes up as time goes by. So, you will write a program to gauge the expected cost of an item in a specified number of years. The program asks for the cost of the item, the number of years, and the rate of inflation. The output is the estimated cost of the item after that number of years, using the given inflation rate. The user enters the inflation rate as a percentage, for example 4.5. You will have to convert the percentage to a fraction (like 0.045), and then use a loop to estimate the item’s price adjusted for inflation. Note that you must check each of the values provided by the user to make sure that they are reasonable.
To actually adjust the price for inflation, you need to increase the price by the inflation rate each year separately. For example, if you have an item that is initially priced at $10, with an inflation rate of 10%, then the adjusted prices will be:
After 1 year: $10.00 * (1 + 0.10) = $11.00
After 2 years: $11.00 * (1 + 0.10) = $12.10
After 3 years: $12.10 * (1 + 0.10) = $13.31
…
In other words, to calculate the price after another year, you have to use the value from the current year, NOT the original price. To do this, you have to use a while or for loop.
Explanation / Answer
#include <stdio.h>
float getValueF(const char*s, float min, float max);
int getValueI(const char*s, int min, int max);
#define COST_MIN 1
#define COST_MAX 100
#define YEARS_MIN 1
#define YEARS_MAX 100
#define INFLATION_MIN 0.5
#define INFLATION_MAX 50
int main(void)
{
float price = getValueF("Enter the item cost ($)",
COST_MIN, COST_MAX);
int years = getValueI("Enter the number of years",
YEARS_MIN, YEARS_MAX);
float inf = getValueF("Enter the inflation rate (%)",
INFLATION_MIN, INFLATION_MAX);
inf /= 100;
for(int y=0; y<years; y++)
{
price += price * inf;
printf(" Price after %d years = $%.2f", y, price);
}
return 0;
}
float getValueF(const char*s, float min, float max){
float v;
int bRetry = 0;
printf(" %s", s);
do
{
scanf("%f", &v);
bRetry = v < min || max < v;
if(bRetry)
{
printf(" Value should be between %.2f and %.2f", min, max);
}
}
while(bRetry);
return v;
}
int getValueI(const char*s, int min, int max)
{
int v;
int bRetry = 0;
printf(" %s", s);
do
{
scanf("%d", &v);
bRetry = v < min || max < v;
if(bRetry)
{
printf(" Value should be between %d and %d", min, max);
}
}
while(bRetry);
return v;
}