DOC PREVIEW
MIT 16 070 - Introduction to Computers & Programming

This preview shows page 1-2-3-4-5-6 out of 18 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 18 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 18 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 18 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 18 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 18 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 18 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 18 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

16.070 — February 10/2003 — Prof. I. K. Lundqvist — [email protected] to Computers & ProgrammingAdaProf. I. Kristina LundqvistDept. of Aero/Astro, MIT16.070 — February 10/2003 — Prof. I. K. Lundqvist — [email protected] program§ Display a message: “Hello there. We hope you enjoy studying Ada!”§ Pseudo code: put (Hello there. We hope you enjoy studying Ada!)WITH Ada.Text_IO; -- declare the packagePROCEDURE Hello IS--------------------------------------------------------------| A very simple program; it just displays a greeting.--| Author: Michael Feldman, The George Washington University--| Last Modified: June 1998------------------------------------------------------------BEGIN -- HelloAda.Text_IO.Put(Item => "Hello there. ");Ada.Text_IO.Put(Item => "We hope you enjoy studying Ada!");Ada.Text_IO.New_Line;END Hello;16.070 — February 10/2003 — Prof. I. K. Lundqvist — [email protected] — February 10/2003 — Prof. I. K. Lundqvist — [email protected] Spider;PROCEDURE Walk_Line IS---------------------------------------------------------------| Walk line with spider--| Author: M. B. Feldman, The George Washington University --| Last Modified: July 1998-------------------------------------------------------------BEGIN -- Walk_LineSpider.Start;Spider.Step;Spider.Step;Spider.Step;Spider.Step;Spider.Step;Spider.Quit;END Walk_Line;16.070 — February 10/2003 — Prof. I. K. Lundqvist — [email protected] Spider Package (spider.ads)§ A package is a way to encapsulate, or group, a set of related operations.§ Divided into 2 parts§ Specification .ads“table of contents”§ Body .adbactual program segments§ Standard Ada packages are “built in”, i.e., provided by the compiler§ Standard (+, -, …, characters)§ Character (Is_upper, To_Lower,…)§ Numerics (Sqrt, Log, …)§ Text_IO (get, put, open, …)§ …16.070 — February 10/2003 — Prof. I. K. Lundqvist — [email protected] Spider Package (spider.adb)PROCEDURE Start ISBEGINDrawRoom;CurrentColumn := 10; -- these are in the spider's viewCurrentRow := 11;Heading := North;ChangeColor(NewColor => Green);ShowSpider;ShowDirection;RandomSteps.Reset(Gen => GSteps);RandomColors.Reset(Gen => GColors);RandomDirections.Reset(Gen => GDirections);END Start;16.070 — February 10/2003 — Prof. I. K. Lundqvist — [email protected] walk_boxWITH Spider;PROCEDURE Walk_Box IS-----------------------------------------| Walk 4 x 4 box with spider--| Author: M. B. Feldman, The GWU --| Last Modified: July 1998---------------------------------------BEGIN -- Walk_BoxSpider.Start;Spider.Step;Spider.Step;Spider.Step;Spider.TurnRight;Spider.Step;Spider.Step;Spider.Step;Spider.TurnRight;Spider.Step;Spider.Step;Spider.Step;Spider.TurnRight;Spider.Step;Spider.Step;Spider.Step;Spider.TurnRight;Spider.Quit;END Walk_Box;16.070 — February 10/2003 — Prof. I. K. Lundqvist — [email protected] — February 10/2003 — Prof. I. K. Lundqvist — [email protected] with Single Loop§ Algorithm for drawing box1. Repeat steps a and b 4 timesa. Take 3 steps forwardb. Turn right§ A repetition is usually called a loop§ Use an Ada control structure called the FORconstruct:FOR Side IN 1..4 LOOPandEND LOOP;WITH Spider;PROCEDURE Draw_Box_with_1_Loop IS-----------------------------------------| Draw 4 x 4 box with spider - use loop--| Author: M. B. Feldman, The GWU--| Last Modified: July 1998---------------------------------------BEGIN -- Draw_Box_with_1_LoopSpider.Start;Spider.ChangeColor(NewColor => Spider.Red);FOR Side IN 1..4 LOOPSpider.Step;Spider.Step;Spider.Step;Spider.TurnRight;END LOOP;Spider.Quit;END Draw_Box_with_1_Loop;16.070 — February 10/2003 — Prof. I. K. Lundqvist — [email protected] and Writing Numbers§ Calculate the sum and product of two numbers§ Algorithm:§ Get the 2 numbers§ Ask for the 2 numbers§ Get number1§ Get number2§ Calculate and print the sum§ Print “The sum is “§ Print (number1 + number2)§ Calculate and print the product§ Print “The product is “§ Print (number1 * number2)§ Pseudo codeNumber1: numberNumber2: numberPut (give me two whole numbers)Get (number1)Get (number2)Put (the sum of the numbers is )Put (number1 + number2)Put (the product of the numbers is )Put (number1 * number2)16.070 — February 10/2003 — Prof. I. K. Lundqvist — [email protected]_prod§ --------------------------------------------------------- sum_prod - sum and product, Skansholm #2.4.2 -------------------------------------------------------with Text_Io; -- specify packages we depend on use Text_Io;procedure Sum_Prod is -- declare integer I/O packagepackage Int_Io is new Text_Io.Integer_Io(Integer);use Int_Io;-- declare any constants and variables requiredNumber1, Number2 : Integer; -- numbers used begin -- sum_prod -- ask user for numbers and read them Put_Line("Give me two whole numbers!");Get(Number1);Get(Number2); -- display sum and product of numbers Put("The sum of the numbers is:");Put(Number1+Number2);New_Line;Put("The product of the numbers is:");Put(Number1*Number2);New_Line;end Sum_Prod;16.070 — February 10/2003 — Prof. I. K. Lundqvist — [email protected] — February 10/2003 — Prof. I. K. Lundqvist — [email protected] solutionwith Text_Io; use Text_Io;procedure Sum_Prod is -- declare integer I/O package package Int_Io is new Text_Io.Integer_Io( Integer );use Int_Io;-- declare any constants and variables required Number1, Number2, Total, Product : Integer; begin -- sum_prod -- ask user for numbers and read them Put ( "Please enter the first number ");Get ( Number1 ); Skip_Line;Put ( "Please enter the second number ");Get ( Number2 ); Skip_Line;-- display sum and product of numbers Total := Number1 + Number2;Product := Number1 * Number2;Put("The sum of the numbers is:");Put(Total, Width=>7); New_Line;Put("The product of the numbers is:");Put(Product, Width=>3); New_Line;end Sum_Prod;Layout conventionsone statement (thought) per linebreak long lines into readable segmentsindent lines to show different parts of the programblank lines separate parts of the programcomments/header to help readers understand the program16.070 — February 10/2003 — Prof. I. K. Lundqvist — [email protected]§ Lines starting with -- are ignored by the compiler. Only there to help someone reading the program § Good comments: § are always correct and up to date § conform to usual conventions of prose §


View Full Document

MIT 16 070 - Introduction to Computers & Programming

Documents in this Course
optim

optim

20 pages

Load more
Download Introduction to Computers & Programming
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 Introduction to Computers & Programming 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 Introduction to Computers & Programming 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?