const 변수로 선언된 students, exams을 2차원 배열로 동적 할당을 하여 입력한 시점에서 2차원 배열이 설정하도록 하였다.
#include <iostream>
using std::cout;
using std::endl;
using std::cin;
using std::fixed;
using std::left;
#include <iomanip>
using std::setw;
using std::setprecision;
// function prototypes
int minimum( int **, int, int );
int maximum( int **, int, int );
double average( int [], int );
void printArray( int **, int, int );
int main()
{
int students; // number of students
int exams; // number of exams
int i=0, j=0;
cin>>students;
cin>>exams;
// initialize student grades for three students (rows)
int ** studentGrades = new int* [students];
for(i=0; i<students; i++)
studentGrades[i] = new int [exams];
for(i=0; i<students; i++)
for(j=0; j<exams; j++)
cin>>studentGrades[i][j];
// output array studentGrades
cout << "The array is:\n";
printArray( studentGrades, students, exams );
// determine smallest and largest grade values
cout << "\n\nLowest grade: "
<< minimum( studentGrades, students, exams )
<< "\nHighest grade: "
<< maximum( studentGrades, students, exams ) << '\n';
cout << fixed << setprecision( 2 );
// calculate average grade for each student
for ( int person = 0; person < students; person++ )
cout << "The average grade for student " << person
<< " is "
<< average( studentGrades[ person ], exams )
<< endl;
if(studentGrades){
for(i=0; i<students; i++)
delete[] studentGrades[i];
delete[] studentGrades;
}
return 0; // indicates successful termination
} // end main
// find minimum grade
int minimum( int ** grades, int pupils, int tests )
{
int lowGrade = 100; // initialize to highest possible grade
for ( int i = 0; i < pupils; i++ )
for ( int j = 0; j < tests; j++ )
if ( grades[ i ][ j ] < lowGrade )
lowGrade = grades[ i ][ j ];
return lowGrade;
} // end function minimum
// find maximum grade
int maximum( int ** grades, int pupils, int tests )
{
int highGrade = 0; // initialize to lowest possible grade
for ( int i = 0; i < pupils; i++ )
for ( int j = 0; j < tests; j++ )
if ( grades[ i ][ j ] > highGrade )
highGrade = grades[ i ][ j ];
return highGrade;
} // end function maximum
// determine average grade for particular student
double average( int setOfGrades[], int tests )
{
int total = 0;
// total all grades for one student
for ( int i = 0; i < tests; i++ )
total += setOfGrades[ i ];
return static_cast< double >( total ) / tests; // average
} // end function maximum
// Print the array
void printArray( int ** grades, int pupils, int tests )
{
// set left justification and output column heads
cout << left << " [0] [1] [2] ";
// output grades in tabular format
for ( int i = 0; i < pupils; i++ ) {
// output label for row
cout << "\nstudentGrades[" << i << "] ";
// output one grades for one student
for ( int j = 0; j < tests; j++ )
cout << setw( 5 ) << grades[ i ][ j ];
} // end outer for
} // end function printArray
-------------------------------------------------------------------------------
원본
#include <iostream>
using std::cout;
using std::endl;
using std::fixed;
using std::left;
#include <iomanip>
using std::setw;
using std::setprecision;
const int students = 5; // number of students
const int exams = 3; // number of exams
// function prototypes
int minimum( int [][ exams ], int, int );
int maximum( int [][ exams ], int, int );
double average( int [], int );
void printArray( int [][ exams ], int, int );
int main()
{
// initialize student grades for three students (rows)
int studentGrades[ students ][ exams ];
for(int i=0; i<students; i++)
for(int j=0; j<exams; j++)
cin>>studentGrades[i][j];
// output array studentGrades
cout << "The array is:\n";
printArray( studentGrades, students, exams );
// determine smallest and largest grade values
cout << "\n\nLowest grade: "
<< minimum( studentGrades, students, exams )
<< "\nHighest grade: "
<< maximum( studentGrades, students, exams ) << '\n';
cout << fixed << setprecision( 2 );
// calculate average grade for each student
for ( int person = 0; person < students; person++ )
cout << "The average grade for student " << person
<< " is "
<< average( studentGrades[ person ], exams )
<< endl;
return 0; // indicates successful termination
} // end main
// find minimum grade
int minimum( int grades[][ exams ], int pupils, int tests )
{
int lowGrade = 100; // initialize to highest possible grade
for ( int i = 0; i < pupils; i++ )
for ( int j = 0; j < tests; j++ )
if ( grades[ i ][ j ] < lowGrade )
lowGrade = grades[ i ][ j ];
return lowGrade;
} // end function minimum
// find maximum grade
int maximum( int grades[][ exams ], int pupils, int tests )
{
int highGrade = 0; // initialize to lowest possible grade
for ( int i = 0; i < pupils; i++ )
for ( int j = 0; j < tests; j++ )
if ( grades[ i ][ j ] > highGrade )
highGrade = grades[ i ][ j ];
return highGrade;
} // end function maximum
// determine average grade for particular student
double average( int setOfGrades[], int tests )
{
int total = 0;
// total all grades for one student
for ( int i = 0; i < tests; i++ )
total += setOfGrades[ i ];
return static_cast< double >( total ) / tests; // average
} // end function maximum
// Print the array
void printArray( int grades[][ exams ], int pupils, int tests )
{
// set left justification and output column heads
cout << left << " [0] [1] [2] ";
// output grades in tabular format
for ( int i = 0; i < pupils; i++ ) {
// output label for row
cout << "\nstudentGrades[" << i << "] ";
// output one grades for one student
for ( int j = 0; j < tests; j++ )
cout << setw( 5 ) << grades[ i ][ j ];
} // end outer for
} // end function printArray