The George Washington University
School of Engineering and Applied Science
Department of Electrical Engineering and Computer Science
CSci 51 -- Spring 1997
My signature on this form indicates that I have read and understood the following rules relating to open-book examinations:
NAME (printed) _______________________________________________
NAME (signed) _______________________________________________
STUDENT ID _______________________ DATE ___________________
Please sign this page and tear it off. We will collect it shortly after the exam begins.
The George Washington University
School of Engineering and Applied Science
Department of Electrical Engineering and Computer Science
CSci 51 -- Spring 1997
MIDTERM EXAMINATION
Open Book--75 minutes = 1 1/4 hour
Your Name _____________________________________________
Student ID_________________________________
READ THIS EXAM CAREFULLY BEFORE WRITING ANYTHING!
You are doing 61 points in 75 minutes!
BUDGET YOUR TIME!
Scoring Summary
1 [_______] (16)
2 [_______] (15)
3 [_______] (20)
4 [_______] (10)
TOTAL [_______] (61)
Given these declarations:
P: Natural;
Q: Float;
Assume P has the value 17 and Q has the value 2.5,
show what will be displayed by each of these program fragments. Treat each fragment
separately; they are not a sequence. If the fragment will lead to a compilation
or execution error, briefly explain the cause of the error. BE CAREFUL!
(A).
P := 3 * 4 / 5;
Ada.Integer_Text_IO.Put(Item => P);
Solution: The expression 3*4/5 is Integer, so the multiplication and division are integer operations and the result is the integer quotient of 12/5, or 2. The value bbbbbbbbbb2 will be displayed, where the b characters represent blanks. The default output field width for an integer value is one more than the width of Integer'Last, or 11.
Solution: The output statement will result in a compilation error, because Ada.Text_IO.Put is used instead of Ada.Integer_Text_IO.Put.
Solution: Again we have an integer expression. The integer quotient of 3/7 is 0, and this is what will be displayed.
Solution: Another compilation error. The 256 is an integer literal, which cannot be directly added to a Float variable.
Solution: The expression 3/7*5 is still Integer. It is evaluated before the conversion to Float, so the result is still 0. Converting this to Float and displaying it with the given formatting parameters results in bbbb0.00, where the b characters represent blanks.
Solution: Here the multiplication is done before the subtraction. We are, in this case, subtracting 3*17 = 51 from 17, which would give a negative result. Because P is declared Natural, it cannot be negative and therefore the exception Constraint_Error is raised at execition time.
Solution: The value of P is converted to Float and stored in Q, which is then displayed. Since P's value is 17, the displayed value will be bbb17.00.
Solution: Now we are doing Float arithmetic, then converting to Integer. The Float result is 2.5; converting to Integer is a rounding operation in which a value in the middle of the interval is rounded away from zero, so the displayed result will be bbbb3.
The Spider package (Program 3.11) provides an operation to make the spider turn right, but none to make it turn left. In this problem you are going to write instructions to cause the spider to turn left. Do this 3 different ways:
A. Write a sequence of three spider commands and no other statements.
Solution:
Spider.Right;
Spider.Right;
Spider.Right;
ELSIF .... THEN
ELSIF .... THEN
ELSE
END IF;
Solution:
IF Spider.IsFacing = Spider.North THEN Spider.Face (WhichWay => Spider.West);
ELSIF Spider.IsFacing = Spider.West THEN Spider.Face (WhichWay => Spider.South);
ELSIF Spider.IsFacing = Spider.South THEN Spider.Face (WhichWay => Spider.East);
ELSE -- Spider must be facing East Spider.Face (WhichWay => Spider.North);
END IF;
IF .... THEN
ELSE
END IF;
Solution:
IF Spider.IsFacing = Spider.Directions'First THEN Spider.Face (WhichWay => Spider.Directions'Last); ELSE Spider.Face (WhichWay => Spider.Directions'Pred(Spider.IsFacing)); END IF;
PROBLEM 3 (20 points total, 10 per part)
Pumpkin Computer produces 4 models: the Wimp, currently priced at $995.99, the Kneetop portable, at $1295.74, the Vanilla at $1595.47, and the TurboScreamer at $2995.11. Because the Pumpkin sales people are always forgetting these prices, they need a program to help them when a customer calls. The program will prompt the user for the desired model, then display the price.
A. Write the first part of this program, down to but not including the main BEGIN. Include context clauses. Use constants for the prices and an enumeration type for the models. This should be approximately ten lines of Ada.
Solution:
1. WITH Ada.Text_IO;
2. WITH Ada.Float_Text_IO;
3. PROCEDURE Pumpkin IS
4.
5. -- problem type
6. TYPE Models IS (Wimp, Kneetop, Vanilla, TurboScreamer);
7.
8. -- input/output instance
9. PACKAGE Models_IO IS
10. NEW Ada.Text_IO.Enumeration_IO (Enum => Models);
11.
12. -- problem constants
13. WimpPrice: CONSTANT Float := 995.99;
14. KneetopPrice: CONSTANT Float := 1295.74;
15. VanillaPrice: CONSTANT Float := 1595.47;
16. TurboPrice: CONSTANT Float := 2995.11;
17.
18. Model: Models; -- input variable
19.
Note: It is tempting to use an enumeration type to represent the prices; unfortunately, enumeration values cannot be numeric in Ada.
B. Write the rest of the program. This should be about fifteen lines of Ada.
20. BEGIN -- Pumpkin
21.
22. -- prompt for input
23. Ada.Text_IO.Put (Item => "Please enter a computer model >");
24. Models_IO.Get (Item => Model);
25.
26. -- select and display output
27. Ada.Text_IO.Put(Item => "This model is priced at $");
28. IF Model = Wimp THEN
29. Ada.Float_Text_IO.Put(Item=>WimpPrice, Fore=>1, Aft=>2, Exp=>0);
30. ELSIF Model = KneeTop THEN
31. Ada.Float_Text_IO.Put(Item=>KneetopPrice, Fore=>1, Aft=>2, Exp=>0);
32. ELSIF Model = Vanilla THEN
33. Ada.Float_Text_IO.Put(Item=>VanillaPrice, Fore=>1, Aft=>2, Exp=>0);
34. ELSE -- Model must be TurboScreamer
35. Ada.Float_Text_IO.Put(Item=>TurboPrice, Fore=>1, Aft=>2, Exp=>0);
36. END IF;
37. Ada.Text_IO.New_Line;
38.
39. END Pumpkin;
39 lines: No errors
A. Briefly explain, in your own words, the purpose in Ada of the construct that begins with WITH (you won't get much credit if you just copy something from the book).
Solution: To get full credit here, you had to mention that WITH applies to packages, and that a WITH statements is a context clause that precedes the beginning of a program unit, and indicates to the compiler which package(s) to reference in compiling the program unit.
Solution: To get full credit here, you had to mention that a type is a
set of values and a set of operations. Types we have studied include
Integer (and various Integer subtypes), Float (and various
Float subtypes), Character, String, and various user-defined
enumeration types.