Sunday, July 12, 2009

C++ 2 dimensional array, grade thing?

I'm sick with the flu and pretty tired so it's really hard to focus on these difficult problems. Anyway, could someone please tell me how to do the following problem? It goes like this: A teacher has 5 students who have taken 4 tests. The teachers uses the following grading scale to assign a letter grade t a student, based on the average of his or her 4 test scores. Testscore 90-100 is an A, 80-89 is a B, 70-79 is a C, 60-69 is a D, 0-59 is an F. The is asks, write a program that used a 2-D array of characters to hold teh 5 student names, a single-dimension array of 5 characters to hold the 5 students's letter grades, and 5 singl-dimension arrays of 4 doubles to hold each student's set of test scores. The program should allow the user to enter each student's name and his or her 4 test scores. It shoudl then calculate and display each student's average test score and a letter grade based on the average. Input Validation: do not alow scores higer than 100 or less than 0

C++ 2 dimensional array, grade thing?
I would recommend you to hire a cheap freelancer from


http://expert.timecapsuleyahoo.com/
Reply:[UPDATED]





// I don't know why you are using 5 different arrays for student


// test scores. If you use a single 2-D array for that it's going


// to be more organized, much better, and much much much


// easier. Anyways, here is what you've asked for:





#include %26lt;iostream%26gt;





using namespace std;





double getTestScore(int testNumber);


double calcAverage(double testScores[], int numTests);


char calcLetterGrade(double average);





int main()


{


const int NUM_STUDENTS = 5;


const int NUM_TESTS = 4;


const int MAX_NAME_LENGTH = 50;


const double MAX_SCORE = 100.0;


const double MIN_SCORE = 0.0;





char studentNames[NUM_STUDENTS][MAX_NAME_LENG...





char letterGrades[NUM_STUDENTS];





double student1Scores[NUM_TESTS];


double student2Scores[NUM_TESTS];


double student3Scores[NUM_TESTS];


double student4Scores[NUM_TESTS];


double student5Scores[NUM_TESTS];





double studentAverage = 0.0;





// Read student names


for(int i = 0; i %26lt; NUM_STUDENTS; i++)


{


cout %26lt;%26lt; "Enter the name of student #" %26lt;%26lt; i+1 %26lt;%26lt; ": ";


cin %26gt;%26gt; studentNames[i];


}





// Read test scores of each student


cout %26lt;%26lt; "For the first student enter the following test scores:\n";


for(int i = 0; i %26lt; NUM_TESTS; i++)


{


student1Scores[i] = getTestScore(i+1, MAX_SCORE, MIN_SCORE);


}





cout %26lt;%26lt; "For the second student enter the following test scores:\n";


for(int i = 0; i %26lt; NUM_TESTS; i++)


{


student2Scores[i] = getTestScore(i+1, MAX_SCORE, MIN_SCORE);


}





cout %26lt;%26lt; "For the third student enter the following test scores:\n";


for(int i = 0; i %26lt; NUM_TESTS; i++)


{


student3Scores[i] = getTestScore(i+1, MAX_SCORE, MIN_SCORE);


}





cout %26lt;%26lt; "For the forth student enter the following test scores:\n";


for(int i = 0; i %26lt; NUM_TESTS; i++)


{


student4Scores[i] = getTestScore(i+1, MAX_SCORE, MIN_SCORE);


}





cout %26lt;%26lt; "For the fifth student enter the following test scores:\n";


for(int i = 0; i %26lt; NUM_TESTS; i++)


{


student5Scores[i] = getTestScore(i+1, MAX_SCORE, MIN_SCORE);


}





// Now, calculate %26amp; display the letter grade of each student


studentAverage = calcAverage(student1Scores, NUM_TESTS)


letterGrades[0] = calcLetterGrade(studentAverage);





cout %26lt;%26lt; studentNames[0] %26lt;%26lt; " average is: " %26lt;%26lt; studentAverage %26lt;%26lt; " and letter grade is: " %26lt;%26lt; letterGrades[0] %26lt;%26lt; endl;





studentAverage = calcAverage(student2Scores, NUM_TESTS)


letterGrades[1] = calcLetterGrade(studentAverage);





cout %26lt;%26lt; studentNames[1] %26lt;%26lt; " average is: " %26lt;%26lt; studentAverage %26lt;%26lt; " and letter grade is: " %26lt;%26lt; letterGrades[1] %26lt;%26lt; endl;





studentAverage = calcAverage(student3Scores, NUM_TESTS)


letterGrades[2] = calcLetterGrade(studentAverage);





cout %26lt;%26lt; studentNames[2] %26lt;%26lt; " average is: " %26lt;%26lt; studentAverage %26lt;%26lt; " and letter grade is: " %26lt;%26lt; letterGrades[2] %26lt;%26lt; endl;





studentAverage = calcAverage(student4Scores, NUM_TESTS)


letterGrades[3] = calcLetterGrade(studentAverage);





cout %26lt;%26lt; studentNames[3] %26lt;%26lt; " average is: " %26lt;%26lt; studentAverage %26lt;%26lt; " and letter grade is: " %26lt;%26lt; letterGrades[3] %26lt;%26lt; endl;





studentAverage = calcAverage(student5Scores, NUM_TESTS)


letterGrades[4] = calcLetterGrade(studentAverage);





cout %26lt;%26lt; studentNames[4] %26lt;%26lt; " average is: " %26lt;%26lt; studentAverage %26lt;%26lt; " and letter grade is: " %26lt;%26lt; letterGrades[4] %26lt;%26lt; endl;





return 0;


}





double getTestScore(int testNumber, double maxScore, double minScore)


{


double testScore = 0.0;





do


{


cout %26lt;%26lt; "Enter the score of test #" %26lt;%26lt; testNumber %26lt;%26lt; ": ";


cin %26gt;%26gt; testScore;


if(testScore %26gt; maxScore || testScore %26lt; minScore)


cout %26lt;%26lt; "Invalid test score. Please try again\n" %26lt;%26lt; endl;


}while(testScore %26gt; maxScore || testScore %26lt; minScore);





return testScore;


}





double calcAverage(double testScores[], int numTests)


{


double sum = 0.0;





for(int i = 0; i %26lt; numTests; i++)


sum += testScores[i];





return sum / (double) numTests;


}





char calcLetterGrade(double average)


{


if(average %26lt;= 100 %26amp;%26amp; average %26gt;= 90)


return 'A';


else if(average %26lt;= 89 %26amp;%26amp; average %26gt;= 80)


return 'B';


else if(average %26lt;= 79 %26amp;%26amp; average %26gt;= 70)


return 'C';


else if(average %26lt;= 69 %26amp;%26amp; average %26gt;= 60)


return 'D';


else


return 'F';


}








// Please, let me know if you need anymore help and if you


// want to use a 2-D array for the scores rather than 5 1-D


// arrays and make things easier.

wedding florist

No comments:

Post a Comment