Update Q3.C

This commit is contained in:
2024-10-22 21:53:34 -07:00
parent cc95aadaea
commit 94d003bf68

18
Q3.C
View File

@@ -1,17 +1,17 @@
#include <stdio.h> #include <stdio.h>
// Struct for Car
typedef struct Car { typedef struct Car {
char model[50]; char model[50];
int year; int year;
double price; double price;
} Car; } Car;
// Print details of passed car pointer
void display_details(Car *car) { void display_details(Car *car) {
printf("Model: %s\n", car->model); printf("Model: %s\n", car->model);
printf("Year: %d\n", car->year); printf("Year: %d\n", car->year);
printf("Price: %0.02f\n", car->price); printf("Price: $%0.02f\n", car->price);
} }
@@ -28,6 +28,7 @@ Car *find_lowest_price(Car arr[], int len) {
return lowest; return lowest;
} }
// Returns double of average price of the cars
double find_average_price(Car arr[], int len) { double find_average_price(Car arr[], int len) {
double average = 0; double average = 0;
for(int i = 0; i < len; i++) { for(int i = 0; i < len; i++) {
@@ -37,15 +38,24 @@ double find_average_price(Car arr[], int len) {
} }
int main() { int main() {
// Cars to test function and struct
Car c1 = {"Honda Pilot", 2016, 20000.50}; Car c1 = {"Honda Pilot", 2016, 20000.50};
Car c2 = {"Chevy Tahoe", 2002, 7500.25}; Car c2 = {"Chevy Tahoe", 2002, 7500.25};
Car c3 = {"Lamborghini Aventador", 2015, 450000}; Car c3 = {"Lamborghini Aventador", 2015, 450000};
Car arr[] = {c1, c2, c3}; Car arr[] = {c1, c2, c3};
// Display all car details
for(int i = 0; i < sizeof(arr) / sizeof(arr[0]); i++) {
display_details(&arr[i]);
}
//Print car with lowest price
printf("\nCar with the lowest price: ");
display_details(find_lowest_price(arr, 3)); display_details(find_lowest_price(arr, 3));
printf("Average price: %0.02f\n", find_average_price(arr, 3)); //Print average price of the cars
printf("\nAverage price: $%0.02f\n", find_average_price(arr, 3));
return 0; return 0;
} }