Write a C program which uses a structure type that stores data records for a car
ID: 3828958 • Letter: W
Question
Write a C program which uses a structure type that stores data records for a car, as described in the Requirements below. im using LYNX
Requirements:
Define a global structure and name it as Car.
This structure has these member variables:
· Make (string), model (string), year (int), mileage (double), original_price (double), and
· sale_price(double) o sale_price will be calculated based on original_price and mileage.
Declare two variables from structure car and call it car_number1 and car_number2.
Write a function called get_input, which is responsible to
· Get the input values for the member variables of car_number1 and car_number2
· For sale_price enter zero. This value will be updated later by function price_calculation
· This function uses call by reference for car_number1 and car_number2
· For string member variables use the correct input method
Write a function called price_calculation which gets car_number1 and car_number2 as input (call by reference) and update the sale_price.
· If the car mileage is greater than 70,000 miles then the sale_price is 70% of the original_price. Otherwise the sale_price is 85% of the original_price.
Write a function called print_output which is responsible
· To print the information for each car separately (including the update value of sale_price)
· This function should print the output based on alphabet order of car’s make.
A sample run of your program for one item should look like below:
Enter the information for the first car
Enter the car’s make: Toyota
Enter the car’s model: Camry
Enter the car’s year: 2000
Enter the car’s mileage: 110000
Enter the car’s original_price: 7000$
Enter the car’s sale_price:0;
Enter the information for the second car
Enter the car’s make: Ford
Enter the car’s model: Mustang
Enter the car’s year: 2014
Enter the car’s mileage: 40000
Enter the car’s original_price: 15000$
Enter the car’s sale_price:0;
Check the car’s information including the sale_price (ordered alphabetically)
Ford Mustang
year: 2014
mileage: 40000
sale_price: 12,750$
Toyota Camry
year: 2000
mileage: 110000
sale_price:4,900$
Compile your program using g++ -Wall program9.cpp -o output
Explanation / Answer
#include <stdio.h>
#include <string.h>
struct Car
{
char make[20];
char model[20];
int year;
double mileage;
double original_price;
double sale_price;
};
void print_output(struct Car *car_number1, struct Car *car_number2)
{
if(strcmp(car_number1->make, car_number2->make)>0){
printf("%s %s ", car_number2->make ,car_number2->model);
printf("year: %d ",car_number2->year);
printf("mileage: %.0lf ",car_number2->mileage);
printf("sale_price: %.0lf$ ",car_number2->sale_price);
printf("%s %s ", car_number1->make ,car_number1->model);
printf("year: %d ",car_number1->year);
printf("mileage: %.0lf ",car_number1->mileage);
printf("sale_price: %.0lf$ ",car_number1->sale_price);
}
else{
printf("%s %s ", car_number1->make ,car_number1->model);
printf("year: %d ",car_number1->year);
printf("mileage: %.0lf ",car_number1->mileage);
printf("sale_price: %.0lf$ ",car_number1->sale_price);
printf("%s %s ", car_number2->make ,car_number2->model);
printf("year: %d ",car_number2->year);
printf("mileage: %.0lf ",car_number2->mileage);
printf("sale_price: %.0lf$ ",car_number2->sale_price);
}
}
void price_calculation(struct Car *car_number1, struct Car *car_number2)
{
if(car_number1->mileage > 70000)
{
car_number1->sale_price = ((double)70/100) * car_number1->original_price;
}
else
{
car_number1->sale_price = ((double)85/100) * car_number1->original_price;
}
if(car_number2->mileage > 70000)
{
car_number2->sale_price = ((double)70/100) * car_number2->original_price;
}
else
{
car_number2->sale_price = ((double)85/100) * car_number2->original_price;
}
}
void get_input(struct Car *car_number1, struct Car *car_number2)
{
printf("Enter the information for the first car ");
printf("Enter the cars make: ");
scanf("%s", &car_number1->make);
printf("Enter the cars model: ");
scanf("%s", &car_number1->model);
printf("Enter the cars year: ");
scanf("%d", &car_number1->year);
printf("Enter the cars mileage: ");
scanf("%lf", &car_number1->mileage);
printf("Enter the cars original_price: ");
scanf("%lf", &car_number1->original_price);
printf("Enter the cars sale_price: ");
scanf("%lf", &car_number1->sale_price);
printf("Enter the information for the second car ");
printf("Enter the cars make: ");
scanf("%s", &car_number2->make);
printf("Enter the cars model: ");
scanf("%s", &car_number2->model);
printf("Enter the cars year: ");
scanf("%d", &car_number2->year);
printf("Enter the cars mileage: ");
scanf("%lf", &car_number2->mileage);
printf("Enter the cars original_price: ");
scanf("%lf", &car_number2->original_price);
printf("Enter the cars sale_price: ");
scanf("%lf", &car_number2->sale_price);
}
int main()
{
struct Car car_number1;
struct Car car_number2;
get_input(&car_number1, &car_number2);
price_calculation(&car_number1, &car_number2);
print_output(&car_number1, &car_number2);
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.