Click here for ObjectSpace: Business- to- Business Integration Company
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 Students

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

1.  Two types of arrays were declared in the TYPE section, a two-dimensional array “ScoreArray” and a one-dimensional array “AverageArray.” These type identifiers are used in the VAR section to declare the two arrays “Score” and “Average.” The first array is used to store the scores of the four students in three classes, while the second is used to hold the averages of the four students (which are, of course, only four values).
2.  Data are read through two loops, using the index “StudentCount” as a counter of students in the outer loop and “ScoreCount” as a counter of scores in the inner loop. Each value read from the keyboard is assigned to the general array variable:
Score[StudentCount, ScoreCount]

The exact location of the array element is determined by the two indexes “StudentCount” and “ScoreCount.”
3.  The average of scores is calculated for each student and stored in the array variable:
Average[StudentCount]

The index “StudentCount” indicates which student has each average.
4.  Notice the initialization of the variable “SumOfScores” before the average calculation. This is a very important step because if it is not done, the average of the previous student will remain in the variable and be added to the new average.

Array Initialization

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

Drill 5-5

Modify the last program to display the students’ names in descending order according to their scores, as in this example:

    Student name    Average
    -----------------
    Porter, Thomas  84.00
    Dalton, Jack    83.33
    Dixon, Jane     83.33
    Bobbin, Dale    66.67
    -----------------

Summary

In this chapter you have had a review of simple and structured data types.

1.  You now know that simple data types are classified as either “real” or “ordinal” types. Of the ordinal types, you learned how to use the user-defined types, enumerations and subranges.
2.  You learned how to use the TYPE statement to declare a new type or rename a predefined type. It takes the general form:
    TYPE
    type-name = type-definition;

In standard Pascal the relative sequence of the TYPE section among the other sections in the declaration part is as follows:
    LABEL section
    CONST section
    TYPE section
    VAR section
3.  You learned about the array as a predefined structured data type that may be declared either in the TYPE section or VAR section. You also learned how to declare and use both one- and two-dimensional arrays. The general form to declare an array of any number of dimensions (in the TYPE section) is:
    TYPE
    type-name = ARRAY[index-range-1, index-range-2,
             ..., index-range-n] OF element-type;


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.