home account info subscribe login search My ITKnowledge FAQ/help site map contact us


 
Brief Full
 Advanced
      Search
 Search Tips
To access the contents, click the chapter and section titles.

Learn Pascal in a Three Days (2nd Ed.)
(Publisher: Wordware Publishing, Inc.)
Author(s):
ISBN: 1556225679
Publication Date: 07/01/97

Bookmark It

Search this book:
 
Previous Table of Contents Next


Example: Scores of One Student

In 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:

1.  The size of the array is declared as a constant (NumberOfClasses).
2.  The index-range of the array is declared using the previously defined constant “NumberOfClasses” as follows:
Score:ARRAY[1..NumberOfClasses] OF REAL;

This is the same as:
Score:ARRAY[1..6] OF REAL;

The first declaration, however, is much better, because if you would like to process a different number of classes, you just change the value of the constant “NumberOfClasses” without modifying the program main body.
3.  Notice that after the program reads the scores, they are stored in the array elements and are available in memory. This means that the sum can be processed later in the program. When you calculated the sum and the average of some numbers before (program 4-4), you had to accumulate the values during data entry in one variable “Sum.” Now, you have six variables.
4.  The index of the array is used as a counter in the FOR loops, both for reading data and calculating the sum. Actually, the index of the array is very useful for displaying results, especially if you like to display the results in table form.

Displaying Tabulated Results

The 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.

Drill 5-3

Write a Pascal program to read and store the scores of five students in one test, then display the output as shown below:

    Student #       Score
    -----------------
     1          90.00
     2          88.00
     3          91.00
     4          78.00
     5          75.00
    -----------------
    Average score = 84.40


Previous Table of Contents Next


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.