summaryrefslogtreecommitdiffstats
path: root/Bachelor/Prog1/examples/ch04/Fig04_23.cpp
blob: 7957c1e49f51fc8cdf75e13c944032f946cbe262 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
// Fig. 4.23: fig04_23.cpp
// Double-subscripted array example.
#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 = 3;   // number of students
const int exams = 4;      // 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 ] = 
      { { 77, 68, 86, 73 },
        { 96, 87, 89, 78 },
        { 70, 90, 86, 81 } };

   // 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]  [3]";

   // 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


/**************************************************************************
 * (C) Copyright 1992-2003 by Deitel & Associates, Inc. and Prentice      *
 * Hall. All Rights Reserved.                                             *
 *                                                                        *
 * DISCLAIMER: The authors and publisher of this book have used their     *
 * best efforts in preparing the book. These efforts include the          *
 * development, research, and testing of the theories and programs        *
 * to determine their effectiveness. The authors and publisher make       *
 * no warranty of any kind, expressed or implied, with regard to these    *
 * programs or to the documentation contained in these books. The authors *
 * and publisher shall not be liable in any event for incidental or       *
 * consequential damages in connection with, or arising out of, the       *
 * furnishing, performance, or use of these programs.                     *
 *************************************************************************/