4 of 6 Part 3 (40 points) In this part, you are asked to write a program to acco
ID: 3789376 • Letter: 4
Question
4 of 6 Part 3 (40 points) In this part, you are asked to write a program to accomplish a specified task. Your solutions will be graded on efficiency. Declare and initialize allvariables you use. 1) Write a program that reads 4 lines from the user in the formatgiven below, and then calculates the average number of gallons of gas consumed per mile. You are encouraged to use a loop for this program but it is not necessary. Sample input: Date Distance (niles) Gas (gallons) 9/4 225.8 15.1 9/15 207 9/23 117.6 8.7 Sample output: The average gas mileage of your car is 15.374 miles per gallon, based on 550.4 miles travelled and 35.8 gallons of gas consumed. Solution: 12 points Sinclude int main (void) float total miles 0, total gas float miles, gas int i; scan f("a s s s s s"); //skip the first line for (im1 icm3 i++) scanf("E s Ef af", smiles, sgas) total miles total miles miles total gas total gas gas: The average gas mileage of your car is 8.3f miles per printf gallon, based on lf miles travelled and E.lf gallons of gas consumed. Vn", total miles/total gas total miles total gas) return 0Explanation / Answer
The main idea of loop here is to iterate 3 times. So, there is no hard and fast rule that you should used only given initialization and condition. You can use the following statement as well.
for(i =0 ;i<3;i++) or
for(i=3;i>=1;i--)
All these statement cause the loop statements to iterate 3 times or even you can write the loop statements 3 times but that will not be a good programming practice.
Hope it helps, do give your response.
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.