hey guyz i have to make the following program:
Write a function to read and display the contents of names and marks. You then ask the user for a name and using the linear search return the index to the user. If -1 is returned then the name is not in the file. Otherwise write out the name and mark for that student.
Next, sort the arrays, write them out and then ask the user for a name to search for. This time use the binarySearch to return -1 or an index. Display the student’s name and mark if found.
The contents of names and marks are:
Collins Bill 80
Smith Bart 75
Allen Jim 82
Griffin Jim 55
Stamey Marty 90
Rose Geri 78
Taylor Terri 56
Johnson Jill 77
Allison Jeff 45
Looney Joe 89
Wolfe Bill 63
James Jean 72
Weaver Jim 77
Pore Bob 91
Rutherford Greg 42
Javens Renee 74
Harrison Rose 58
Setzer Cathy 93
Pike Gordon 48
Holland Beth 79
I have finished my program and it looks like this:
When i run this program it has no trouble compiling but it shows me random numbers. can u guyz plz tell me if i am something...or if i did something wrong....ty in advance.Code:#include <iostream> #include <iomanip> #include <fstream> #include <string> using namespace std; void selectionSort(string[], int[], int); void displayData(const string[], const int[], int); void getNames(ifstream& inStream, string[], int[], int); int searchList(string list[], int numElems, string value); int binarySearch(string list[], int size, string value); const int numElems = 20; const int NUM_NAME = 20; int main () { string first_name[20]; string last_name[20]; string names[20]; int marks[20]; ifstream inStream; inStream.open("TextFile4.txt"); getNames(inStream, names, marks, NUM_NAME); displayData(names, marks, NUM_NAME); string value, fname, lname; cout<<"Please enter the name you would like to find: "<<endl; cin>>lname>>fname; value = lname + " " + fname; int result1 = searchList(names, numElems, value); if (result1==-1) { cout <<"Can not find the name!"<<endl; } else cout<<names[result1]<<" "<<marks[result1]; selectionSort(names, marks, numElems); displayData(names, marks, NUM_NAME); cout<<"Please enter the name you would like to find: "<<endl; cin>>lname>>fname; value = lname + " " + fname; int result2 = binarySearch(names, numElems, value); if (result2==-1) { cout <<"Can not find the name!"<<endl; } else cout<<names[result2]<<" "<<marks[result2]; return 0; } void getNames(ifstream& inStream, string names[], int marks[], int numElems) { string fname[NUM_NAME]; string lname[NUM_NAME]; inStream.open("file2.txt"); for(int i=0; i<numElems; i++) { inStream>>lname[i]>>fname[i]>>marks[i]; } for(int i=0; i<numElems; i++) { names[i] = lname[i] + " " + fname[i]; } } void displayData(const string names[] ,const int marks[], int numElems) { for (int i = 0; i < numElems; i++) { cout<<names[i]<<" "<<marks[i]<<endl; } } int searchList(string list[], int numElems, string value) { int index = 0; // Used as a subscript to search array int position = -1; // To record position of search value bool found = false; // Flag to indicate if value was found while (index < numElems && !found) { if (list[index] == value) // If the value is found { found = true; // Set the flag position = index; // Record the value's subscript } index++; // Go to the next element } return position; // Return the position, or -1 } void selectionSort(string names[], int marks[], int numElems) { int startScan, minIndex; string minValue; int value; for (startScan = 0; startScan < (numElems - 1); startScan++) { minIndex = startScan; minValue = names[startScan]; for(int index = startScan + 1; index < numElems; index++) { if (names[index] < minValue) { minValue = names[index]; minIndex = index; } } value = marks[minIndex]; names[minIndex] = names[startScan]; names[startScan] = minValue; marks[minIndex] = marks[startScan]; marks[startScan] = value; } } int binarySearch(string list[], int size, string value) { int first = 0, // First list element last = size - 1, // Last list element middle, // Mid point of search position = -1; // Position of search value bool found = false; // Flag while (!found && first <= last) { middle = (first + last) / 2; // Calculate mid point if (list[middle] == value) // If value is found at mid { found = true; position = middle; } else if (list[middle] > value) // If value is in lower half last = middle - 1; else first = middle + 1; // If value is in upper half } return position; }



LinkBack URL
About LinkBacks


