1 / 11

Arrays in Turing

Arrays in Turing. Need for Arrays - Why?. Currently: every variable can contain only one data item at a time Currently: previous piece of information stored will be replaced This is called destructive input. Destructive Input. Example: var name : string name := "Fred Flintstone

rhett
Download Presentation

Arrays in Turing

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Arrays in Turing

  2. Need for Arrays - Why? • Currently: every variable can contain only one data item at a time • Currently: previous piece of information stored will be replaced • This is called destructive input

  3. Destructive Input Example: • var name : string name := "Fred Flintstone • put "The name is ", name, "." • name := "Barney Rubble" • put "The contents of name is ", name, "." • name := "George Jetson" • put "The contents of name is now ", name, "."

  4. Arrays • Provide a mechanism so that all contents can be stored under one variable • No more destructive input • Each element is accessed by the name of the variable and number of the content (subscript)

  5. Arrays: Ordering • Use arrays to organize data in a specific order (e.g. alphabetical/numerical) • Sorting requires the items to be available in memory simultaneously for comparison

  6. Declaration of an Array • The following are sample declarations: • var names : array 1..4 of string%4 strings accessed as names(1), names(2), names(3), names(4) • var nums : array 0..3 of int%4 integers accessed as nums(0), nums(1), nums(2), nums(3) • var realnums : array 11..14 of real%4 reals accessed by realnums(11), realnums(12), realnums(13), realnums(14) • var status : array 1..4 of boolean%4 booleans accessed by status(1), status(2), status(3), status(4)

  7. Declaration of Arrays(Cont’d) • Storing or accessing information to or from an array must include assigning to the variable name along with the subscript.

  8. Example Program Using an Array The following program: • declares an array of 10 integers • randomly generates numbers between 1 and 100 • stores them into the array • displays the numbers in the order in which they were generated

  9. Continued... The Program also: • displays the numbers in the reverse order (last to first generated) • then calculates and displays the average of the 10 numbers. • outputs the contents of the fifth element in the array.

  10. The Program % declaring the array of 10 integers var nums : array 1..10 of int randomize % randomly generating 10 numbers between 1 and 100 and % storing them in the array for i : 1..10 randint (nums (i), 1, 100) end for

  11. ... % output the list from the first to tenth element. As i % increases from 1 to 10, it serves as the subscript to % identify which array element we want. On the first % iteration of the counted loop, i has a value of 1 so % nums(i) is accessing the contents of num(1), etc. put "The list of numbers between 1 and 100 that has been “.. put “generated is” for i : 1..10 put nums(i) end for

More Related