Steve3164
asked on
Binary Search
I am trying to do a binary search for the following program. This program reads a txt file and sorts and outputs the student information by either grade, email, last name, or first name.
Using binary search my program is supposed to ask the user to enter a student grade, once the user enters the grade the program outputs the closes student name with grade. For example, if user enters 86 for the grade the program should output Alice Keith has grade of 84.6.
The infile has this information:
Richard,Jones,richars@jone s.com,78.5
Alice,Keith,alicek@yahoo.c om,84.6
Diego,Salazar,ds@hotmail.c om,91.2
Max,Karlov,max@kkk.org,63. 2
Here is my program.
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
struct Student {
string fname;
string lname;
string email;
double grade;
};
Student* list[20];
int main1()
{
fstream inFile("studentlist.txt");
ofstream outFile; //output file stream variable
outFile.open("output10p1.t xt"); //open outfile
//declare variables
char str[128];
int i=0, c;
while (!inFile.eof())
{
inFile.getline(str, 128);
string line = str;
int pos1 = line.find(",");
string fname = line.substr(0,pos1);
int pos2 = line.find(",",pos1+1);
string lname = line.substr(pos1+1,pos2-po s1-1);
int pos3 = line.find(",",pos2+1);
string email = line.substr(pos2+1,pos3-po s2-1);
string grade = line.substr(pos3+1,line.le ngth()-pos 3-1);
double grd = atof(grade.c_str());
list[i] = new Student;
list[i] ->fname = fname;
list[i] ->lname = lname;
list[i] ->email = email;
list[i] ->grade = grd;
i++;
}
// SAVE THE CURRENT VALUE OF THE LOOP COUNTER. IT'S THE STUDENTS COUNT
int count = i;
// loop will keep function until user enters "0"
while (true)
{
//(0 to quit)
cout<<"Which column would you like to sort by (0 to quit) ==> "<<endl;
cout<<"1) First Name"<<endl<<"2) Last Name"<<endl<<"3) Email"<<endl<<"4) Grade"<<endl;
cin>>c;
// if "0" is entered loop will terminate
if (c == 0)
break;
// Loop Runs Til Count == Number Of Students Read
for (i=0; i < count; i++)
{
for (int j=i+1; j < count; j++)
{
switch (c)
{
case 1: if (list[i]->fname > list[j]->fname){
Student* tmp = list[i];
list[i] = list[j];
list[j] = tmp;
break;
}
case 2: if (list[i]->lname > list[j]->lname){
Student* tmp = list[i];
list[i] = list[j];
list[j] = tmp;
break;
}
case 3: if (list[i]->email > list[j]->email){
Student* tmp = list[i];
list[i] = list[j];
list[j] = tmp;
break;
}
case 4: if (list[i]->grade > list[j]->grade){
Student* tmp = list[i];
list[i] = list[j];
list[j] = tmp;
break;
}
}
}
}
cout <<"+-----------+---------- ---+------ ---------- -----+---- ---------- ------+"<< endl;
cout <<"|First Name | Last Name | Email | Grade |"<<endl;
cout <<"+-----------+---------- ---+------ ---------- -----+---- ---------- ------+"<< endl;
// statement to output sorted table
for (i=0; i< count; i++)
{
cout <<list[i]->fname<<"\t | "<<list[i]->lname<<"\t | "<<list[i]->email<<"\t|\t" <<
list[i]->grade <<"\t |"<< endl;
}
cout <<"+-----------+---------- ---+------ ---------- -----+---- ---------- ------+"<< endl;
}
return 0;
}
Using binary search my program is supposed to ask the user to enter a student grade, once the user enters the grade the program outputs the closes student name with grade. For example, if user enters 86 for the grade the program should output Alice Keith has grade of 84.6.
The infile has this information:
Richard,Jones,richars@jone
Alice,Keith,alicek@yahoo.c
Diego,Salazar,ds@hotmail.c
Max,Karlov,max@kkk.org,63.
Here is my program.
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
struct Student {
string fname;
string lname;
string email;
double grade;
};
Student* list[20];
int main1()
{
fstream inFile("studentlist.txt");
ofstream outFile; //output file stream variable
outFile.open("output10p1.t
//declare variables
char str[128];
int i=0, c;
while (!inFile.eof())
{
inFile.getline(str, 128);
string line = str;
int pos1 = line.find(",");
string fname = line.substr(0,pos1);
int pos2 = line.find(",",pos1+1);
string lname = line.substr(pos1+1,pos2-po
int pos3 = line.find(",",pos2+1);
string email = line.substr(pos2+1,pos3-po
string grade = line.substr(pos3+1,line.le
double grd = atof(grade.c_str());
list[i] = new Student;
list[i] ->fname = fname;
list[i] ->lname = lname;
list[i] ->email = email;
list[i] ->grade = grd;
i++;
}
// SAVE THE CURRENT VALUE OF THE LOOP COUNTER. IT'S THE STUDENTS COUNT
int count = i;
// loop will keep function until user enters "0"
while (true)
{
//(0 to quit)
cout<<"Which column would you like to sort by (0 to quit) ==> "<<endl;
cout<<"1) First Name"<<endl<<"2) Last Name"<<endl<<"3) Email"<<endl<<"4) Grade"<<endl;
cin>>c;
// if "0" is entered loop will terminate
if (c == 0)
break;
// Loop Runs Til Count == Number Of Students Read
for (i=0; i < count; i++)
{
for (int j=i+1; j < count; j++)
{
switch (c)
{
case 1: if (list[i]->fname > list[j]->fname){
Student* tmp = list[i];
list[i] = list[j];
list[j] = tmp;
break;
}
case 2: if (list[i]->lname > list[j]->lname){
Student* tmp = list[i];
list[i] = list[j];
list[j] = tmp;
break;
}
case 3: if (list[i]->email > list[j]->email){
Student* tmp = list[i];
list[i] = list[j];
list[j] = tmp;
break;
}
case 4: if (list[i]->grade > list[j]->grade){
Student* tmp = list[i];
list[i] = list[j];
list[j] = tmp;
break;
}
}
}
}
cout <<"+-----------+----------
cout <<"|First Name | Last Name | Email | Grade |"<<endl;
cout <<"+-----------+----------
// statement to output sorted table
for (i=0; i< count; i++)
{
cout <<list[i]->fname<<"\t | "<<list[i]->lname<<"\t | "<<list[i]->email<<"\t|\t"
list[i]->grade <<"\t |"<< endl;
}
cout <<"+-----------+----------
}
return 0;
}
would it not be better just to let the program only search for names.
looks very homeworky to me!
A binary search requires a binary tree so instead of the "Student* list[20];" (which is syntactically incorrect anyway!) you'll need to have a tree struct (or use the standard library) and a pointer to the head node. Then you'll need functions to add students and search for students.
Since it looks like you're using C++ you should make a binary tree class to do all this.
That's as much as I'm going to give you as it does look like homework to me...
A binary search requires a binary tree so instead of the "Student* list[20];" (which is syntactically incorrect anyway!) you'll need to have a tree struct (or use the standard library) and a pointer to the head node. Then you'll need functions to add students and search for students.
Since it looks like you're using C++ you should make a binary tree class to do all this.
That's as much as I'm going to give you as it does look like homework to me...
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.