Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Please convert the c++ code below to use vectors instead of dynamic arrays: #include #include using namespace std; double* studentscores(char* anskey, char** std_anskey, int NUM_STUD);

Please convert the c++ code below to use vectors instead of dynamic arrays: #include #include using namespace std; double* studentscores(char* anskey, char** std_anskey, int NUM_STUD); double bestgrade(double* students_scores, int NUM_STUD); double lowestgrade(double* students_scores, int NUM_STUD); char* lettergrade(double* students_scores, int NUM_STUD); void distream(ostream& out, char** stdid, char** stdanswers, double* stud_percentage, double best_grade, double lowest_grade, char* gletter, int numq, int numstud); //not needed anymore... replaced with the general implemenation of distream //void savegrades(char** stdID, char** std_anskey, double* students_scores, double bestgrade, double lowestgrade, char* gletter, int NUM_Q, int NUM_STUD); //void display(char** stdID, char** std_anskey, double* students_scores, double best_grade, double lowest_grade, char* gletter, int NUM_Q, int NUM_STUD); double* studentscores(char* anskey, char** stdanswers, int numstud) { double* stud_percentage = new double[numstud]; for (int s = 0; s < numstud; s++) { int q = 0, correct=0; while (anskey[q] != '\0') { if (stdanswers[s][q] == anskey[q]) { correct++; } q++; } stud_percentage[s] = (static_cast(correct) / q) * 100.0; } return stud_percentage; } void distream(ostream &out, char** stdid, char** stdanswers, double* stud_percentage, double best_grade, double lowest_grade, char* gletter, int numq, int numstud) { out << "Student\t" << " ------Answers----\t" << "Percent\t" << "Grade" << endl; for (int s = 0; s < numstud; s++) { out<< stdid[s] << "\t"; out << stdanswers[s]; out << "\t" << stud_percentage[s] << "\t" << gletter[s] << endl; } out << "Highest grade is " << best_grade << " the following students achieved it "; for (int s = 0; s < numstud; s++) { if (stud_percentage[s] == best_grade) { out << stdid[s] << " "; } } out << endl; out << "Lowest grade is " << lowest_grade << " the following students achieved it "; for (int s = 0; s < numstud; s++) { if (stud_percentage[s] == lowest_grade) { out<< stdid[s] << " "; } } out << endl; } double bestgrade(double* stud_percentage, int numstud) { double best_grade = stud_percentage[0]; for (int s = 0;s < numstud; s++) { if (best_grade < stud_percentage[s]) { best_grade = stud_percentage[s]; } } return best_grade; } double lowestgrade(double* stud_percentage, int numstud) { double lowest_grade = stud_percentage[0]; for (int p = 0; p < numstud; p++) { if (lowest_grade > stud_percentage[p]) { lowest_grade = stud_percentage[p]; } } return lowest_grade; } char* lettergrade(double* stud_percentage, int numstud) { char* gletter = new char[numstud]; for (int i = 0; i < numstud; i++) { if (stud_percentage[i] >= 90) { gletter[i] = 'A'; } else if (stud_percentage[i] >= 80) { gletter[i] = 'B'; } else if (stud_percentage[i] >= 70) { gletter[i] = 'C'; } else if (stud_percentage[i] >= 60) { gletter[i] = 'D'; } else { gletter[i] = 'F'; } } return gletter; } int main() { //openning the input file ifstream infile; infile.open("his101midterm1.txt"); //checking if the openning fails if (infile.fail()) { cout << "Error opening the file"; } //reading the number of students, number of questions, and number of char in a student id int numstud, numq, numidchar; infile >> numstud >> numidchar >> numq; //creating the dynamic arrays char* anskey = new char[numq + 1]; char** stdid = new char* [numstud]; char** std_ans = new char* [numstud]; for (int s = 0; s < numstud; s++) { stdid[s] = new char[numidchar +1]; std_ans[s] = new char[numq + 1]; } //reading from a file and storing the values read into the arrays infile >> anskey; for (int s = 0; s < numstud; s++) { infile >> stdid[s]; infile >> std_ans[s]; } infile.close(); //calling the function created double* stud_percentage = studentscores(anskey, std_ans, numstud); char* gletter = lettergrade(stud_percentage, numstud); double best_grade = bestgrade(stud_percentage, numstud); double lowest_grade = lowestgrade(stud_percentage, numstud); //passing cout to the distream function to display on the screen console distream(cout, stdid, std_ans, stud_percentage, best_grade, lowest_grade, gletter, numq, numstud); //saving to a file ofstream outfile("outfile.txt"); //passing the file stream to the distream to save to a file distream(outfile, stdid, std_ans, stud_percentage, best_grade, lowest_grade, gletter, numq, numstud); outfile.close(); //deleting the arrays delete[] stud_percentage; delete[] gletter; delete[] anskey; for (int i = 0; i < numstud; i++) { delete[] stdid[i]; delete[] std_ans[i]; } delete[] stdid; delete[] std_ans; stud_percentage = NULL; gletter = NULL; anskey = NULL; stdid = NULL; std_ans = NULL; system("pause"); return 0; }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Making Databases Work The Pragmatic Wisdom Of Michael Stonebraker

Authors: Michael L. Brodie

1st Edition

1947487167, 978-1947487161

More Books

Students also viewed these Databases questions