Question: I need help fixing this code: #include #include #include #include using namespace std; struct StudentRecord { string name; int tests[5]; double average; char grade; };
I need help fixing this code:
#include
using namespace std;
struct StudentRecord { string name; int tests[5]; double average; char grade; };
void getData(ifstream &infile, StudentRecord &student); double calcAverage(int tests[]); char determineGrade(int average); void showOutput(ofstream &outfile, StudentRecord student);
int linearSearch(StudentRecord arr[], int size, string key) { for (int i = 0; i < size; i++) { if (arr[i].name == key) { return i; } } return -1; }
int binarySearch(StudentRecord arr[], int size, string key) { int low = 0; int high = size - 1; while (low <= high) { int mid = (low + high) / 2; if (arr[mid].name == key) { return mid; } else if (arr[mid].name < key) { low = mid + 1; } else { high = mid - 1; } } return -1; }
void bubbleSort(StudentRecord arr[], int size) { bool swapped = true; int j = 0; StudentRecord temp; while (swapped) { swapped = false; j++; for (int i = 0; i < size - j; i++) { if (arr[i].name > arr[i + 1].name) { temp = arr[i]; arr[i] = arr[i + 1]; arr[i + 1] = temp; swapped = true; } } } }
void selectionSort(StudentRecord arr[], int size) { int i, j, minIndex; StudentRecord temp; for (i = 0; i < size - 1; i++) { minIndex = i; for (j = i + 1; j < size; j++) { if (arr[j].average < arr[minIndex].average) { minIndex = j; } } if (minIndex != i) { temp = arr[i]; arr[i] = arr[minIndex]; arr[minIndex] = temp; } } }
int main() { const int MAX_RECORDS = 100; StudentRecord records[MAX_RECORDS]; ifstream infile("students.txt"); ofstream outfile("output.txt");
int numRecords = 0; while (infile && numRecords < MAX_RECORDS) { getData(infile, records[numRecords]); numRecords++; }
bubbleSort(records, numRecords);
outfile << "Name\t\t\tTest1\tTest2\tTest3\tTest4\tTest5\tAvg\tGrade "; for (int i = 0; i < numRecords; i++) { showOutput(outfile, records[i]); }
string searchName = "Nancy M. Zamora"; int index = linearSearch(records, numRecords, searchName); if (index != -1) { cout << "Found " << searchName << " at index " << index << endl; } else { cout << searchName << " not found" << endl; }
int searchTotal = 406; index = binarySearch(records, numRecords, searchName); if (index != -1) { cout << "Found total score " << searchTotal << " for " << records[index].name << " at index " << index << endl; } else { cout << "Total score " << searchTotal << " not found" << endl; }
selectionSort(records, numRecords);
outfile << " Sorted by total score: "; outfile << "Name\t\t\tTest1\tTest2\tTest3\tTest4\tTest5\tAvg\tGrade "; for (int i = 0; i < numRecords; i++) { showOutput(outfile, records[i]); }
infile.close(); outfile.close(); return 0; }
void getData(ifstream &infile, StudentRecord &student) { getline(infile, student.name); for (int i = 0; i < 5; i++) { infile >> student.tests[i]; } infile.ignore(); student.average = calcAverage(student.tests); student.grade = determineGrade(student.average); }
double calcAverage(int tests[]) { double sum = 0; for (int i = 0; i < 5; i++) { sum += tests[i]; } return sum / 5; }
char determineGrade(int average) { if (average >= 90) { return 'A'; } else if (average >= 80) { return 'B'; } else if (average >= 70) { return 'C'; } else if (average >= 60) { return 'D'; } else { return 'F'; } }
void showOutput(ofstream &outfile, StudentRecord student) { outfile << student.name << "\t"; for (int i = 0; i < 5; i++) { outfile << student.tests[i] << "\t"; } outfile << student.average << "\t" << student.grade << endl; }
Input file:
Norman M. Richard 88 90 89 97 93 Alfred G. Pena 67 63 61 67 60 Hema A. Garcia 77 71 79 68 70 Omar T. Garza 83 80 81 89 85 Nancy M. Zamora 55 56 57 60 63
The output file and display output is supposed to look like this:
Name Test1 Test2 Test3 Test4 Test5 Avg Grade
Norman M. Richard 88 90 88 97 93 91 A
But it gives me this for the output file:
Name Test1 Test2 Test3 Test4 Test5 Avg Grade Norman M. Richard 88 90 89 97 93 0 0 0 0 0 0 F
Sorted by total score: Name Test1 Test2 Test3 Test4 Test5 Avg Grade Norman M. Richard 88 90 89 97 93 0 0 0 0 0 0 F
And it gives me this on the display output:
Nancy M. Zamora not found Total score 406 not found
Language is C++
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
