You are on page 1of 14

Solved

exercise III
A car has a VIN (int), Brand (array of characters), power(float), price (float). We are
storing up to 10 cars in a garage
• Write the corresponding structures
• Write a function to input a car
• Write a function to fill the garage = insert the car in the garage
• Write a function to output the information of a car
• Write a function to search for a car by its VIN
• Write a function that returns the VIN of the most expensive car
• Sort the garage in ascending order by the power
• Sell a car by its VIN
• Buy a car and insert in the sorted garage
• Write the main
#include<iostream> //initialize the garage. It
using namespace std; is used to create an empty
#include<string> garage
struct car void initialize(garage &Gar)
{
int VIN; {
string brand; Gar.numc = 0;
float power, price; }
};

#define N 10 //test if the garage is empty


struct garage bool isEmpty(garage Gar)
{ {
car G[N];
return (Gar.numc == 0);
int numc;
} }
//test if the garage is full
bool isFull (garage &G)
{
return (Gar.num == N);
}

//Input a car
void inputCar(car &c)
{
cout<<“Input the car info”;
cin >> c.VIN >> c.brand;
cin >> c.power >> c.price;
}
//fill the garage
void fillGarage(garage &Gar, car c)
{
if(isFull(Gar))
{
cout <<“Garage full”;
return;
}
Gar.G[Gar.numc]=c;
Gar.G[Gar.numc++] = c;
Gar.numc++;
}
//output the info of a car
void outputCar (car c)
{
cout <<“VIN: “<<c.VIN<<endl;
cout <<“Brand: “<<c.brand<<endl;
cout <<”Power: “<<c.power<<endl;
cout <<”Price: “<<c.price<<endl;
}
//Search for a car by VIN: return the position
int findCar (garage Gar, int vin)
{
int i;
if(isEmpty(Gar))
{
cout << “Empty garage”;
return -1;
}
for(i=0;i<Gar.numc;i++)
{
if(Gar.G[i].VIN == vin)
return i;
}
cout <<“ Car not found”;
return -1;
}
//Return the VIN of the most expensive car
int expensiveCar (garage Gar)
{
int i, indexmax ;
if(isEmpty(Gar))
{
cout << “Empty garage”;
return -1; car carmax = Gar.G[0];
} for(i=1;i<Gar.numc;i++)
if(Gar.G[i].price >= carmax.price)
indexmax = 0; carmax=Gar.G[i];
for(i=1;i<Gar.numc;i++)
return carmax.VIN
{
if(Gar.G[i].price >= Gar.G[indexmax].price)
indexmax = i;
}
return Gar.G[indexmax].VIN;
}
//Sort the garage
void sortGarage(garage &Ga)
{
int i,j;
for(i=0;i<Ga.numc-1;i++)
for(j=i+1; j<=Ga.numc-i-1; j++)
if(Ga.G[j-1].power > Ga.G[j].power)
{
car c = Ga.G[j-1];
Ga.G[j-1] = Ga.G[j];
Ga.G[j] = c;
}
}
//Sell a car by its VIN
void Sell (garage &Ga, int vin)
{
int i;
int p = findCar(Ga, vin);
if(p==-1)
return;
for(i = p; i<Ga.numc-1; i++)
{
Ga.G[i] = Ga.G[i+1];
}
Ga.numc--;
}
//buy a car & insert in the sorted garage
void Buy (garage &Ga, car c)
{
int i,j;
if(isFull (Ga))
{
cout<<“Garage full, cannot add more”;
return;
}

for(i = G.numc-1; i>=0 && c.power < Ga.G[i].power; i--)


{
Ga.G[i+1] = Ga.G[i];//shifting
}

Ga.G[i+1] = c;//insertion
Ga.numc++;
}
//main
int main()
{
Garage autoC; If(n + carauto.numc >10)
initialize(autoC);
cout<<“cannot add”;

car c; While(!isFull(autoc))
int n, i; add a car
do{
cout<<“Input number of cars”;
cin>>n;
}
while(n<=0 || n>N);
for(i=0;i<n;i++)
{
inputCar(c);
fillGarage(autoC, c);
}

//output all the garage


for(i=0;i<autoC.numc;i++)
{
outputCar(autoC.G[i]);
cout<<endl;
}
//search for a car
int v;
cout<<“Input a vin number to search”;
cin>> v;
int p = findCar(autoC, v);
if(p!=-1)
outputCar(autoC.G[p]);

int vexp = expensiveCar(autoC);


cout<<“The VIN of the most expensive car is\n”<<vexp;

//sort
sortGarage(autoC);
//output all the garage
for(i=0;i<autoC.numc;i++)
{
outputCar(autoC.G[i]);
cout<<endl;
}

//sell our most expensive car


Sell(autoC, vexp);
//buy a new car: Input then buy
cout<<“\nInput a new car”;
inputCar(c);
Buy(autoC, c);

//output all the garage


for(i=0;i<autoC.numc;i++)
{
outputCar(autoC.G[i]);
cout<<endl;
}

return 0;
}
//fill the garage
void fillGarage(garage *Gar, car c)
{
if(isFull(*Gar))
{
cout <<“Garage full”;
return;
}
Gar->G[Gar->numc++] = c;
}

//in main
fillGarage(&autoC, c);

You might also like