Write code in C Using the source code below declare an array
Write code in C++
Using the source code below, declare an array called arrCar of type Car and dimension SIZE where Size is a constant with a value of 3.
Modify your main function to call the getCar() function to populate each element of the array arrCar declared above.
Note that there is NO modification needed for getCar() function.
Also, modify your main function to print each element of the array by calling printCar() in a loop to print each element of the array.
Then add a function called sortCar() such that this function will accept an array of type Car, and then sort this array based on yearModel in ascending order. Implement the sortCar() function using any sort algorithm of your own choice.
Test your output by making sure that output is printed ascending by yearModel.
Source code below:
// ConsoleApplication31.cpp : Defines the entry point for the console application.
//
#include \"stdafx.h\"
#include <iostream>
#include <string>
using namespace std;
// Car struct
struct Car {
string carMake;
string carModel;
int yearModel;
double cost;
};
// function prototype
Car getCar();
void printCar(const Car car);
int main() {
Car c1 = { \"Ford\", \"Mustang\", 2968, 20300 };
printCar(c1);
Car c2 = getCar();
printCar(c2);
return 0;
}
// function defination
Car getCar() {
Car c;
cout << \"Enter car make: \"; // Ask user to input car make
cin >> c.carMake;
cout << \"Enter car model: \"; // Ask user to input car model
cin >> c.carModel;
cout << \"Enter car year model: \"; // Ask user to input car year
cin >> c.yearModel;
cout << \"Enter car cost: \"; // Ask user to input car cost
cin >> c.cost;
return c;
}
void printCar(const Car car) {
cout << endl;
cout << \"Car Make: \" << car.carMake << endl;
cout << \"Car Model: \" << car.carModel << endl;
cout << \"Car Year Model: \" << car.yearModel << endl;
cout << \"Car Cost: \" << car.cost << endl;
cout << endl;
}
Solution
Here is the revised version for you:
// ConsoleApplication31.cpp : Defines the entry point for the console application.
//#include \"stdafx.h\"
#include <iostream>
#include <string>
using namespace std;
// Car struct
struct Car {
string carMake;
string carModel;
int yearModel;
double cost;
};
// function prototype
Car getCar();
void printCar(const Car car);
int main() {
//Using the source code below, declare an array called arrCar of type Car and dimension
//SIZE where Size is a constant with a value of 3.
const int SIZE = 3;
Car arrCar[SIZE];
//Modify your main function to call the getCar() function to populate each element
//of the array arrCar declared above.
for(int i = 0; i < SIZE; i++)
arrCar[i] = getCar();
/*Car c1 = { \"Ford\", \"Mustang\", 2968, 20300 };
printCar(c1);
Car c2 = getCar();
printCar(c2);*/
//Also, modify your main function to print each element of the array by calling printCar()
//in a loop to print each element of the array.
for(int i = 0; i < SIZE; i++)
printCar(arrCar[i]);
//Test your output by making sure that output is printed ascending by yearModel.
return 0;
}
// function defination
Car getCar() {
Car c;
cout << \"Enter car make: \"; // Ask user to input car make
cin >> c.carMake;
cout << \"Enter car model: \"; // Ask user to input car model
cin >> c.carModel;
cout << \"Enter car year model: \"; // Ask user to input car year
cin >> c.yearModel;
cout << \"Enter car cost: \"; // Ask user to input car cost
cin >> c.cost;
return c;
}
//Then add a function called sortCar() such that this function will accept an array of type Car,
//and then sort this array based on yearModel in ascending order. Implement the sortCar()
//function using any sort algorithm of your own choice.
void sortCar(Car arr[], int size)
{
for(int i = 0; i < size; i++)
for(int j = 0; j < size-i-1; j++)
{
if(arr[j].yearModel > arr[j+1].yearModel)
{
Car temp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = temp;
}
}
}
void printCar(const Car car) {
cout << endl;
cout << \"Car Make: \" << car.carMake << endl;
cout << \"Car Model: \" << car.carModel << endl;
cout << \"Car Year Model: \" << car.yearModel << endl;
cout << \"Car Cost: \" << car.cost << endl;
cout << endl;
}


