Page 77 - DCAP201_FUNDAMENTALS_OF_DATA_STRUCTURES
P. 77

Fundamentals of Data Structures




                    Notes               2   Test(3)
                                        2   Final
                                        2   Grade
                                   The identifier Student(20) indicates that there are 20 students. The identifier Test (3) indicates
                                   that there are three tests per student. Observe that there are 8 elementary items per Student,
                                   since test is counted three times. Altogether, there are 160 elementary items in the entire Student
                                   structure.

                                   5.3.1 Indexing Items in a Record

                                   Suppose we want to access some data item in a record. In some cases, we cannot simply write the
                                   data name of the item since the same name may appear in different places in the record.





                                     Notes  Accordingly, in order to specify a particular item, we may have to qualify the name
                                     by using appropriate group item names in the structure. This qualification is indicated by
                                     using decimal points to separate group items from sub items.


                                          Example:
                                   (a)  Consider the record structure Newborn in the example of newborn given above. Sex and
                                       year need no qualification, since each refers to a unique item in the structure. On the other
                                       hand, suppose we want to refer to the age of the father. This can be done by writing
                                       Newborn.Father.Age              or simply     Father.Age

                                       The first reference is said to be fully qualified. Sometimes one adds qualifying identifiers
                                       for clarity.
                                   (b)  Suppose the first line in the record structure in the example of newborn given above is
                                       replaced by
                                       1 Newborn(20)
                                       That is, Newborn is defined to be a file with 20 records. Then every item automatically
                                       becomes a 20-element array. Some languages allow the sex of the sixth newborn to be
                                       referenced by writing
                                          Newborn.Sex[6] or simple     Sex[6]
                                       Analogously, the age of the father of the sixth newborn may be referenced by writing

                                          Newborn. Father. Age[6] or simply   Father.Age[6]
                                   (c)  Consider the record structure Student in the student example given above. Since Student
                                       is declared to be a file with 20 students, all items automatically become 20-element arrays.
                                       Furthermore, Test becomes a two-dimensional array. In particular, the second test of the
                                       sixth student may be referenced by writing
                                          Student. Test[6, 2]  or simply      Test[6, 2]

                                       The order of the subscripts corresponds to the order of the qualifying identifiers. For
                                       example,
                                                                     Test[3,1]
                                       does not refer to the third test of the first student, but to the first test of the third student.




          70                                LOVELY PROFESSIONAL UNIVERSITY
   72   73   74   75   76   77   78   79   80   81   82