Sort An Array of Structure Based on one of It's Values in Alphabetical Order.

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;

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
#include <iostream>
#include <string>
#include <cstring>

using namespace 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'
Last edited on
Genious28 wrote:

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;

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
#include <iostream>
#include <string>
#include <cstring>

using namespace 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?
Last edited on
Topic archived. No new replies allowed.