Need help converting my code from a 1D array to a 2 D array. Thanks. #include #i
ID: 3584539 • Letter: N
Question
Need help converting my code from a 1D array to a 2 D array. Thanks. #include #include using namespace std; // Global constants const int MAX = 20; // Function declarations // Get number of students from the user int getStudentCount( ); // Reads students exam scores (floating numbers from 0.0 to 100.0) void getExamScores(double score[ ], int count); // Reads students lab scores (floating numbers from 0.0 to 100.0) void getLabScores(double score[ ], int count); // Reads students homework scores (floating numbers from 0.0 to 100.0) void getHWScores(double score[ ], int count); // Populates point average array with each students weighted average score void calculatePointGrades(const double examScore[ ], const double labScore[ ], const double hwScore[ ], int count, double pointScore[ ]); // Populate letterGrade array with each students // letter grade (90 and above = A, 80 to 89 B, etc.) void calculateLetterGrades(const double pointScore[ ], int count, char letterGrade[ ]); // Display table of scores, point averages, and letter grades // and overall averages for exam, labs and point grades void showGradeData(const double examScore[ ], const double labScore[ ], const double hwscore[ ], const double pointScore[ ], const char letterGrade[ ], int count); // Returns the average of specified number (count) items // in an int array double intArrayAve(const int data[ ], int count); // Returns the average of specified number (count) items // in a double array double doubleArrayAve(const double data[ ], int count); int main( ) { int numStuds; numStuds = getStudentCount( ); double exam[MAX]; getExamScores(exam, numStuds); double labAve[MAX]; getLabScores(labAve, numStuds); double hwAve[MAX]; getHWScores(hwAve, numStuds); // Calculate and store point grades: // 60% for exam, 30% for labs, and 10% for homework double pointGrade[MAX]; calculatePointGrades(exam, labAve, hwAve, numStuds, pointGrade); // Calculate letter grades based on point average grades: // 90 or above = A // At least 80 but less than 90 = B // At least 70 but less than 80 = C // At least 60 but less than 70 = D // Less than 60 = F char grade[MAX]; calculateLetterGrades(pointGrade, numStuds, grade); // Display table of grade data and overall averages showGradeData(exam, labAve, hwAve, pointGrade, grade, numStuds); cout << endl << endl << "Press ENTER key to exit" << endl; cin.ignore(2); return 0; } // Get number of students from the user int getStudentCount( ) { cout << "Enter the number of students (maximum " << MAX << "): " << flush; int count; cin >> count; while(count < 1 || count > MAX) { cout << endl << "Error: "; if(count < 1) { cout << " number of students must be at least 1" << ", try again." << endl << endl; } else // Must be greater than MAX { cout << " cannot exceed " << MAX << ", try again." << endl << endl; } cout << "Enter the number of students (maximum " << MAX << "): " << flush; cin >> count; } return count; } /*-------------------------------------------------- * Function Name: getExamScores * * * Description: prompts for and reads in the specified * number (count) of exam scores. * * -----------------------------------------------------*/ void getExamScores(double score[ ], int count) { cout << endl << "Exam scores must be floating numbers from 0.0 to 100.0" << endl; for(int i = 0; i < count; ++i) { cout << "Enter exam score for student #" << i + 1 << ": " << flush; cin >> score[i]; } cout << endl; return; } /*-------------------------------------------------- * Function Name: getLabScores * * * Description: prompts for and reads in the specified * number (count) of lab scores. * * -----------------------------------------------------*/ void getLabScores(double score[ ], int count) { cout << endl << "Lab average scores must be floating point numbers" << " from 0.0 to 100.0." << endl; for(int i = 0; i < count; ++i) { cout << "Enter lab average for student #" << i + 1 << ": " << flush; cin >> score[i]; } cout << endl; return; } /*-------------------------------------------------- * Function Name: getHWScores * * * Description: prompts for and reads in the specified * number (count) of homework scores. * * -----------------------------------------------------*/ void getHWScores(double score[ ], int count) { cout << endl << "Homework average scores must be floating point numbers" << " from 0.0 to 100.0." << endl; for(int i = 0; i < count; ++i) { cout << "Enter Homework average for student #" << i + 1 << ": " << flush; cin >> score[i]; } cout << endl; return; } /*-------------------------------------------------- * Function Name: calculatePointGrades * * * Description: * Calculates a weighted average for each student * (60% X exam + 30% X Lab Avg + 10% X Homework Avg) and writes the values into * the pointScore array. * * -----------------------------------------------------*/ void calculatePointGrades(const double examScore[ ], const double labScore[ ], const double hwScore[ ], int count, double pointScore[ ]) { for(int i = 0; i < count; ++ i) { pointScore[i] = 0.6 * examScore[i] + 0.3 * labScore[i] + 0.1 * hwScore[i]; } return; } /*----------------------------------------------------------------------- // Calculate and store letter grades // based on point average grades: // 90 or above = A // At least 80 but less than 90 = B // At least 70 but less than 80 = C // At least 60 but less than 70 = D // Less than 60 = F // ---------------------------------------------------------------------*/ void calculateLetterGrades(const double pointScore[ ], int count, char letterGrade[ ]) { for(int i = 0; i < count; ++i) { // Convert point score to an int from 0 to 10 // so a switch can be easily used int numberGrade = static_cast(pointScore[i]) / 10; if(numberGrade > 10) // set overacheivers to 10 for an 'A' { numberGrade = 10; // Makes switch easier } switch(numberGrade) { case 10: case 9: letterGrade[i] = 'A'; break; case 8: letterGrade[i] = 'B'; break; case 7: letterGrade[i] = 'C'; break; case 6: letterGrade[i] = 'D'; break; default: letterGrade[i] = 'F'; } } } // Display table of scores, point averages, and letter grades // and overall averages for exam, labs, homework, and point grades void showGradeData(const double examScore[ ], const double labScore[ ], const double hwScore[ ], const double pointScore[ ], const char letterGrade[ ], int count) { // Set up point average output format cout << fixed << showpoint << setprecision(1); cout << " ----------------------------------------- " << endl; cout << "| SN | EXAM | LABS | HW | PTS | GR |" << endl; // i = index = student number - 1 for(int i = 0; i < count; ++i) { cout << "|-----------------------------------------|" << endl << "| " << setw(2) << i + 1 << " | " << setw(5) << examScore[i] << " | " << setw(5) << labScore[i] << " | " << setw(5) << hwScore[i] << " | " << setw(5) << pointScore[i] << " | " << letterGrade[i] << " |" << endl; } cout << " ----------------------------------------- " << endl << endl; cout << "Overall Exam average = " << doubleArrayAve(examScore, count) << endl << "Overall Lab average = " << doubleArrayAve(labScore, count) << endl << "Overall Homework average = " << doubleArrayAve(hwScore, count) << endl << "Overall Point average = " << doubleArrayAve(pointScore, count) << endl << endl; return; } // Returns the average of specified number (count) items // in an int array double intArrayAve(const int data[ ], int count) { int sum = 0; for(int i = 0; i < count; ++i) { sum += data[i]; } return static_cast(sum) / count; } // Returns the average of specified number (count) items // in a double array double doubleArrayAve(const double data[ ], int count) { double sum = 0.0; for(int i = 0; i < count; ++i) { sum += data[i]; } return sum / count; }Explanation / Answer
//file hosted on my dropbox. Rate and comment and I'll unhost https://www.dropbox.com/s/gu5nd8rv3npz4q1/3-1Dto%201-2DGrades.txt
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.