-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path28 Student Record Sorting.CPP
More file actions
75 lines (61 loc) · 1.72 KB
/
28 Student Record Sorting.CPP
File metadata and controls
75 lines (61 loc) · 1.72 KB
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
//Program to record student's names and then sort by first name and then last name.
#include <iostream.h>
#include <conio.h>
#include <stdio.h>
#include <string.h>
struct Student {
char firstName[15];
char lastName[15];
int roll;
};
void sort (Student s[], int total) {
int minIndex = 0, minValue;
//Sort Last Name
for (int j = 0; j < total; ++j) {
minIndex = j;
for (int i = j; i < total; ++i) {
if (strcmp(s[i].lastName, s[minIndex].lastName) < 0) {
minIndex = i;
}
}
Student temp = s[minIndex];
s[minIndex] = s[j];
s[j] = temp;
}
//Sort First Name
for (j = 0; j < total; ++j) {
minIndex = j;
for (int i = j; i < total; ++i) {
if (strcmp(s[i].firstName, s[minIndex].firstName) < 0) {
minIndex = i;
}
}
Student temp = s[minIndex];
s[minIndex] = s[j];
s[j] = temp;
}
}
void main() {
clrscr();
Student pupils[50];
int total = 0;
cout << "Enter total students in class: ";
cin >> total;
for (int i = 0; i < total; ++i) {
cout << "\nEnter Roll: ";
cin >> pupils[i].roll;
cout << "Enter first name (" << pupils[i].roll << "): ";
gets(pupils[i].firstName);
cout << "Enter last name (" << pupils[i].roll << "): ";
gets(pupils[i].lastName);
}
sort(pupils,total);
clrscr();
cout << "Sorted students by first name and then last name: \n\n";
cout << "\tRoll\t\tFirst Name\t\tLast Name\n"
<< "----------------------------------------------------------------------------\n";
for (i = 0; i < total; ++i) {
cout << "\t" << pupils[i].roll << "\t\t" << pupils[i].firstName << "\t\t" << pupils[i].lastName << "\n";
}
getch();
}