Please write a program that does the following: • (1 Point) Provide the followin
ID: 3816102 • Letter: P
Question
Please write a program that does the following:
• (1 Point) Provide the following “menu” (choices) to the user:
o Enter trip data
o Show Eco Score
o Exit program
• Ask the user to choose from the menu
o (2 Points) Continue to show the menu (even with invalid choice – give error message
but don’t exit) until the user chooses “Exit program.”
• For “Enter trip data”:
o (1 Point) Ask the user for:
- # miles traveled in the past 5 minutes
- Ask the user for gallons of gas consumed in the past 5 minutes
o (2 Points) Calculate total distance traveled, total gas consumed (gallons), MPG over the past 5 minutes, and cumulative MPG
o Ask the user if the user would like to add more data in 5 minute increments
- (2 Points) Allow the user to keep entering data in 5 minute increments until the user has no more data to enter
o (1 Point) Return to the menu when the user is done entering trip data
• For “Show Eco Score”
o (2 Points) Show total distance traveled, total gas consumed, and cumulative MPG
o (3 Points) Show an appropriate message (you decide the message) for when cumulative MPG is: Over 100, between 50 and 100, between 30 and 50, between 10 and 30, and below 10.
o (1 Point) Return to the menu after displaying message
Explanation / Answer
#include <stdio.h>
int main(void)
{
int miles,gallons,choice;
float CMPG,MPG;
char Continue;
do
{
//menu
printf(" 1.Enter trip data");
printf(" 2.Show Eco Score");
printf(" 3.Exit program");
printf(" Enter your choice : ");
scanf("%d",&choice);
switch(choice)
{
case 1:
CMPG = 0;
do
{
printf(" Enter # miles travelled in the past 5 minutes");
scanf("%d",&miles);
printf(" Enter gallons of gas consumed in the past 5 minutes");
scanf("%d",&gallons);
MPG = (double)miles/gallons;
CMPG = CMPG + MPG;
printf(" Distance = %d gallons = %d MPG = %.2f Commulative MPG = %.2f ",miles,gallons,MPG,CMPG);
printf(" Do you want to add more data in 5 minute increments <y-yes, n-no>");
scanf(" %c",&Continue);
if(Continue == 'n' || Continue == 'N')
break;
}while(Continue == 'y' || Continue == 'Y');
break;
case 2:
printf(" Distance = %d gallons = %d Commulative MPG = %.2f ",miles,gallons,CMPG);
if(CMPG > 100)
printf(" Excellent Average");
else if(CMPG >= 50 && CMPG <= 100)
printf(" Very Good Average");
else if(CMPG >= 30 && CMPG <= 50)
printf(" Good Average");
else if(CMPG >= 10 && CMPG <= 30)
printf(" Poor Average");
else if(CMPG < 30)
printf(" Very Poor Average");
break;
case 3: break;
default : printf(" Invalid option");
break;
}
}while(choice != 3);
return 0;
}
Java
import java.util.*;
class MilesPerGallon
{
public static void main (String[] args)
{
int miles,gallons,choice;
miles = gallons = choice = 0;
double CMPG,MPG = 0;
char Continue = 'o';
CMPG = 0;
Scanner scan = new Scanner(System.in);
do
{
//menu
System.out.println(" 1.Enter trip data");
System.out.println(" 2.Show Eco Score");
System.out.println(" 3.Exit program");
System.out.println(" Enter your choice : ");
choice = scan.nextInt();
switch(choice)
{
case 1:
CMPG = 0;
do
{
System.out.println(" Enter # miles travelled in the past 5 minutes");
miles = scan.nextInt();
System.out.println(" Enter gallons of gas consumed in the past 5 minutes");
gallons = scan.nextInt();
MPG = (double)miles/gallons;
CMPG = CMPG + MPG;
System.out.printf(" Distance = %d gallons = %d MPG = %.2f Commulative MPG = %.2f ",miles,gallons,MPG,CMPG);
System.out.println(" Do you want to add more data in 5 minute increments <y-yes, n-no>");
Continue = scan.next().charAt(0);
if(Continue == 'n' || Continue == 'N')
break;
}while(Continue == 'y' || Continue == 'Y');
break;
case 2:
System.out.printf(" Distance = %d gallons = %d Commulative MPG = %.2f ",miles,gallons,CMPG);
if(CMPG > 100)
System.out.println(" Excellent Average");
else if(CMPG >= 50 && CMPG <= 100)
System.out.println(" Very Good Average");
else if(CMPG >= 30 && CMPG <= 50)
System.out.println(" Good Average");
else if(CMPG >= 10 && CMPG <= 30)
System.out.println(" Poor Average");
else if(CMPG < 30)
System.out.println(" Very Poor Average");
break;
case 3: break;
default : System.out.println(" Invalid option");
break;
}
}while(choice != 3);
}
}
Output: