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

Chapter Review

In this chapter you have seen how to use the Ada programming language to perform some fundamental operations. You learned how to instruct the computer to read information into memory, perform some simple computations, and display the results of those computations. All of this was done using symbols (punctuation marks, variable names, and special operators such as *, -, and + ) that are familiar, easy to remember, and easy to use. You have also learned a bit about data types, a very important concept in developing programs whose results make sense. You do not have to know very much about your computer in order to understand and use Ada.

In the remainder of this text we introduce more features of the Ada language and provide rules for using these features. You must remember throughout that, unlike the rules of English, the rules of Ada--like those of any computer language--must be followed precisely. The compiler will be unable to translate Ada instructions that violate these rules. Remember to declare every identifier used as a variable or constant and to terminate program statements with semicolons.

New Ada Constructs

Table 2.7 describes the new Ada constructs introduced in this chapter.

Table 2.7
Summary of New Ada Constructs

Construct					Effect

Context Clause
WITH Ada.Text_IO;				indicates that package
Ada.Text_IO					is used by the program

Program Heading

PROCEDURE Payroll IS				identifies Payroll as the
						name of the program

Constant declaration

Tax : CONSTANT Float := 25.00;			associates the constant, Tax, 
						with the Float value 25.00

Star : CONSTANT Character := '*'; associates the constant, Star, with the Character value '*'

Variable declaration

X: Float;					allocates a memory cell named X
						for storage of Float numbers
Me : Integer;					allocates a memory
						cell named Me for storage of
						Integer numbers

Assignment Statement

Distance := Speed * Time;			assigns the product of Speed and
						Time as the value of Distance.

Input Statements

Ada.Text_IO.Get(Item=>Initial);			enters data into the
						character variable Initial

Ada.Integer_Text_IO.Get(Item=>HowMany); enters data into the integer variable HowMany
Ada.Float_Text_IO.Get(Item=>PayRate); enters data into the float variable PayRate

Output Statements

Ada.Text_IO.Put(Item=>Initial);			displays the value of the
						character variable Initial
Ada.Integer_Text_IO.Put(Item=>HowMany, Width=>5);
						displays the value of the integer 
						variable HowMany, using five
						columns on the display
Ada.Float_Text_IO.Put(Item=>GrossPay, Fore=>4, Aft=>2,Exp=>0);
						displays the value of the float variable
						PayRate using four columns 
						before the decimal point and two
						columns after the decimal point

Quick-Check Exercises

  1. What value is assigned to X by the following statement?
    X := 25.0 * 3.0 / 2.5;
    
  2. What value is assigned to X by the following statement?
    X := X - 20.0;
    
  3. Show the exact form of the output displayed when X is 3.456.
    Ada.Text_IO.Put(Item => "Three values of X are");
    Ada.Float_Text_IO.Put(Item => X, Fore => 2, Aft => 1, Exp => 0);
    Ada.Text_IO.Put(Item => '*');
    Ada.Float_Text_IO.Put(Item => X, Fore => 1, Aft => 2, Exp => 0);
    Ada.Text_IO.Put(Item => '*');
    Ada.Float_Text_IO.Put(Item => X, Fore => 2, Aft => 3, Exp => 0);
    Ada.Text_IO.New_Line;
    
  4. Show the exact form of the output displayed when N is 345.
    Ada.Text_IO.Put(Item => "Three values of N are");
    Ada.Integer_Text_IO.Put(Item => N, Width => 4);
    Ada.Text_IO.Put(Item => '*');
    Ada.Float_Text_IO.Put(Item => N, Width => 5);
    Ada.Text_IO.Put(Item => '*');
    Ada.Float_Text_IO.Put(Item => N, Width => 1);
    Ada.Text_IO.New_Line;
    
  5. What data type would you use to represent each of the following items: number of children at school, a letter grade on an exam, the average number of school days students are absent each year?
  6. Suppose Ada.Integer_Text_IO.Get is called twice in succession, for example
    Ada.Integer_Text_IO.Get(Item => X);
    Ada.Integer_Text_IO.Get(Item => Y);
    
    What character(s) may be typed after the first number is entered? What may be typed after the second number is entered?
  7. Suppose Ada.Integer_Text_IO.Get is called twice in succession, for example
    Ada.Text_IO.Get(Item => X);
    Ada.Text_IO.Get(Item => Y);
    
    What happens if a blank is entered after the first character? What happens if RETURN is pressed after the first character?
  8. What kind of errors does a compilation listing show?

Answers to Quick-Check Exercises

  1. 30.0
  2. 10.0
  3. Three values of X are 3.5*3.46* 3.456
  4. Three values of N are 345* 345*345
  5. Natural, Character, Float (or NonNegFloat)
  6. Any number of blanks and/or RETURNs; same
  7. The blank will be read into Y; the RETURN will be skipped and the next character (if it is not a RETURN) will be read into Y.
  8. Compilation errors: syntax and semantic errors.

Programming Projects

  1. Write a program to convert a temperature in degrees Fahrenheit to degrees Celsius. Use the formula
    Celsius = (5/9) × (Fahrenheit - 32)
  2. Write a program that reads three data items into variables X, Y, and Z and then finds and displays their product and sum.
  3. Write a program that reads in the weight (in pounds) of an object and then computes and displays its weight in kilograms and grams. (Hint: One pound is equal to 0.453592 kilograms or 453.59237 grams.)
  4. Write a program that displays your first initial as a large block letter. (Hint: Use a 6 x 6 grid for the letter and print six strings. Each string should consist of a row of *s interspersed with blanks.)
  5. A track star competes in a 1-mile race. Write a program that reads in the race time in minutes (Minutes) and seconds (Seconds) for this runner and then computes and displays the speed in feet per second (FPS) and in meters per second (MPS). (Hint: There are 5280 feet in 1 mile and 1 kilometer equals 3282 feet.) Test your program on each of the times below.
    	Minutes	Seconds 
    	3 		52.83 
    	3 		59.83 
    	4 		00.03 
    	4 		16.22 
    
  6. A cyclist coasting on a level road slows from a speed of 10 miles per hour to 2.5 miles per hour in one minute. Write a computer program that calculates the cyclist's constant rate of acceleration and determines how long it will take the cyclist to come to rest, given an initial speed of 10 miles per hour ( Hint: Use the equation a = (vf - vi) / t, where a is acceleration, t is time interval, vi is the initial velocity, and vf is the final velocity.)
  7. If a human heart beats on the average of once a second for 78 years, how many times does the heart beat in a lifetime? (Use 365.25 for days in a year.) Rerun your program for a heart rate of 75 beats per minute.


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

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