DOC PREVIEW
MIT 16 070 - Problem Set #1 Solutions

This preview shows page 1-2-3 out of 10 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 10 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 10 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 10 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 10 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

PSET 1 Solutions Problem 1 Part a: You can generate a listing by using Alt+F2 or by selecting Run, Compile to Listing in the toolbar. A sample listing is shown below. GNAT 3.13p (20000509) Copyright 1992-2000 Free Software Foundation, Inc. Compiling: c:\docume~2\jk\desktop\adatex~1\pset1~1\drunken_spider.adb (source file time stamp: 1998-08-11 14:21:06) 1. WITH Spider; 2. PROCEDURE Drunken_Spider IS 3. ---------------------------------------------------------------- 4. --| Spider tries to tour its room but has drunk too much, so 5. --| takes a random number of steps and may hit the wall. If the 6. --| spider hits the wall, it turns around and keeps going. 7. --| Author: M. B. Feldman, The George Washington University 8. --| Last Modified: July 1998 9. ---------------------------------------------------------------- 10. 11. BEGIN -- Drunken_Spider 12. 13. Spider.Start; 14. 15. LOOP -- keep going forever 16. 17. Spider.Face(WhichWay => Spider.RandomDirection); 18. Spider.ChangeColor(NewColor => Spider.RandomColor); 19. 20. -- Spider will count steps correctly 21. -- but might change direction 22. FOR Count IN 1..Spider.RandomStep LOOP 23. 24. IF Spider.AtWall THEN 25. Spider.TurnRight; 26. Spider.TurnRight; 27. END IF; 28. 29. Spider.Step; 30. 31. END LOOP; 32. 33. Spider.TurnRight; 34. 35. END LOOP; 36. 37. END Drunken_Spider; 38. 38 lines: No errorsPart b: You get an exception either Exception: Spider hit the wall raised SCREEN.WIN32_FILL_SCREEN_ERROR : screen.adb:99 Problem 2: Problem Statement You are taking a vacation in the beautiful country of LaLa Land. You rent a car there, and you're driving on the highway. Then you notice that the distances are measured in furlongs. Each furlong is 1/8 mile (really!). Not only that, but speeds are measured in furlongs per fortnight (fpf). Each fortnight is two weeks or 14 days (really). The highway speed limits are, of course, given in these units. Worse still, the speedometers on the cars show miles per hour (mph) as is used here in the United States. So how do you know if you are exceeding the speed limit? What you need is a quick calculator program, so that if your speedometer reads, for example, 65 mph, you can input this number and the calculator will tell you immediately what your speed is in fpf, so you can compare it with the speed limit signs. Analysis Input : Speedometer reading in miles per hour (mph) Output : Speed in feet per furlong (fpf) Constraints : The fpf reading may not be a whole number so real numbers have to be used. Constants used o 1 mile = 8 furlongs o 1 fortnight = 336 hours Design Algorithm 1. Get the mph reading from the user. 2. Convert the mph reading into fpf using the formula Speed in fpf = Speed in mph * 26883. Display the fpf reading to the user. Refinement 1. Define the input variable as Miles_Per_Hour 2. Define the output variable as Furlong_Per_Fortnight 3. Define the constant conversion factor Mph_To_Fpf to be 2688.0 (Using floating point computation hence the .0 at the end) 4. Get input reading in mph from user. 5. Compute the output value using: Miles_Per_Hour * Mph_To_Fpf 6. Display output to the user. Test Plan Test Case Input Expected Output 1 0 0.0 2 65 174720.0 3 -1 -2688.0 Code Listing GNAT 3.13p (20000509) Copyright 1992-2000 Free Software Foundation, Inc. Compiling: c:\docume~2\jk\desktop\adatex~1\pset1~1\speed_converter.adb (source file time stamp: 2003-02-11 20:45:52) 1. with Ada.Text_Io; 2. with Ada.Float_Text_Io; 3. use Ada.Text_Io; 4. use Ada.Float_Text_Io; 5. 6. ---------------------------------------------------------- 7. -- Program Name : Speed Converter 8. -- Purpose : To convert between miles per hour 9. -- and furlongs per fortnight 10. -- Programmer : Jayakanth Srinivasan 11. -- Date Last Modified : 02/10/03 12. -- Constraints : The assumption is that negative 13. -- values will not be entered 14. ---------------------------------------------------------- 15. 16. procedure Speed_Converter is 17. Mph_To_Fpf : constant := 2688.0;18. -- conversion factor 19. Miles_Per_Hour : Float; 20. Furlong_Per_Fortnight : Float; 21. 22. begin 23. Put(" Please enter the mph reading: "); 24. Get(Item => Miles_Per_Hour); -- get the input value in miles per hour 25. New_Line; 26. 27. Furlong_Per_Fortnight:= Miles_Per_Hour * Mph_To_Fpf;-- compute output 28. 29. Put("Furlong Per Fortnight value = "); 30. Put( 31. Item => Furlong_Per_Fortnight, 32. Fore => 10, 33. Aft => 10, 34. Exp => 0); 35. -- format the output to create 10 places before the decimal point 36. -- and ten places after the decimal point 37. New_Line; 38. 39. end; 39 lines: No errors Tests Test Case Input Expected Output 1 0 0.0 2 65 174720.0 3 -1 -2688.0 Problem 3 Part a: All the outputs are 1. Chapter Review Problem 2a: 10 Chapter Review Problem 2b:0/11 Chapter Review Problem 2c: 10 Part b: Chapter Review Problem 3a: 100 Chapter Review Problem 3b: 011 Chapter Review Problem 3c: 1110Part c: Chapter Review Problem 4 This is a flip flop that is triggered by 0’s rather than 1’s. That is, temporarily changing the upper input to 0 will establish an output of 1, whereas temporarily changing the lower input to 0 will establish an output of 0. To obtain an equivalent circuit with NAND gates, replace every AND-NOT pair with a NAND gate as shown below. Problem 4 Part a: Chapter Review Problem 6 You can have 4096 cells. There are 16*16*16 possible addresses. Part b: Four bits are used to represent a hexadecimal number as seen in Figure 1.6 in the Brookshear book. Chapter Review Problem 7a: BC = 10111100 Chapter Review Problem 7b: 67 = 01100111 Chapter Review Problem 7c: 9A = 10011010 Chapter Review Problem 7d: 10 = 00010000Chapter Review Problem 7e: 3F = 00111111 Part c: Chapter Review Problem 9a: 1010 1010 1010 = AAA Chapter Review Problem 9b: 1100 1011 0111= CB7 Chapter Review Problem 9c: 0000 1110 1011 = 0EB Problem 5 Part a: Chapter Review Problem 24 68 65 78 61 64 65 63 69 6D 61 6C =


View Full Document

MIT 16 070 - Problem Set #1 Solutions

Documents in this Course
optim

optim

20 pages

Load more
Download Problem Set #1 Solutions
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Problem Set #1 Solutions and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Problem Set #1 Solutions 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?