Hello! I'm trying to sort an array of structure-based one of the structure variable in Alphabetical order..
I want to sort all the rest of the structure data belonging to the 'make' variable.. That is;
> Sort 'make' and print it out along with it's contents
> OR Sort Everything alphabetically depending on the 'make'
Here is my Code;
#include <iostream>
#include <string>
#include <cstring>
usingnamespace std;
struct Date {
int day;
int year;
string month;
};
struct Vehicle {
string make;
string model;
int engCapacity;
string color;
Date DOR;
};
void showData(Vehicle ve[5]);//shows contents of data input by user
Vehicle inputData (); //gets input from user
void sortByMake(Vehicle ve[5]); // sort arrays of structure by Car Make
int main()
{
Vehicle v[5];
//Getting Input from user
for (int i=0;i<5;i++)
{
v[i] = inputData(); //Input unsorted e.g car make suzuki, then honda, the tesla etc
}
//Displaying the contents of Input Array
cout<<"\n\nCar Particulars are : \n";
showData(v);
//Diplaying the Contents of array of structure after sorting
cout<<"\n\nCar Particulars after sorting are : \n";
sortByMake(v); // you have to write the code for sorting by car make in this function
}
void showData (Vehicle ve[5])
{
for(int i=0;i<5;i++)
{
cout<<"Make : "<<ve[i].make <<"\nModel : "<<ve[i].model<<"\nEngine : "<<ve[i].engCapacity <<"\nColor :"<<ve[i].color;
cout<<"\nDate of Regtration "<<ve[i].DOR.day<<ve[i].DOR.month<<ve[i].DOR.year<<endl;
}
}
Vehicle inputData () {
Vehicle car;
cout<<"Enter Data For Car : ";
cout<<"Make : ";
cin>>car.make;
cout<<"Model : ";
cin>>car.model;
cout<<"Engine Capacity : ";
cin>>car.engCapacity;
cout<<"Color : ";
cin>>car.color;
cout<<"Day of Reg : ";
cin>>car.DOR.day;
cout<<"Year of Reg : ";
cin>>car.DOR.year;
cout<<"Month of Reg : ";
cin>>car.DOR.month;
return car;
}
void sortByMake(Vehicle ve[5])
{
//Write the code for sorting structure of array, input by user by Car Make in Ascending Order
Vehicle temp;
for(int(i)=0;i<5;i++)
{
for(int(j)=i+1;j<5;j++)
{
if (ve[i].make>ve[i+1].make)
{ // True, in this case, because b is less than j on the ascii table.
temp.make=ve[i].make;
temp.model=ve[i].model;
temp.engCapacity=ve[i].engCapacity;
temp.color=ve[i].color;
temp.DOR.day=ve[i].DOR.day;
temp.DOR.year=ve[i].DOR.year;
temp.DOR.month=ve[i].DOR.month;
ve[i].make=ve[j].make;
ve[i].model=ve[j].model;
ve[i].engCapacity=ve[j].engCapacity;
ve[i].color=ve[j].color;
ve[i].DOR.day=ve[j].DOR.day;
ve[i].DOR.year=ve[j].DOR.year;
ve[i].DOR.month=ve[j].DOR.month;
ve[j].make=temp.make;
ve[j].model=temp.model;
ve[j].engCapacity=temp.engCapacity;
ve[j].color=temp.color;
ve[j].DOR.day=temp.DOR.day;
ve[j].DOR.year=temp.DOR.year;
ve[j].DOR.month=temp.DOR.month;
}
}
}
showData(ve);
}
Deadline within 1 hour
It's one of the questions from the final exams of a friend, i tried what i knew but it seems not to cout anything after 'Contents of Array After Sorting'
Hello! I'm trying to sort an array of structure-based one of the structure variable in Alphabetical order..
I want to sort all the rest of the structure data belonging to the 'make' variable.. That is;
> Sort 'make' and print it out along with it's contents
> OR Sort Everything alphabetically depending on the 'make'
Here is my Code;
#include <iostream>
#include <string>
#include <cstring>
usingnamespace std;
struct Date {
int day;
int year;
string month;
};
struct Vehicle {
string make;
string model;
int engCapacity;
string color;
Date DOR;
};
void showData(Vehicle ve[5]);//shows contents of data input by user
Vehicle inputData (); //gets input from user
void sortByMake(Vehicle ve[5]); // sort arrays of structure by Car Make
int main()
{
Vehicle v[5];
//Getting Input from user
for (int i=0;i<5;i++)
{
v[i] = inputData(); //Input unsorted e.g car make suzuki, then honda, the tesla etc
}
//Displaying the contents of Input Array
cout<<"\n\nCar Particulars are : \n";
showData(v);
//Diplaying the Contents of array of structure after sorting
cout<<"\n\nCar Particulars after sorting are : \n";
sortByMake(v); // you have to write the code for sorting by car make in this function
}
void showData (Vehicle ve[5])
{
for(int i=0;i<5;i++)
{
cout<<"Make : "<<ve[i].make <<"\nModel : "<<ve[i].model<<"\nEngine : "<<ve[i].engCapacity <<"\nColor :"<<ve[i].color;
cout<<"\nDate of Regtration "<<ve[i].DOR.day<<ve[i].DOR.month<<ve[i].DOR.year<<endl;
}
}
Vehicle inputData () {
Vehicle car;
cout<<"Enter Data For Car : ";
cout<<"Make : ";
cin>>car.make;
cout<<"Model : ";
cin>>car.model;
cout<<"Engine Capacity : ";
cin>>car.engCapacity;
cout<<"Color : ";
cin>>car.color;
cout<<"Day of Reg : ";
cin>>car.DOR.day;
cout<<"Year of Reg : ";
cin>>car.DOR.year;
cout<<"Month of Reg : ";
cin>>car.DOR.month;
return car;
}
void sortByMake(Vehicle ve[5])
{
//Write the code for sorting structure of array, input by user by Car Make in Ascending Order
Vehicle temp;
for(int(i)=0;i<5;i++)
{
for(int(j)=i+1;j<5;j++)
{
if (ve[i].make>ve[i+1].make)
{ // True, in this case, because b is less than j on the ascii table.
temp.make=ve[i].make;
temp.model=ve[i].model;
temp.engCapacity=ve[i].engCapacity;
temp.color=ve[i].color;
temp.DOR.day=ve[i].DOR.day;
temp.DOR.year=ve[i].DOR.year;
temp.DOR.month=ve[i].DOR.month;
ve[i].make=ve[j].make;
ve[i].model=ve[j].model;
ve[i].engCapacity=ve[j].engCapacity;
ve[i].color=ve[j].color;
ve[i].DOR.day=ve[j].DOR.day;
ve[i].DOR.year=ve[j].DOR.year;
ve[i].DOR.month=ve[j].DOR.month;
ve[j].make=temp.make;
ve[j].model=temp.model;
ve[j].engCapacity=temp.engCapacity;
ve[j].color=temp.color;
ve[j].DOR.day=temp.DOR.day;
ve[j].DOR.year=temp.DOR.year;
ve[j].DOR.month=temp.DOR.month;
}
}
}
showData(ve);
}
Deadline within 1 hour
It's one of the questions from the final exams of a friend, i tried what i knew but it seems not to cout anything after 'Contents of Array After Sorting'
So, just to be clear - you're trying to help your friend cheat on his final exams?