|
To access the contents, click the chapter and section titles.
Learn Pascal in a Three Days (2nd Ed.)
Example: Scores of StudentsThe following program is used to read the scores of a number of students in different classes as represented in Table 5-2. For simplicity of demonstration, only four students and three classes will be considered; you can, however, modify the number of students or classes by simply changing the values of the two constants NumberOfClasses and NumberOfStudents. { ------------------- figure 5-6 -------------------- } PROGRAM Scores3(INPUT,OUTPUT); { using two-dimensional array } CONST NumberOfClasses = 3; { Change this number for more classes } NumberOfStudents = 4; { Change this number for more students } Tab = ' '; { 7 spaces } Dash = '-'; NumberOfDashes = 23; TYPE ScoreArray = ARRAY[1..NumberOfStudents, 1..NumberOfClasses] OF REAL; AverageArray = ARRAY[1..NumberOfStudents] OF REAL; VAR Score :ScoreArray; Average :AverageArray; SumOfScores :REAL; StudentCount, ScoreCount, DashCount :INTEGER; BEGIN { Read the scores array } { --------------- } FOR StudentCount:= 1 TO NumberOfStudents DO BEGIN WRITELN; WRITELN('Scores of student #', StudentCount,': '); FOR ScoreCount:= 1 TO NumberOfClasses DO BEGIN WRITE('Enter score for class #', ScoreCount,': '); READLN(Score[StudentCount, ScoreCount]) END; END; { Calculate the average for each student } { --------------------------- } FOR StudentCount:= 1 TO NumberOfStudents DO BEGIN SumOfScores:= 0; { Initialize for each student } FOR ScoreCount:= 1 TO NumberOfClasses DO SumOfScores:= SumOfScores + Score[StudentCount, ScoreCount]; Average[StudentCount]:= SumOfScores/NumberOfClasses END; { Display results } { ------------ } WRITELN; WRITELN(Tab, 'Student #', Tab, 'Average'); WRITE(Tab); FOR DashCount:= 1 TO NumberOfDashes DO WRITE(Dash); WRITELN; FOR StudentCount:= 1 TO NumberOfStudents DO WRITELN(Tab, StudentCount:3, Tab, Average[StudentCount]:12:2); WRITE(Tab); FOR DashCount:= 1 TO NumberOfDashes DO WRITE(Dash); WRITELN; WRITELN('Press ENTER to continue..'); READLN END. The following is a sample run: Scores of student #1: Enter score for class #1: 90 Enter score for class #2: 89 Enter score for class #3: 93 Scores of student #2: Enter score for class #1: 80 Enter score for class #2: 70 Enter score for class #3: 60 Scores of student #3: Enter score for class #1: 77 Enter score for class #2: 78 Enter score for class #3: 90 Scores of student #4: Enter score for class #1: 91 Enter score for class #2: 94 Enter score for class #3: 95 Student # Average ---------------- 1 90.67 2 70.00 3 81.67 4 93.33 ---------------- Press ENTER to continue.. Notice the following in this program:
Array InitializationIf you are assigning values to only some of the elements of an uninitialized array, do not expect that the rest of the elements will contain zeros. In such applications you have to initialize the whole array using a loop like this: FOR I:= 1 TO N DO MyArray[I]:= 0; You need another loop if the array is two-dimensional: FOR I:= 1 TO N DO FOR J:= 1 TO M DO MyArray[I,J]:= 0; In the last example, we assigned values to each element of the array, so there was no need for initialization. SummaryIn this chapter you have had a review of simple and structured data types.
|
Products | Contact Us | About Us | Privacy | Ad Info | Home
Use of this site is subject to certain Terms & Conditions, Copyright © 1996-2000 EarthWeb Inc. All rights reserved. Reproduction whole or in part in any form or medium without express written permission of EarthWeb is prohibited. Read EarthWeb's privacy statement. |