1 // Fig. 7.15: DoubleArray.cs
2 // Manipulating a double-subscripted array.
3 using System;
4 using System.Drawing;
5 using System.Collections;
6 using System.ComponentModel;
7 using System.Windows.Forms;
8 using System.Data;
9
10 public class DoubleArray :
System.Windows.Forms.Form
11 {
12 private System.Windows.Forms.Button
showOutputButton;
13 private System.Windows.Forms.Label
outputLabel;
14
15 int[][] grades;
16 int students, exams;
17
18 //
Visual Studio .NET generated code
19
20 [STAThread]
21 static void
22 {
23 Application.Run( new DoubleArray() );
24 }
25
26 private void showOutputButton_Click(
object sender,
27 System.EventArgs e )
28
29 {
30 grades = new int[ 3 ][];
31 grades[ 0 ] = new int[]{ 77, 68, 86, 73 };
32 grades[ 1 ] = new int[]{ 96, 87, 89, 81 };
33 grades[ 2 ] = new int[]{ 70, 90, 86, 81 };
34
35 students = grades.Length; // number of students
36 exams = grades[ 0 ].Length; // number of exams
37
38 // line up column headings
39 outputLabel.Text += " ";
40
41 // output the column headings
42 for ( int i = 0; i < exams; i++ )
43 outputLabel.Text += "[" +
i + "] ";
44
45 // output the rows
46 for ( int i = 0; i < students; i++
)
47 {
48 outputLabel.Text +=
"\ngrades[" + i + "]
";
49
50 for ( int j = 0; j < exams; j++ )
51 outputLabel.Text += grades[ i ][
j ] + " ";
52 }
53
54 outputLabel.Text += "\n\nLowest
grade: " + Minimum() +
55 "\nHighest grade: " +
Maximum() + "\n";
56
57 for ( int i = 0; i < students; i++ )
58 outputLabel.Text += "\nAverage
for student " + i + " is " +
59 Average( grades[ i ] );
60
61 } // end method showOutputButton_Click
62
63 // find minimum grade in grades array
64 public int Minimum()
65 {
66 int lowGrade = 100;
67
68 for ( int i = 0; i < students; i++
)
69
70 for ( int j = 0; j < exams; j++
)
71
72 if ( grades[ i ][ j ] <
lowGrade )
73 lowGrade = grades[ i ][ j ];
74
75 return lowGrade;
76 }
77
78 // find maximum grade in grades array
79 public int Maximum()
80 {
81 int highGrade = 0;
82
83 for ( int i = 0; i < students; i++
)
84
85 for ( int j = 0; j < exams; j++
)
86
87 if ( grades[ i ][ j ] >
highGrade )
88 highGrade = grades[ i ][ j ];
89
90 return highGrade;
91 }
92
93 // determine average grade for a
particular student
94 public double Average( int[] setOfGrades
)
95 {
96 int total = 0;
97
98 for ( int i = 0; i <
setOfGrades.Length; i++ )
99 total += setOfGrades[ i ];
100
101 return ( double ) total /
setOfGrades.Length;
102 }
103
104 } // end class
