Arrays in Turing - PowerPoint PPT Presentation

1 / 11
About This Presentation
Title:

Arrays in Turing

Description:

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 ... – PowerPoint PPT presentation

Number of Views:45
Avg rating:3.0/5.0
Slides: 12
Provided by: richard1990
Category:
Tags: arrays | turing

less

Transcript and Presenter's Notes

Title: Arrays in Turing


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 string4 strings
    accessed as names(1), names(2), names(3),
    names(4)
  • var nums array 0..3 of int4 integers accessed
    as nums(0), nums(1), nums(2), nums(3)
  • var realnums array 11..14 of real4 reals
    accessed by realnums(11), realnums(12),
    realnums(13), realnums(14)
  • var status array 1..4 of boolean4 booleans
    accessed by status(1), status(2), status(3),
    status(4)

7
Declaration of Arrays (Contd)
  • 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
Write a Comment
User Comments (0)
About PowerShow.com