C++, can someone help me with this code? I need to modify it (which is listed be
ID: 3778473 • Letter: C
Question
C++, can someone help me with this code? I need to modify it (which is listed below)
My lab 15 question 2 is listed below:
#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: ";
cin>>c.carMake;
cout<<"Enter car model: ";
cin>>c.carModel;
cout<<"Enter car year model: ";
cin>>c.yearModel;
cout<<"Enter car cost: ";
cin>>c.cost;
return c;
}
void printCar(const Car car){
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;
}
Explanation / Answer
//HEADERS REQUIRED
#include <iostream>
#include <string>
using namespace std;
// Car struct CREATION
struct Car{
string carMake;
string carModel;
int yearModel;
double cost;
}c[3]; //ARRAY VARIBLE FOR STRUCUTRE
//FUNCTION DECLARATIONS
void getCar();
void printCar();
void sortCar(Car []); //PASSING ARRAY
int main()
{
//CALLING FUNCTIONS
getCar();
//printCar();
sortCar(c);
printCar();
return 0;
}
// function defination FOR TAKING VALUES FOR STURUCTURE VARIBLE
void getCar(){
int i;
for(i=0;i<3;i++)
{
cout<<"INPUT FUNCTION BEGINS"<<endl;
cout<<"Enter car"<<i+1<<" make: "<<endl;
cin>>c[i].carMake;
cout<<"Enter car"<<i+1<<" model: "<<endl;
cin>>c[i].carModel;
cout<<"Enter car "<<i+1<<"year model: "<<endl;
cin>>c[i].yearModel;
cout<<"Enter car "<<i+1<<" cost: "<<endl;
cin>>c[i].cost;
cout<<"INPUT FUNCTION CLOSE"<<endl;
}
}
//FUNCTION DEFINATION FOR PRINTING STURUCTRE ARRAY VARIBLE
void printCar(){
int i;
for(i=0;i<3;i++)
{
cout<<"DISPLAY FUNCTION BEGINS"<<endl;
cout<<" car"<<i+1<<" make: "<<endl;
cout<<c[i].carMake<<endl;;
cout<<"car"<<i+1<<" model: "<<endl;
cout<<c[i].carModel<<endl;;
cout<<"car "<<i+1<<"year model: "<<endl;
cout<<c[i].yearModel<<endl;;
cout<<"car "<<i+1<<" cost: "<<endl;
cout<<c[i].cost<<endl;
cout<<"DISPALY FUNCTION ENDS"<<endl;
}
}
//FUNCTION DEFINATION FOR SORTING ARRAY VARIBLE USING YEAR MODEL
void sortCar(Car c[])
{
int a,i,j;
for (i = 0; i < 3; ++i)
{
for (j = i + 1; j < 3; ++j)
{
if (c[i].yearModel > c[j].yearModel)
{
//SWAPPING FOR ASCENDING ORDER GETTING
a = c[i].yearModel;
c[i].yearModel = c[j].yearModel;
c[j].yearModel = a;
}
}
}
}
Output :
INPUT FUNCTION BEGINS
Enter car1 make:
AAAA
Enter car1 model:
SSSS
Enter car 1year model:
2013
Enter car 1 cost:
400000
INPUT FUNCTION CLOSE
INPUT FUNCTION BEGINS
Enter car2 make:
HHHH
Enter car2 model:
NNNNN
Enter car 2year model:
2012
Enter car 2 cost:
7777777
INPUT FUNCTION CLOSE
INPUT FUNCTION BEGINS
Enter car3 make:
UUUUU
Enter car3 model:
IIIII
Enter car 3year model:
2016
Enter car 3 cost:
8888888
INPUT FUNCTION CLOSE
DISPLAY FUNCTION BEGINS
car1 make:
AAAA
car1 model:
SSSS
car 1year model:
2012
car 1 cost:
400000
DISPALY FUNCTION ENDS
DISPLAY FUNCTION BEGINS
car2 make:
HHHH
car2 model:
NNNNN
car 2year model:
2013
car 2 cost:
7.77778e+006
DISPALY FUNCTION ENDS
DISPLAY FUNCTION BEGINS
car3 make:
UUUUU
car3 model:
IIIII
car 3year model:
2016
car 3 cost:
8.88889e+006
DISPALY FUNCTION ENDS
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.