Previous | Next | Table of Contents | Index | Program List | Copyright

Chapter Review

Summary

In this chapter we studied the record data structure. Records were shown to be useful for organizing a collection of related data items of different types. We were able to create some very general data structures to model our "real world" data organization through the use of hierarchical records.

In processing records, we learned how to reference each individual component through the use of a field selector consisting of the record variable name and field name separated by a period.

Each individual component of a record must be manipulated separately in an input or output operation or in an arithmetic expression. However, it is permissible to assign one record variable to another record variable of the same type (record copy statement), to compare two records of the same type for equality or inequality, and to pass a record as a parameter to a procedure or function.

This chapter also introduced a data structure called an array, a convenient facility for naming and referencing a collection of like items. We discussed how to declare an array type and how to reference an individual array element by placing a subscript in parentheses, following the array name.

The FOR statement enables us to reference easily the elements of an array in sequence. We used FOR statements to initialize arrays, to read and print arrays, and to control the manipulation of individual array elements in sequence.

New Ada Constructs in Chapter 8

The new Ada constructs introduced in this chapter are described in Table 8.6.

Table 8.6
Summary of New Ada Constructs

Statement					Effect

Record Declaration

 
SUBTYPE PartID IS Positive RANGE 1111.9999;
TYPE Part IS RECORD				A record type Part is declared
  ID : PartID;					with fields that can store
  Quantity : Integer;				two integers and a
  Price : Float;				float number. Nuts and
END RECORD;					Bolts are record variables
						of type Part.
Nuts, Bolts : Part;

Record Reference

TotalCost := Nuts.Quantity			Multiplies two fields of Nuts.
             * Nuts.Price;

Ada.Integer_Text_IO.Put(Item=>Bolts.ID);	Displays ID field of Bolts.

Record Copy

Bolts := Nuts;					Copies record Nuts to Bolts.

Record Aggregate Assignment

Bolts := (ID=>2234, Quantity=>53, Price=>0.09);	Assigns values to all fields of Bolts.

Record Compare

IF Nuts = Bolts THEN				Compares Nuts to Bolts.

Array Declaration

TYPE IntArray IS				The data type
  ARRAY (1..10) OF Integer;			describes an array with 10
						type Integer elements. Cube 
Cube, Count : IntArray;				and Count are arrays with
						this structure.

SUBTYPE Index IS Integer RANGE 0..10;		The data type Index is the
						range used as the subscript
Name: ARRAY (Index) OF Character;		type for Name, an array of
						characters.

Array Reference

FOR I IN 1 .. 10 LOOP				Saves I3 in the Ith 
  Cube(I) := I * I * I;				element of array Cube.
END LOOP;

IF Cube(5) > 100 THEN				Compares Cube(5) to 100.

Ada.Integer_Text_IO.Put (Item=>Cube(1),Width=>5);	Displays the first 
							two cubes.
Ada.Integer_Text_IO.Put (Item=>Cube(2),Width=>5);

Array Aggregate Assignment

Count := (3=>29,5=>17,OTHERS => 1);		Sets all elements of Count to 1
						except Count(3) and Count(5).

Array Copy

Count := Cube;					Copies contents of array 
						Cube to array Count.

Array Comparison

IF Count /= Cube THEN				Compares each element of Countto
						the corresponding element of Cube.

IF Count(1..5) = Cube(6..10)			Compares slices of Count and Cube.

Quick-Check Exercises

  1. What is the primary difference between a record and an array? Which would you use to store the catalog description of a course? Which would you use to store the names of students in the course?
  2. What is a field selector?
  3. When can you use the assignment operator with record operands? When can you use the equality operator?
  4. If AStudent is a variable whose type is the record type declared below, provide a program segment that displays the initials of AStudent.
    TYPE Student  IS RECORD
      First: String(1..20);
      Last : String(1..20);
      Age: Natural;
      Score: Natural;
      Grade : Character;
    END RECORD;
    

  5. How many fields are there in a record of type Student?
  6. If an Integer uses two bytes of storage and a character one, how many bytes of storage are occupied by a variable of type Student?
  7. Write a procedure that displays a variable of type Student.
  8. What is a composite structure?
  9. Which predefined types cannot be array subscript types? Array element types?
  10. Can values of different types be stored in an array?
  11. If an array is declared to have ten elements, must the program use all ten?
  12. When can the assignment operator be used with an array as its operands? Answer the same question for the equality operator.
  13. The two methods of array access are ___________ and ___________.
  14. The ___________ loop allows us to access the elements of an array in ____________ order.

