You are on page 1of 10

Answer:

Code:

// Used for input & output


#include <stdio.h>

// Function to print lowest mileage, cheapest & oldest car on a lot


void O_Lot_Info_8(float database[][5])
{
// Store mileage of first car as a lowest
float mileage = database[0][2];
// Store year of first car as a oldest
float year = database[0][3];
// Store mileage of first car as a cheapest
float price = database[0][4];

// Variable for loop counter


int i;

// Loop continue till 4th car


for(i = 1; i < 4; i++)
{
// If mileage is greater than next car's mileage
if(mileage > database[i][2])
{
// Change mileage to next mileage
mileage = database[i][2];
}

// If year is greater than next car's year


if(year > database[i][3])
{
// Change year to next year
year = database[i][3];
}

// If price is greater than next car's price


if(price > database[i][4])
{
// Change price to next price
price = database[i][4];
}
}

// Print all the information

printf("\n<<<<<< General information about cars on a lot: >>>>>>\n\n");


printf(">> The lowest mileage on a lot: %0.0f km\n", mileage);
printf(">> The cheapest car on a lot: %0.0f KD\n", price);
printf(">> The oldest car on a lot: %0.0f\n\n", year);
}

// Function to find the discounted price


float O_Car_Discount_8(float mileage, float year, float price)
{
// If mileage is less than 100000
if(mileage < 100000)
{
// If year is less than 2010
if(year < 2010)
{
// Find discounted price & return
return price * (1 - 0.07);
}
// If year is greater than 2010 & less than 2015
else if(year >= 2010 && year <= 2015)
{
// Find discounted price & return
return price * (1 - 0.05);
}
// If year is greater than 2015
else
{
// Find discounted price & return
return price * (1 - 0.03);
}
}
// If mileage is equal & greater than 100000
else
{
// If year is less than 2010
if(year < 2010)
{
// Find discounted price & return
return price * (1 - 0.2);
}
// If year is greater than 2010 & less than 2015
else if(year >= 2010 && year <= 2015)
{
// Find discounted price & return
return price * (1 - 0.12);
}
// If year is greater than 2015
else
{
// Find discounted price & return
return price * (1 - 0.1);
}
}
}

// Function to print car infromation


void O_Print_Car_8(float database[][5])
{
// Variable to store id
int id;

printf("\n\n<<<<<< Detailed view of information about the cars


>>>>>>\n\n");

// Infinite loop till user gives 999 for exit


for(;;)
{
// Take id from user
printf("\nPlease, enter ID of a car to view (or 999 to exit): ");
scanf("%d",&id);

// If id is 9999
if(id == 999)
// Exit from loop
break;
// If id is greater than 5
else if(id > 4)
// Print error
printf("Wrong input! Please, try again!\n");
// If is is valid
else
{
printf("******\n");
// Print car year
printf("Car info: %0.0f ", database[id-1][3]);
// If car type is 10
if(database[id-1][0] == 10)
{
printf("Hatchback\n");
}
// If car type is 20
else if(database[id-1][0] == 20)
{
printf("Sedan\n");
}
// If car type is 30
else if(database[id-1][0] == 30)
{
printf("SUV\n");
}
// If car type is 40
else if(database[id-1][0] == 40)
{
printf("Minivan\n");
}

// Print engine size


printf("Engine Size: %0.1fL\n",database[id-1][1]);
// Print mileage
printf("Mileage: %0.0f KM\n",database[id-1][2]);
// Print original price
printf("Original Price: %0.0fKD. ",database[id-1][4]);
// Print discounted price
printf("Discounted Price:
%0.0fKD\n",O_Car_Discount_8(database[id-1][2], database[id-1][3],
database[id-1][4]));
printf("******\n\n");
}
}
}

// Driver program
int main()
{
// 2D array to contain car database
float database[4][5];
// Loop varibales
int i, j;
printf("*** EGAILA CAR SALES AGENCY DATABASE ***\n\n");

printf("WELCOME TO SYSTEM,\n\n");

// Loop from 0 to 3
for(i = 0; i < 4; i++)
{
// Take car information
printf("Please enter body type, engine size, mileage, year and price of
the car #%d >> ",i + 1);
scanf("%f %f %f %f
%f",&database[i][0],&database[i][1],&database[i][2],&database[i][3],&databa
se[i][4]);

// If car type not 10, 20, 30 & 40


if(database[i][0] != 10 && database[i][0] != 20 && database[i][0] != 30
&& database[i][0] != 40)
{
// Print error message
printf("Wrong vehicle type, please try again!\n");
i--;
}
}

// Call function to print lot info


O_Lot_Info_8(database);

printf("<<<<<< List of cars available on lot: >>>>>>\n\n");

printf("ID# \t Year \t Price\n");


// Loop for each car for print List of cars
for(i = 0; i < 4; i++)
{
// Print year & price
printf("%d \t %0.0f \t %0.0f\n",i + 1, database[i][3], database[i][4]);
}

// Call function to print detailed car information


O_Print_Car_8(database);

printf("\nStudent Name: Abdullah al Abdullah, ID: 12345, Section:


O1\n");

return 0;
}
output:

You might also like