C language (array, functions, decisions) (<stdio.h>..) Write a program to get a
ID: 3717863 • Letter: C
Question
C language (array, functions, decisions) (<stdio.h>..)
Write a program to get a gas station’s name and 4 gasoline prices, calculate and display the average of the prices, and determine and display if the price of gas is rising or not. Top-Level Instructions: ? Get the gas station name, and store the name in a variable. ? Using a loop, get 4 gas prices, validating that each price is between $0.01 and $5.00 (and continue to re-get the price if it is not in the range), and store the 4 prices in an array. ? Using a function, calculate and return the average of the 4 gas prices. Pass to the function the 4 gas prices. Store the return value from the function in a variable. ? Display the station’s name and average gas price. ? Using a function, determine and return whether the price of gas is rising or not. Pass to the function the 1st gas price and the average of the 4 gas prices. ? Display whether the price of gas is rising or not. Detailed Instructions: 1. Create a local constant for the size of the array that will hold the 4 gas prices 2. Create 2 local constants for the minimum ($0.01) and maximum ($5.00) gas prices used when validating the gas prices entered by the user 3. Create a local character array (of size 30) to hold the gas station’s name 4. Create a local array to hold the station’s 4 gas prices 5. Create any other local variables needed for the program 6. Prompt the user for the gas station’s name, use fgets() to get the name (which could include spaces), store the name in the character array, and strip-off the at the end of the string 7. Create a loop that runs 4 times to get the station’s 4 gas prices a. Prompt for the station’s gas price, and get the price b. Using the constants, validate that the price is between $0.01 and $5.00 (inclusive), and if not re-get the price until the price is in the correct range (use the constants for both the validation condition and the error message) (Note the indenting of the message and the re-prompt in Example Run #2) c. Store the price in the array 8. Display a blank line between the inputs and the outputs 9. Pass 4 pieces of data - the 4 individual gas prices in the array - to a function, calculate the average of the gas prices, return the average, and store the average in a variable (if you want, instead of passing the 4 individual gas prices, you can pass the entire array and its size to the function) (create local variables in the function as needed) (don’t forget to create the function prototype if necessary) Final Project 10. In the main program, display the gas station’s name and average price of the gas 11. Pass the 1st gas price and the average to a Boolean function named IsRising(), and determine if the price of gas is rising or not (create local variables in the function as needed) (do not display anything in the function – have the function return true if the price of gas is rising, or false if the price of gas is falling) (don’t forget to create the function prototype if necessary) 12. Using the return value of the Boolean function, determine and display in the main program whether the price of gas is rising or falling 13. Display a blank line before “Press any key to continue…” Example Run #1: Enter the gas station’s name: Geo Gas Enter Geo Gas’s gas price #1: $1.95 Enter Geo Gas’s gas price #2: $1.97 Enter Geo Gas’s gas price #3: $2.05 Enter Geo Gas’s gas price #4: $2.09 Geo Gas had an average gas price of $2.01. The price of gas at Geo Gas is rising. Example Run #2: Enter the gas station’s name: Fossil Fuel Enter Fossil Fuel’s gas price #1: $2.59 Enter Fossil Fuel’s gas price #2: $12.49 The price was not in the range of $0.01 - $5.00. Please re-enter gas price #2: $2.49 Enter Fossil Fuel’s gas price #3: $2.39 Enter Fossil Fuel’s gas price #4: $2.35 Fossil Fuel had an average gas price of $2.46. The price of gas at Fossil Fuel is falling. The example runs show EXACTLY how your program input and output will look.
Explanation / Answer
ANS:-
Given that,
a gas station’s name and 4 gasoline prices, calculate and display the average of the prices, and determine and display if the price of gas is rising or not. Top-Level Instructions.
PROGRAM:-
PLEASE REFER BELOW CODE
#include<stdlib.h>
#include<stdio.h>
#include<stdbool.h>
#include<string.h>
double cal_average(double *arr, int Size)
{
int i;
double avg=0.0;
//calculate sum of all elements of array
for(i = 0; i < Size; i++)
avg += arr[i];
return(avg/Size); //return average
}
bool IsRising(double first_price, double avg)
{
return(avg > first_price);
}
int main()
{
char gas_station_name[30];
double gas_price[4],p,avg;
int i = 0;
char *pos;
printf("Enter the gas station’s name: ");
fgets(gas_station_name,30,stdin);
if ((pos = strchr(gas_station_name, ' ')) != NULL) //removing trailing enter from string
*pos = '';
while(i < 4)
{
printf(" Enter %s’s gas price #%d: $", gas_station_name,(i+1));
scanf("%lf",&p);
if(p < 0.01 || p > 5.00) //checking with given range
{
printf(" The price was not in the range of $0.01 - $5.00.");
continue;
}
else
{
gas_price[i] = p;
i++;
}
}
printf(" ");
avg = cal_average(gas_price,4);
printf("%s had an average gas price of $%.2lf", gas_station_name,avg);
if(IsRising(gas_price[0], avg))
{
printf(" The price of gas at Geo Gas is rising. ");
}
else
printf(" The price of gas at Fossil Fuel is falling. ");
return 0;
}
PLEASE REFER BELOW OUTPUT
gcc -g gas_price.c
./a.out
Enter the gas station’s name: Geo Gas
Enter Geo Gas’s gas price #1: $1.95
Enter Geo Gas’s gas price #2: $1.97
Enter Geo Gas’s gas price #3: $2.05
Enter Geo Gas’s gas price #4: $2.09
Geo Gas had an average gas price of $2.01
The price of gas at Geo Gas is rising.
./a.out
Enter the gas station’s name: Fossil Fuel
Enter Fossil Fuel’s gas price #1: $2.59
Enter Fossil Fuel’s gas price #2: $12.49
The price was not in the range of $0.01 - $5.00.
Enter Fossil Fuel’s gas price #2: $2.49
Enter Fossil Fuel’s gas price #3: $2.39
Enter Fossil Fuel’s gas price #4: $2.35
Fossil Fuel had an average gas price of $2.46
The price of gas at Fossil Fuel is falling.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.