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.
The new Ada constructs introduced in this chapter are given in Table 9.1
Table 9.1
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
Open
, Create
<eol>
, text, file terminator or
<eof>
Ada.Text_IO.End_Error
is raised.
Ada.Text_IO.Name_Error
is raised.
Ada.Text_IO.Mode_Error
is raised.That's all, folks
should be displayed after the last
number.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.
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.)
SkipBlanks
, that skips over a sequence of blanks between words.
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
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
Copyright © 1996 by Addison-Wesley Publishing Company, Inc.