Year 1 Semester 1 C++ project. (Gender Sorting algorithm)
The current Computer Science freshmen are in three groups, A, B and C respectively. The gender ratios in these groups are as shown below.
ICS Group | Male | Female |
---|---|---|
ICS A | 19 | 11 |
ICS B | 16 | 12 |
ICS C | 36 | 19 |
Totals | 71 | 42 |
Assuming the Faculty has decided to merge the three groups into two, balancing the gender ratios as shown below. Develop a perfect shuffle algorithm (coded) that will give the output in the table below.
ICS Group | Male | Female |
---|---|---|
ICS A | 36 | 21 |
ICS B | 35 | 21 |
Totals | 71 | 42 |
- Objects and Classes
- STL library (vectors)
- clone repository
- locate
Release
folder - run
studentSystem.exe
- surname
- Name
- Admission number
- Gender
1.Create
student list
A_students
B_students
-
First, files are read from file (one at a time) and stored into a vector
student list
-
student list
is split into two separate vectorsboys
andgirls
-
push
boy ( n )
fromboys
toA_students
andboy ( n + 1 )
fromboys
toB_students
-
push
girl ( n )
fromgirls
toA_students
andgirl ( n + 1 )
fromgirls
toB_students
-
print
A_students
vector toB_list.csv
-
print
B_students
vector toA_list.csv
// split student list to boys and girls
for (unsigned int i = 0; i < student_list.size(); i++){
if (student_list.at(i).gender == "M"){
boys.push_back(student_list.at(i));
} else {
girls.push_back(student_list)
}
}
//push female(n) to A female (n+1) to B
div = girls.size() % 2;
for (unsigned int i = 0; i<girls.size()-1; i+=2) {
A_students.push_back(girls.at(i));
B_students.push_back(girls.at(i + 1));
if (div > 0 && i == girls.size() - 3) {
A_students.push_back(girls.at(i + 2));
}
}
// printing B students to listA
std::ofstream outFileA;
outFileA.open("listA.csv");
for (unsigned int i = 0; i < listB.size(); i++) {
outFileA <<listB.at(i).numbr<<","
<<listB.at(i).surname<<" "
<<listB.at(i).name<<","
<<listB.at(i).gender
<<std::endl;
}
outFileA.close();