Summary Instructions:(decisions,strings, arrays, functions) (C <stdio.h>,..) Wri
ID: 3717865 • Letter: S
Question
Summary Instructions:(decisions,strings, arrays, functions) (C <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 1
st 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 <Enter> 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 exam scores, 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.(*i cant get this line to display in my program*HELP PLZ)
The example runs show EXACTLY how your program input and output will look.
Explanation / Answer
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
bool validPrice(double price){
return( (price>= 0.01 && price <= 5.00) ? true : false);
}
double getAverage(double gasPrices[],int size){
double average = 0.0;
double sum = 0.0;
int i;
for( i = 0; i < size; i++){
sum += gasPrices[i];
}
average = ( sum / size);
return average;
}
bool IsRising(double price , double average){
return( (price < average) ? true : false);
}
int main(){
// 1. Create a local constant for the size of the array that will hold the 4 gas prices
const int size = 4;
// 2. Create 2 local constants for the minimum ($0.01) and maximum ($5.00) gas prices used when
double min = 0.01;
double max = 5.00;
// validating the gas prices entered by the user
// 3. Create a local character array (of size 30) to hold the gas station’s name
char gasStationName[30];
// 4. Create a local array to hold the station’s 4 gas prices
double gasPrices[4];
// 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 <Enter> at the end of the string
printf(" Enter the gas station’s name:");
fgets(gasStationName, 30, stdin);
int len = strlen(gasStationName);
if( gasStationName[len-1] == ' ' )
gasStationName[len-1] = 0;
// 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
int i = 0;
double price;
for( i = 0; i < size;){
printf("Enter %s's gas price #%d: $", gasStationName, i +1);
scanf("%lf", &price);
if(validPrice(price)){
gasPrices[i] = price;
i++;
}
else{
do{
printf("The price was not in the range of $0.01 - $5.00. ");
printf("Please re-enter gas price #%d: $", i +1);
scanf("%lf", &price);
}while(!validPrice(price));
gasPrices[i] = price;
i++;
}
}
// 8. Display a blank line between the inputs and the outputs
printf(" ");
// 9. Pass 4 pieces of data - the 4 individual gas prices in the array - to a function, calculate the
// average of the exam scores, 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
double average = getAverage(gasPrices, size);
// 10. In the main program, display the gas station’s name and average price of the gas
printf("%s had an average gas price of $%.2lf ",gasStationName, average);
// 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)
bool risingStatus = IsRising(gasPrices[0], average);
// 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
if(risingStatus){
printf("The price of gas at %s is rising ",gasStationName );
}
else{
printf("The price of gas at %s is falling ",gasStationName );
}
// 13. Display a blank line before "Press any key to continue…"
printf("Press any key to continue…");
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.