Working on an assignment where I have to :
a. Create three array of 12+ students records including ID’s, student names, and
the corresponding e-mail addresses,– student ID’s not sorted in order.
b. Sort the above data by student ID’s using bubble sorting or selection sorting.
c. Binary Search five ID’s which are from the sorted array and a 6th ID which is not from the array.
I have everything done but the sort only sorts out the student ID and I can't seem to understand the logic on how to get the bubble sort to sort the names and email variables in the "studentArray" as well.
#include <iostream>
#include <iomanip>
#include <string>
#include <cmath>
using namespace std;
struct Student {
string name;
int stuID;
string email;
};
void showInfo(Student *studentArray, int stuCount) {
cout << "Student Info: " << endl << endl <<
"\t\tStudent Name" << "\t\tStudent ID" << "\t\tStudent Email" << endl << endl;
for (int i = 0; i < stuCount; i++)
{
cout << "\t\t" << studentArray[i].name << "\t\t" << studentArray[i].stuID << "\t\t\t" << studentArray[i].email << endl;
}
cout << endl;
}
void displayResult(Student studentArray[], int result, int studCount){
if (result==-1){
cout << "Student Id was Not Found"<<endl<< endl;
}
else
cout << "Student Name: " << studentArray[result].name<<endl
<< "Student ID: " <<studentArray[result].stuID<< endl
<< "Student Email: "<< studentArray[result].email<<endl<< endl;
}
int binarySearch(Student studentArray[], int stuCount, int idSearch)
{
int first = 0,
last = idSearch - 1,
middle,
position = -1;
bool found = false;
while (!found && first <= last)
{
middle = (first + last) / 2;
if (studentArray[middle].stuID == stuCount)
{
found = true;
position = middle;
}
else if (studentArray[middle].stuID > stuCount)
last = middle - 1;
else
first = middle + 1;
}
return position;
}
void bubblesortArray(Student *studentArray[], int stuCount)
{
bool swap;
int temp;
do{
swap = false;
for (int count = 0; count < (stuCount - 1); count++)
{
if (studentArray[count] > studentArray[count + 1])
{
temp = studentArray[count];
studentArray[count] = studentArray[count + 1];
studentArray[count + 1] = temp;
swap = true;
}
}
} while (swap);
}
void recordSearch(Student studentArray[], int stuCount)
{
int result;
int idSearch;
bool isChar=false;
for (int i=0; i<13;i++)
{
if (i<6)
{
cout << "Binary Search: Enter ID Number: ";
cin >> idSearch;
}
else if (idSearch>12||idSearch<0)
{
i--;
}
if(isChar)
{
cout<< "ERROR: Please enter valid number:";
}
result=binarySearch(studentArray,idSearch,stuCount);
displayResult(studentArray,result,idSearch);
}
}
int main() {
Student studentArray[13];
studentArray[0].name = "Bob McBoberston";
studentArray[0].stuID = 12;
studentArray[0].email = "BMcboberts@txstate.edu";
studentArray[1].name = "Shelby Donald";
studentArray[1].stuID = 1;
studentArray[1].email = "SDonald@txstate.edu";
studentArray[2].name = "Ronald Mcdonald";
studentArray[2].stuID = 11;
studentArray[2].email = "RMcdonald@txstate.edu";
studentArray[3].name = "Dick Cheney";
studentArray[3].stuID = 2;
studentArray[3].email = "DCheney@txstate.edu";
studentArray[4].name = "Ben Dover";
studentArray[4].stuID = 10;
studentArray[4].email = "BDover@txstate.edu";
studentArray[5].name = "Ash Katchum";
studentArray[5].stuID = 3;
studentArray[5].email = "AKatchum@txstate.edu";
studentArray[6].name = "Morty Smith";
studentArray[6].stuID = 9;
studentArray[6].email = "MSmith@txstate.edu";
studentArray[7].name = "Rick Sanchez";
studentArray[7].stuID = 4;
studentArray[7].email = "RSanchez@txstate.edu";
studentArray[8].name = "Johnny Bravo";
studentArray[8].stuID = 8;
studentArray[8].email = "JBravo@txstate.edu";
studentArray[9].name = "Tom N. Jerry";
studentArray[9].stuID = 5;
studentArray[9].email = "Tnjerry@txstate.edu";
studentArray[10].name = "Fred Flinstone";
studentArray[10].stuID = 7;
studentArray[10].email = "FFlinstone@emial.com";
studentArray[11].name = "Son Goku";
studentArray[11].stuID = 6;
studentArray[11].email = "sGoku@txstate.edu";
studentArray[12].name = "Johnny Test";
studentArray[12].stuID = 00;
studentArray[12].email = "JTest@txstate.edu";
int stuCount = 13;
showInfo(studentArray, stuCount);
bubblesortArray(studentArray,stuCount);
showInfo(studentArray,stuCount);
recordSearch(studentArray,stuCount);
return 0;
}
temp = studentArray[count].stuID;
studentArray[count].stuID = studentArray[count + 1].stuID;
studentArray[count + 1].stuID = temp;
You are swapping only IDs. Swap the whole Student objects:
temp = studentArray[count];
studentArray[count] = studentArray[count + 1];
studentArray[count + 1] = temp;
(you'll need to change temp
type obviously).
That's basically the main point of using struct
s and class
es — to treat them as a whole.