Answers to Quick-Check Exercises

  1. The values stored in an array must all be the same type; the values stored in a record do not have to be the same type. Record for catalog item; array for list of names.
  2. Used to select a particular record field for processing.
  3. When the records are the same type; when the records are the same type.
  4. Ada.Text_IO.Put (Item=>AStudent.First(1)); Ada.Text_IO.Put (Item=>AStudent.Last(1));
  5. 5
  6. 45
  7. PROCEDURE WriteStudent (OneStu : Student) IS
    BEGIN
      Ada.Text_IO.Put (Item=>"Student is ");
      Ada.Text_IO.Put (Item=>OneStu.First);
      Ada.Text_IO.Put (Item=>' ');
      Ada.Text_IO.Put (Item=>OneStu.Last);
      Ada.Text_IO.Put (Item=>"; age is ");
      Ada.Integer_Text_IO.Put (Item => OneStu.Age, Width=>1);
      Ada.Text_IO.Put (Item=>"; score is ");
    

    Ada.Integer_Text_IO.Put (Item => OneStu.Score, Width=>1); Ada.Text_IO.Put (Item=>"; grade is "); Ada.Text_IO.Put (Item=>OneStu.Grade); Ada.Text_IO.New_Line; END WriteStudent;

  8. A composite structure is a grouping of related values in main memory.
  9. Float; all can be element types.
  10. No
  11. No
  12. If the arrays are the same type
  13. Direct and sequential
  14. FOR, sequential

Review Questions for Chapter 8

  1. Declare a record called Subscriber, which contains the fields Name, StreetAddress, MonthlyBill (how much the subscriber owes), and which paper the subscriber receives (Morning, Evening, or Both).
  2. Write an Ada program to enter and then display the data in record Competition declared below.
    StringSize: CONSTANT Positive := 20;
    
    TYPE OlympicEvent IS RECORD
    	Event: String(1..StringSize);
    	Entrant: String(1..StringSize);
    	Country : String(1..StringSize);
    	Place : Integer
    END RECORD;
    
    Competition: OlympicEvent;
    
  3. Declare the proper data structure to store the following student data: GPA, Major, Address (consisting of StreetAddress, City, State, and ZipCode) and ClassSchedule (consisting of up to six class records, each of which has Description, Time, and Days fields). Use whatever data types are most appropriate for each field.
  4. a. Identify the error in the following program.
    PROCEDURE P IS
    
      TYPE AnArray IS ARRAY(1..8) OF Integer;
    
      X: AnArray;
      I: Integer;
    
    BEGIN
    
      FOR I IN 1..9 LOOP
        X(I) := I;
      END LOOP;
    
    END P;
    

    b. When will the error be detected? What will the error be?

  5. Declare an array of floats called Week that can be referenced by using any day of the week as a subscript, where Sunday is the first subscript.
  6. Identify the error in the following Ada program.
    PROCEDURE P IS
    
    	TYPE FloatArray IS ARRAY (Character) OF Float;
    
    	X : FloatArray;
    	I : Integer;
    
    BEGIN
    	I := 1;
    	X(I) := 8.384; 
    END P;
    
  7. Is the following Ada program valid?
    PROCEDURE P IS
    
    	TYPE FloatArray IS ARRAY (1..8) OF Float;
    
    	X : FloatArray;
    	I : Integer;
    
    BEGIN
    	I := 1;
    	X(I) := 8.384; 
    END P;
    
  8. What are two common ways of selecting array elements for processing?
  9. Write an Ada program segment to print out the index of the smallest and th largest numbers in an array X of 20 integers with values from 0 to 100. Assume array X already has values assigned to each element.
  10. The parameters for a procedure are two arrays (type FloatArray) and an integer representing the length of the arrays. The procedure copies the first array in the parameter list to the other array in reverse order using a loop structure. Write the procedure.

