|
To access the contents, click the chapter and section titles.
Learn Pascal in a Three Days (2nd Ed.)
Example: Scores of One StudentIn the following program the array Score is used to store the scores of one student in six different classes. The scores are entered from the keyboard, then the sum and average of the scores are displayed. { -------------------- figure 5-3 -------------------- } PROGRAM Scores1(INPUT,OUTPUT); CONST NumberOfClasses = 6; VAR Score:ARRAY[1..NumberOfClasses] OF REAL; Average, SumOfScores:REAL; Index :INTEGER; BEGIN { Read the scores array } { --------------------- } FOR Index:= 1 TO NumberOfClasses DO BEGIN WRITE('Enter score for class #', Index,': '); READLN(Score[Index]) END; { Calculate the sum } { ----------------- } SumOfScores:= 0; FOR Index:= 1 TO NumberOfClasses DO SumOfScores:= SumOfScores + Score[Index]; { Calculate the average } { --------------------- } Average:= SumOfScores / NumberOfClasses; { Display Results } { --------------- } WRITELN; WRITELN('Sum of scores = ', SumOfScores:0:2); WRITELN('Average of scores = ', Average:0:2); WRITELN; WRITELN('Press ENTER to continue..'); READLN END. A sample run of the program gives the following: Enter score for class #1: 90 Enter score for class #2: 80 Enter score for class #3: 85 Enter score for class #4: 75 Enter score for class #5: 89 Enter score for class #6: 91 Sum of scores = 510.00 Average of scores = 85.00 Press ENTER to continue.. The following points in this program are worthy of your attention:
Displaying Tabulated ResultsThe following program deals with the same problem but displays the results in a tabulated form. { -------------------- figure 5-4 -------------------- } PROGRAM Scores2(INPUT,OUTPUT); CONST NumberOfClasses = 6; Tab = ' '; { 9 spaces } VAR Score:ARRAY[1..NumberOfClasses] OF REAL; Average, SumOfScores:REAL; Index :INTEGER; BEGIN { Read the scores array } { --------------------- } FOR Index:= 1 TO NumberOfClasses DO BEGIN WRITE('Enter score for class #', Index,': '); READLN(Score[Index]) END; { Calculate the sum } { ----------------- } SumOfScores:= 0; FOR Index:= 1 TO NumberOfClasses DO SumOfScores:= SumOfScores + Score[Index]; { Calculate the average } { --------------------- } Average:= SumOfScores / NumberOfClasses; { Display Results } { --------------- } WRITELN; WRITELN(Tab,'CLASS #'); WRITE(' '); { 6 spaces } FOR Index:= 1 TO NumberOfClasses DO WRITE(Index:7); WRITELN; WRITE(Tab); FOR Index:= 1 TO NumberOfClasses DO WRITE('-------'); WRITELN; WRITE('SCORES '); FOR Index:= 1 TO NumberOfClasses DO WRITE(Score[Index]:7:2); WRITELN; WRITE(Tab); FOR Index:= 1 TO NumberOfClasses DO WRITE('-------'); WRITELN; WRITELN(Tab,'Sum of scores = ', SumOfScores:0:2); WRITELN(Tab,'Average of scores = ', Average:0:2); WRITELN; WRITELN('Press ENTER to continue..'); READLN END. This is a sample run: Enter score for class #1: 90.5 Enter score for class #2: 80.5 Enter score for class #3: 86.2 Enter score for class #4: 90.3 Enter score for class #5: 74.8 Enter score for class #6: 98.5 CLASS # 1 2 3 4 5 6 ------------------------- SCORES 90.50 80.50 86.20 90.30 74.80 98.50 ------------------------- Sum of scores = 520.80 Average of scores = 86.80 Press ENTER to continue.. In this program extensive use of loops has been made to display the dashed lines, the class numbers, and the scores; this makes the program more generic. For example, the dashed line could be displayed using the statement: WRITELN(' ------------------------------------------'); This is useful only for six classes, but the following statements: WRITE(Tab); FOR Index:= 1 TO NumberOfClasses DO WRITE('-------'); are useful for any number of classes, because a seven-dash segment is displayed for each class. Thus, if you had only four classes, the output would look like this: CLASS # 1 2 3 4 -------------------- SCORES 80.00 90.00 85.00 75.00 -------------------- Sum of scores = 330.00 Average of scores = 82.50 Notice that the number of dashes is equal to the field width specified in the output format of Score and Index: WRITE(Index:7); WRITE(Score[Index]:7:2); Note also the use of the constant Tab (which contains nine spaces) for proper indentation of the output. A weak point of this program is that we have to repeat the same lines of code every time we want to draw a line. Such repetitive tasks can instead be programmed separately as procedures and called whenever wanted. This is discussed later in the book.
|
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. |