- 443 Views
- Uploaded on

Download Presentation
## PowerPoint Slideshow about 'Arrays in Turing' - mike_john

**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.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.

- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -

Presentation Transcript

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
- 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, "."

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)

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

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)

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.

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

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.

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

...

% 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

Download Presentation

Connecting to Server..