Programming Projects

  1. A number expressed in scientific notation is represented by its mantissa (a fraction) and its exponent. Write a procedure that reads two character strings representing numbers in Ada scientific notation and stores each number in a record with two fields. Write a procedure that displays the contents of each record as a floating-point value. Also write a procedure that computes the sum, product, difference, and quotient of the two numbers. (Hint: The string -0.1234E20 represents a number in scientific notation. The fraction -0.1234 is the mantissa and the number 20 is the exponent.)
  2. Write a program to read N data items into two arrays X and Y of size 20. Store the product of corresponding elements of X and Y in a third array Z, also of size 20. Display a three-column table showing the arrays X, Y and Z. Then compute and display the square root of the sum of the items in Z. Make up your own data, with N less than 20.
  3. Assume for the moment that your computer has the very limited capability of being able to read and display only single decimal digits at a time, and to add together two integers consisting of one decimal digit each. Write a program to read in two integers of up to ten digits each, add these numbers together, and display the result. Test your program on the following numbers.
      X =  1487625 
      Y =    12783 
    
      X =  60705202 
      Y =  30760832 
    
      X =  1234567890 
      Y =  9876543210
    
    (Hints: Store the numbers X and Y in two arrays X and Y of size 10, one decimal digit per element (type Character). If the number is less than 10 digits in length, enter enough leading zeros (to the left of the number) to make the number 10 digits long.
                   array X
    [1] [2] [3] [4] [5] [6] [7] [8] [9] [10]
     0   0   0   1   4   8   7   6   2   5
    

    array Y [1] [2] [3] [4] [5] [6] [7] [8] [9] [10] 0 0 0 0 0 1 2 7 8 3

    You will need a loop to add together the digits in corresponding array elements, starting with the element with subscript 10. Don't forget to handle the carry if there is one! Use a Boolean variable Carry to indicate whether or not the sum of the last pair of digits is greater than 9.)

  4. Write a program for the following problem: You are given a collection of scores for the last exam in your computer course. You are to compute the average of these scores and then assign grades to each student according to the following rule. If a student's score is within 10 points (above or below) of the average, assign the student a grade of Satisfactory. If the score is more than 10 points higher than the average, assign the student a grade of Outstanding. If the score is more than 10 points below the average, assign the student a grade of Unsatisfactory. (Hint: The output from your program should consist of a labeled three-column list containing the name, exam score, and grade of each student.)
  5. It can be shown that a number is prime if there is no smaller prime number that divides it. Consequently, in order to determine whether N is prime, it is sufficient to check only the prime numbers less than N as possible divisors. Use this information to write a program that stores the first 100 prime numbers in an array. Have your program display the array after it is done.
  6. The results of a survey of the households in your township have been made available. Each record contains data for one household, including a four-digit integer identification number, the annual income for the household, and the number of members of the household. Write a program to read the survey results into three arrays and perform the following analyses:

    a. Count the number of households included in the survey and print a three-column table displaying the data read in. (You may assume that no more than 25 households were surveyed.)

    b. Calculate the average household income, and list the identification number and income of each household that exceeds the average.

    c. Determine the percentage of households having incomes below the poverty level. The poverty level income can be computed using the formula

    p = $6500.00 + $750.00 (m - 2)

    where m is the number of members of each household. This formula shows that the poverty level depends on the number of family members, m, and that the poverty level increases as m gets larger.

    Test your program on the following data.

       Identification number    Annual income	Household members
    
    	1041			$12,180			4 
    	1062			13,240			3 
    	1327			19,800			2 
    	1483			22,458			8 
    	1900			17,000			2 
    	2112			18,125			7 
    	2345			15,623			2 
    	3210			3,200			6 
    	3600			6,500			5 
    	3601			11,970			2 
    	4725			8,900			3 
    	6217			10,000			2
    	9280			6,200			1
    
    


Previous | Next | Table of Contents | Index | Program List | Copyright

Copyright © 1996 by Addison-Wesley Publishing Company, Inc.