Summary: This C program stores and analyzes data for a fleet of five vehicles. It calculates and displays each vehicle's details along with the maximum distance each can travel based on current fuel and mileage. It also identifies the vehicle capable of traveling the farthest.
#include <stdio.h>
#include <string.h>
// Define the Vehicle structure
struct Vehicle {
char registrationNumber[16];
char ownerName[51];
char vehicleType[21];
float mileage;
float fuelCapacity;
float currentFuel;
};
// Function to calculate max distance
float calculateMaxDistance(struct Vehicle v) {
return v.mileage * v.currentFuel;
}
// Function to display vehicle details
void displayVehicle(struct Vehicle v) {
printf("Registration Number:\t%s\n", v.registrationNumber);
printf("Owner Name:\t\t%s\n", v.ownerName);
printf("Vehicle Type:\t\t%s\n", v.vehicleType);
printf("Mileage (km/l):\t\t%.2f\n", v.mileage);
printf("Fuel Capacity (l):\t%.2f\n", v.fuelCapacity);
printf("Current Fuel (l):\t%.2f\n", v.currentFuel);
printf("Maximum Distance:\t%.2f km\n", calculateMaxDistance(v));
printf("--------------------------------------\n");
}
int main() {
// Initialize data for 5 vehicles
struct Vehicle fleet[5] = {
{"AP09AB1234", "Ramesh Kumar", "Car", 18.5, 45, 30},
{"TS10CD5678", "Sneha Reddy", "Truck", 6.0, 120, 80},
{"AP29XY7890", "Aamir Hussain", "Van", 12.0, 60, 40},
{"TS07MN3456", "Divya Sharma", "Car", 16.0, 50, 25},
{"AP31PQ1122", "Arjun Patel", "Truck", 5.5, 130, 100}
};
printf("Fleet Vehicle Details:\n");
printf("======================================\n");
float maxDistance = 0;
int maxIndex = 0;
// Display all vehicle details and calculate max distance
for (int i = 0; i < 5; i++) {
displayVehicle(fleet[i]);
float distance = calculateMaxDistance(fleet[i]);
if (distance > maxDistance) {
maxDistance = distance;
maxIndex = i;
}
}
// Print the vehicle that can travel the farthest
printf("\nVehicle that can travel the farthest:\n");
printf("======================================\n");
displayVehicle(fleet[maxIndex]);
return 0;
}
Structure Definition:
A struct Vehicle
is used to group related vehicle information into one unit. This includes strings for registration number, owner name, and vehicle type, as well as float values for mileage, fuel capacity, and current fuel.
Array of Structures:
The fleet of vehicles is stored as an array of struct Vehicle
. This allows easy iteration and access to each vehicle's data.
Functions:
calculateMaxDistance()
: A reusable function that calculates the maximum travel distance using the formula mileage * currentFuel
.displayVehicle()
: A modular function that prints formatted details of a given vehicle. This promotes code reuse and readability.Looping:
A for
loop iterates over the fleet array to display each vehicle's details and compute its maximum distance.
Comparison and Index Tracking:
The program keeps track of the vehicle with the greatest maximum distance using a comparison condition. maxIndex
stores the index of the farthest vehicle for final display.
Modularization:
Separating logic into functions like calculateMaxDistance
and displayVehicle
demonstrates modular design, improving maintainability and readability.