//Purpose: This program reads data for a computer science closed lab section. //
ID: 3600944 • Letter: #
Question
//Purpose: This program reads data for a computer science closed lab section.
// Add a function PrintScores that prints the scores so that each student's labs appear
// on a separate line of output. Your output should be labeled as follows:
/* Student 1: 80 90 70 100 60 90 85 78 93 80 70 98 89 94
Student 2: 98 85 100 99 89 90 72 0 78 98 100 65 0 56
Student 3: 85 60 25....
.... */
//Input: The first two entries in the input are the number of students
// in the class and the number of closed labs the class has finished.
// For each student, their closed labs are input from the
// following lines of the file.
//
//Limitations: It is assumed that there will be no more than
// MAX_STUDENTS students in the class and there will be
// no more than MAX_LABS labs.
//include files...
#include <iostream>
using namespace std;
//global constants...
const int MAX_STUDENTS=40; //maximum number of students
const int MAX_LABS=14; //maximum number of closed labs
//function prototypes...
//function to read labs from the data file
void ReadScores(int labScores[][MAX_LABS], int& numStudents, int& numLabs);
//function prototypes for PrintScores
void PrintScores(const int labScores[][MAX_LABS], int numStudents, int numLabs);
int main()
{
//local declarations...
int numStudents; //how many students are in the class
int numLabs; //how many closed labs
int labScores[MAX_STUDENTS][MAX_LABS]; //holds lab scores
//read in the data for all students in the class
ReadScores(labScores, numStudents, numLabs);
//print the data in labScores read using ReadScores
//end of main...
return 0;
}
//Function: ReadScores()
//Purpose: This function reads data for students in a closed lab class.
// Data is read from the standard input. The number of students in the lab
// and the number of closed labs finished by each student are read first.
// Next, for each student, their closed labs are read into the 2D array labscores.
//Assumption: MAX_LABS is a global constant which has been defined
// previously.
void ReadScores(int labScores[][MAX_LABS], //OUT: Holds the lab scores
int& numStudents, //OUT: # of students
int& numLabs) //OUT: Number of Labs
{
//local declarations...
int student; //controls which student's labs are read
int lab; //controls which lab is being read
//get the number of students in the class
cin >> numStudents >> numLabs;
//outer loop controls which student(row) is being read
for (student = 0; student < numStudents; student++)
{
//inner loop controls which lab(column) is being read
for (lab = 0; lab < numLabs; lab++)
cin >> labScores[student][lab];
}
return;
}
//Place the definition of your function to print the scores here
Explanation / Answer
#include <iostream>
using namespace std;
//global constants...
const int MAX_STUDENTS=40; //maximum number of students
const int MAX_LABS=14; //maximum number of closed labs
//function prototypes...
//function to read labs from the data file
void ReadScores(int labScores[][MAX_LABS], int& numStudents, int& numLabs);
//function prototypes for PrintScores
void PrintScores(const int labScores[][MAX_LABS], int numStudents, int numLabs);
int main()
{
//local declarations...
int numStudents; //how many students are in the class
int numLabs; //how many closed labs
int labScores[MAX_STUDENTS][MAX_LABS]; //holds lab scores
//read in the data for all students in the class
ReadScores(labScores, numStudents, numLabs);
//print the data in labScores read using ReadScores
PrintScores(labScores,numStudents,numLabs);
//end of main...
return 0;
}
//Function: ReadScores()
//Purpose: This function reads data for students in a closed lab class.
// Data is read from the standard input. The number of students in the lab
// and the number of closed labs finished by each student are read first.
// Next, for each student, their closed labs are read into the 2D array labscores.
//Assumption: MAX_LABS is a global constant which has been defined
// previously.
void ReadScores(int labScores[][MAX_LABS], //OUT: Holds the lab scores
int& numStudents, //OUT: # of students
int& numLabs) //OUT: Number of Labs
{
//local declarations...
int student; //controls which student's labs are read
int lab; //controls which lab is being read
//get the number of students in the class
cin >> numStudents >> numLabs;
//outer loop controls which student(row) is being read
for (student = 0; student < numStudents; student++)
{
//inner loop controls which lab(column) is being read
for (lab = 0; lab < numLabs; lab++)
cin >> labScores[student][lab];
}
return;
}
//Place the definition of your function to print the scores here
void PrintScores(const int labScores[][MAX_LABS], int numStudents, int numLabs){
//local declarations...
int student; //controls which student's labs are read
int lab; //controls which lab is being read
//we need to print scores for every student, so outer loop is for students:
for (student = 0; student < numStudents; student++)
{
cout<<"Student "<<(student + 1)<<": ";
//For every student, we need to print the lab scores:
for (lab = 0; lab < numLabs; lab++)
cout<<labScores[student][lab]<<" ";
cout<<endl;
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.