#include <iostream> #include <string> using std::string; using std::cout; using
ID: 3623934 • Letter: #
Question
#include <iostream>#include <string>
using std::string;
using std::cout;
using std::endl;
static const int NUM_STUDENTS = 4;
static const int NUM_GRADES = 5;
int main()
{
int grades[NUM_STUDENTS][NUM_GRADES] =
{
{ 92, 88, 94, 96, 91 },
{ 75, 66, 81, 70, 63 },
{ 68, 55, 68, 74, 72 },
{ 75, 90, 95, 85, 83 }
};
string students[NUM_STUDENTS] =
{
"Kirk", "Picard", "Spock", "Worf"
};
for(int i = 0; i < NUM_STUDENTS; i ++)
{
double total = 0.0;
for(int j = 0; j < NUM_GRADES; j ++)
total += grades[i][j];
cout << "The average for student: " <<
students[i] << " is: " << total / NUM_GRADES << endl;
}
for(int i = 0; i < NUM_STUDENTS; i ++)
{
int bestScore = 0;
int worstScore = 0;
int bestExam = 0;
int worstExam = 0;
for(int j = 0; j < NUM_GRADES; j ++)
{
if(j == 0)
{
bestScore = worstScore = grades[i][j];
bestExam = worstExam = 1;
}
else
{
if(grades[i][j] > bestScore)
{
bestScore = grades[i][j];
bestExam = j + 1;
}
if(grades[i][j] < worstScore)
{
worstScore = grades[i][j];
worstExam = j + 1;
}
}
}
cout << "Student: " << students[i] << ", best exam: " <<
bestExam << ", worst exam: " << worstExam << endl;
}
//THIS SHOULD BE PRINTED WHEN RUNNING THE PROGRAM :
// Exam 1, best: Kirk, worst: Spock
// Exam 2, best: Worf, worst: Spock
// Exam 3, best: Worf, worst: Spock
// Exam 4, best: Kirk, worst: Picard
// Exam 5, best: Kirk, worst: Picard
// THE PART BELOW IS THE CODE THAT I NEED...
for (int j = 0; j < NUM_GRADES; j++)
{
int best = 0;
int worst = 0;
int bestIndex = 0;
int worstIndex = 0;
for(int i = 0; i < NUM_STUDENTS; i++)
{
if (i == 0)
{
bestIndex = worstIndex = grades[i][j];
best = worst = 1;
}
else
{
if (grades[i][j]> bestIndex)
{
bestIndex = grades[i][j];
best = j + 1;
}
if (grades[i][j]< worstIndex)
{
worstIndex = grades[i][j];
worst = j + 1;
}
}
}
cout << "Exam " << (j + 1);
cout << ", best: " << students[bestIndex];
cout << ", worst: " << students[worstIndex]<< endl;
}
return(0);
}
Explanation / Answer
Your sum function that shows each student's average score seems to be working correctly.
//THIS SHOULD BE PRINTED WHEN RUNNING THE PROGRAM :
// Exam 1, best: Kirk, worst: Spock
// Exam 2, best: Worf, worst: Spock
// Exam 3, best: Worf, worst: Spock
// Exam 4, best: Kirk, worst: Picard
// Exam 5, best: Kirk, worst: Picard
// THE PART BELOW IS THE CODE THAT I NEED...
for (int j = 0; j < NUM_GRADES; j++)
{
int best = 0;
int worst = 0;
int bestIndex = 0;
int worstIndex = 0;
for(int i = 0; i < NUM_STUDENTS; i++)
{
if (i == 0)
{
//you called these variables indexes.
// if this is true, they can only be in the values of
//[0 through 3] (you only have 4 //people)
//set values to 0, that is where the indexes start
bestIndex = worstIndex = 0;
//this actually stores the VALUE at that specific place.
best = worst = grades[i][j];
}
else
{
if (grades[i][j]> best)
{
best = grades[i][j];
bestIndex = i;
}
if (grades[i][j]< worst)
{
worst = grades[i][j];
worstIndex = i;
}
} //end else
} //end for (NUM_STUDENTS)
cout << "Exam " << (j + 1);
cout << ", best: " << students[bestIndex];
cout << ", worst: " << students[worstIndex]<< endl;
}
return(0);
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.