ASSIGNMENT 1 2. (GasMileage) (30 points) Drivers are often concerned with the mi
ID: 3709220 • Letter: A
Question
ASSIGNMENT 1
2. (GasMileage) (30 points) Drivers are often concerned with the mileage their automobiles get. One driver has kept track of several tankfuls of gasoline by recording the miles driven and gallons used for each tankful.
Develop a C# static function that has two parameters representing the miles driven and gallons used (both as integers) for each tankful. The function will calculate the miles per gallon obtained for each tankful. The data type of each parameter is int. The data type of the miles per gallon obtained for each tankful is double.
In the main method, this function is called for five times. Then, the main method will calculate and then display the combined miles per gallon obtained from 5 calls.
Explanation / Answer
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace GasMileage
{
class MilesPerGallon
{
static void Main()
{
int miles, gallons;
double tot_miles = 0.0;
double tot_galls = 0.0;
double gasmileage = 0.0;
int temp = 1;
while(temp <= 5)
{
Console.WriteLine("Enter the Miles used for the trip {0}: ", temp);
miles = int.Parse(Console.ReadLine());
Console.WriteLine("Enter the Gallons used for the trip {0}: ", temp);
gallons = int.Parse(Console.ReadLine());
tot_miles += miles;
tot_galls += gallons;
gasmileage = tot_miles/tot_galls;
temp++;
Console.WriteLine();
}
Console.WriteLine("Total GasMileage is {0:N} miles per gallon ", gasmileage);
Console.WriteLine();
}
}
}
OUTPUT
Enter the Miles used for the trip 1: 10
Enter the Gallons used for the trip 1: 3
Enter the Miles used for the trip 2: 20
Enter the Gallons used for the trip 2: 3
Enter the Miles used for the trip 3: 30
Enter the Gallons used for the trip 3: 4
Enter the Miles used for the trip 4: 40
Enter the Gallons used for the trip 4: 5
Enter the Miles used for the trip 5: 50
Enter the Gallons used for the trip 5: 6
Total GasMileage is 7.14 miles per gallon