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

Chapter Review

In this chapter you studied the use of Ada strings, which are arrays of characters. A number of useful operations are provided for strings, and several standard packages as well.

You also learned how to instruct a program to read its data from a data file rather than from the keyboard and how to save the output generated by a program as a file on disk. Both techniques make it easier to debug large programs because test data can be prepared in advance and read repeatedly by a program in successive test runs, instead of needing to be entered each time the program is restarted.

New Ada Constructs in Chapter 9

The new Ada constructs introduced in this chapter are given in Table 9.1

Table 9.1
New Ada Constructs

String Declaration

Name : String(1..11);			Declares a string variable 
					Name of length 11.
String Assignment
Name := "Daffy Duck";			Saves "Daffy Duck" in array Name.
String Concatenation
Name := "Jane" & " " & "Jones";		Saves "Jane Jones" in array Name.
String Input
Text_IO.Get_Line(Item=>Name,Last=>L);	Reads a string into Name. Stops if 
					all characters of Name are filled 
					or if the end of the input line is
					reached. The number of characters 
					read is returned in L.
File Declaration, Open and Close
MyInput: Ada.Text_IO.File_Type;			Declares two files.
MyOutput: Ada.Text_IO.File_Type;

Ada.Text_IO.Open 
  (File=>MyInput,Mode=>Ada.Text_IO.In_File,	Attempts to open a
  Name=>"mydata.dat");				data file for input

Ada.Text_IO.Open 
  (File=>MyOutput,Mode=>Ada.Text_IO.Out_File,	Attempts to open a
  Name=>"testoutput.dat");			data file for output
	
Ada.Text_IO.Close(File=>MyInput);		Closes the file.
End of File and End of Line functions
IF Ada.Text_IO.End_Of_File(File=>MyInput) THEN	True if we are at the
						end of the file.

IF Ada.Text_IO.End_Of_Line(File=>MyInput) THEN	True if we are at the
						end of the current line
						in the file

Quick-Check Exercises

  1. The _________ operation prepares a file for input, and the _________operation prepares it for output.
  2. A _________ separates a _________ file into lines, and the ___________ appears at the end of a file.
  3. What happens if a program attempts to read past the file terminator in a file?
  4. What happens if a given file on disk cannot be found by a program?
  5. What happens if a program attempts to write to a file that is open for input (or read from a file that is open for output)?

Answers to Quick-Check Exercises

  1. Open, Create
  2. Line terminator or <eol>, text, file terminator or <eof>
  3. Ada.Text_IO.End_Error is raised.
  4. Ada.Text_IO.Name_Error is raised.
  5. Ada.Text_IO.Mode_Error is raised.

Review Questions for Chapter 9

  1. List three advantages to using files for input and output as opposed to the standard input and output you have used thus far in this course.
  2. Where are files stored?
  3. Explain why a file may have two distinct names. What conventions are followed for choosing each name? Which name appears in the file variable declaration? Which name appears in an operating system command?
  4. Write a loop that reads up to ten integer values from a data file and displays them on the screen. If there are not ten integers in the file, the message That's all, folks should be displayed after the last number.

Programming Projects

  1. The results of a true-false exam given to a computer science class has been coded for input to a program. The information available for each student consists of a student identification number and the student's answers to ten true-false questions. The available data are as follows:
         Student
      identification        Answer string
    
          0080               FTTFTFTTFT
          0340               FTFTFTTTFF
          0341               FTTFTTTTTT
          0401               TTFFTFFTTT
          0462               TTFTTTFFTF
          0463               TTTTTTTTTT
          0464               FTFFTFFTFT
          0512               TFTFTFTFTF
          0618               TTTFFTTFTF
          0619               FFFFFFFFFF
          0687               TFTTFTTFTF
          0700               FTFFTTFFFT
          0712               FTFTFTFTFT
          0837               TFTFTTFTFT
    

    Write a program that first reads in the answer string representing the ten correct answers (use FTFFTFFTFT as data). Next, for each student, read the student's data and compute and store the number of correct answers for each student in one array, and store the student ID number in the corresponding element of another array. Determine the best score, Best. Then print a three-column table displaying the ID number, score, and grade for each student. The grade should be determined as follows: If the score is equal to Best or Best - 1, give an A; if it is Best - 2 or Best - 3, give a C. Otherwise, give an F.

  2. Assume a set of sentences is to be processed. Each sentence consists of a sequence of words, separated by one or more blank spaces. Write a program that will read these sentences and count the number of words that have one letter, two letters, and so on, up to ten letters.
  3. Write an interactive program that plays the game of Hangman. Read the word to be guessed into the string Word. The player must guess the letters belonging to Word. The program should terminate when either all letters have been guessed correctly (player wins) or a specified number of incorrect guesses have been made (computer wins). (Hint: Use a string Solution to keep track of the solution so far. Initialize Solution to a string of symbols '*'. Each time a letter in Word is guessed, replace the corresponding '*' in Solution with that letter.)
  4. Write a program that reads several lines from a data file and writes each word of the file on a separate line of an output file, followed by a count of the number of letters in that word. After all lines are processed, the program should display the number of words processed and data lines read. Assume that words are separated by one or more blanks. Include a procedure, SkipBlanks, that skips over a sequence of blanks between words.
  5. Whatsamata U. offers a service to its faculty in computing grades at the end of each semester. A program processes three weighted test scores and calculates a student's average and letter grade (an A is 90-100, a B is 80-89, and so on). The program reads the student data from a file and writes each student's name, test score, average, and grade to an output file.

    Write a program to provide this valuable service. The data will consist first of the three test weights, then a series of student records, each of which consists of the student's name, ID number (four digits), and the three test scores. Calculate the weighted average for each student and the corresponding grade. This information should be printed along with the initial three test scores. The weighted average for each student is equal to

    weight1 * grade1 + weight2 * grade2 + weight3 * grade3
    

    For summary statistics, print the highest average, the lowest average, the average of the averages, the median average, and the total number of students processed. The median is that average obtained by the "middle" student when the file is sorted by weighted average. To find this, it will be necessary to read the file into an array and store each student's average in a field of the memory record. In this way the array can be sorted. Some sample data follow:

    0.35  0.25  0.40
    Mouse, Minnie 1014 100 76 88
    Duck, Donald 2234 90 85 65
    
  6. An employee time card is represented as one long string of characters. Write a program that processes a collection of these strings stored on a data file and writes the results to an output file. The data string for each employee takes up 42 positions:
    Positions          Data
    
    1-10               Employee's last name
    11-20              Employee's first name
    21                 C for city office or S for suburban office
    22                 U for union or N for nonunion
    23-26              Employee's identification number
    27                 Blank 
    28-29              Number of regular hours (a whole number)
    30                 Blank
    31-36              Hourly rate (dollars and cents)
    37                 Blank
    38-39              Number of dependents
    40                 Blank
    41-42              Number of overtime hours (a whole number)
    

    a. Compute gross pay using the formula

    Gross = regular hours x rate + overtime hours x 1.5 x rate.

    b. Compute net pay by subtracting the following deductions:

    Federal tax = .14 × (gross - 13 × dependents)

    Social Security = 0.052 × gross

    City tax = 4% of gross if employee works in the city

    Union dues = 6.75% of gross for union member


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

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