Arrays - PowerPoint PPT Presentation

About This Presentation
Title:

Arrays

Description:

What would the printing code look like? How about finding the max? Arrays ... Write a method that takes an integer array and prints the values ... – PowerPoint PPT presentation

Number of Views:73
Avg rating:3.0/5.0
Slides: 55
Provided by: thaddeusf
Category:
Tags: arrays | prints

less

Transcript and Presenter's Notes

Title: Arrays


1
Arrays
  • CSC 171 FALL 2001
  • LECTURE 10

2
History Alan Turing Founder of Computer Science
  • 1937 - Alan Turing developed the idea of a
    "Universal Machine" capable of executing any
    describable algorithm, and forming the basis for
    the concept of "computability".
  • Turing's ideas differed from those of others who
    were solving arithmetic problems by introducing
    the concept of "symbol processing".

3
The Turing Machine
Tape memory (movable)
a
c
a
b
c
b
b
a
a
Read head
q0
h
q1
q3
q2
State machine
4
The Turing Machine
  • A Turing machine is a quintuple (K,S,d,s,H)
  • K is a finite set of states
  • S is an alphabet of symbols
  • s is the initial state
  • H is the halting state
  • d is the transition function
  • From (K-H) x S
  • To K x (S U move_tape_left, move_tape_right)

5
The Turing Test
  • Turing put forward the idea of an 'imitation
    game', in which a human being and a computer
    would be interrogated under conditions where the
    interrogator would not know which was which, the
    communication being entirely by textual messages.
  • Turing argued that if the interrogator could not
    distinguish them by questioning, then it would be
    unreasonable not to call the computer
    intelligent.
  • Turing's 'imitation game' is now usually called
    'the Turing test' for intelligence.

6
Arrays
  • Suppose we want to write a program that reads a
    set of test grades and prints them, marking the
    highest grade?
  • 65.2
  • 81.7
  • 31.3
  • 95.4 lt - highest grade
  • 76.1
  • 58.6

7
Individual data items
  • If we knew that there were always 150 students in
    the class, we could store the data as individual
    variables
  • score1,score2,score3, . . , score150
  • What would the data entry code look like?
  • What would the printing code look like?
  • How about finding the max?

8
(No Transcript)
9
Arrays
  • An array is a collection of data items of the
    same type
  • Every element of the collection can be accessed
    separately.

10
Constructing Arrays
  • double data new double10

11
Setting Array values
  • To get values into an array you need to specify
    which slot you want to use.
  • Specification is done with the operator
  • The operator follows the name of the array
  • The operator encloses and integer-valued
    expression called the index or subscript

12
Setting array values
  • data4 29.95

13
Using array values
  • Similar to setting
  • int i 4
  • System.out.println(datai datai)
  • gt data4 29.95

14
Array data items
  • Suppose we want to write a program that reads a
    set of test grades and prints them, marking the
    highest grade?
  • What would the data entry code look like?
  • What would the printing code look like?
  • How about finding the max?

15
Array issues
  • Does it work?
  • double data new double10
  • data10 5.4

16
Array issues
  • Does it work?
  • double data new double10
  • data10 5.4
  • When the program runs, an out-of-bounds subscript
    generates an exception and terminates the program
    why?

17
Array issues
  • Can we search for the top grade as follows
  • double maxScore data0
  • for (int i 1Iltdata.lengthi)
  • If (datai gt maxScore)
  • maxScore datai

18
Array issues
  • An array of length n has index values from 0 to
    (n-1)
  • double maxScore data0
  • for (int i 1Iltdata.lengthi)
  • If (datai gt maxScore)
  • maxScore datai

19
Array issues
  • Does it work?
  • public static void main(String args) double
    data
  • If (data0 gt 4.0)
  • System.out.println(GT 4!)

20
Array issues
  • Arrays must be allocated!
  • public static void main(String args) double
    data new double10
  • If (data0 gt 4.0)
  • System.out.println(GT 4!)

21
Array issues
  • Arrays can be initialized!
  • public static void main(String args) double
    data 2,3,4,5,6
  • If (data0 gt 4.0)
  • System.out.println(GT 4!)
  • // note new int 2,3,4,5,6 is also legal

22
Copying Arrays
  • Is this ok?
  • double data new double10
  • double testScores
  • testScores data

23
Copying Arrays
  • Is this ok?
  • double data new double10
  • double testScores
  • testScores data
  • How many arrays are there?

24
Copying Array REFERENCES
  • double data new double10
  • double testScores
  • testScores data

25
Copying Arrays
  • So, what if we want to make two real copies
    what does the code look like?
  • Write a method so that
  • int x 3,4,5,6,7,8,9,10
  • int y myCopy(x)

26
Copying
  • public static int myCopy(int x)
  • int r_arry new intx.length
  • for (int i 0 ilt x.lengthi)
  • r_arryi xi
  • return r_arry

27
System.arrayCopy
  • //System.arraycopy(from,fromstart,to,toStart,count
    )
  • System.arraycopy(data,0,prices,0,data.length)

28
(No Transcript)
29
History Colossus Bletchley Park
  • 1940 A major need for supporting the war effort
    was to decrypt the intercepted messages of the
    German forces. Encrypted in the early years using
    the US designed ENIGMA, a team at Bletchley Park,
    built a series of machines culminating in 1943
    with Colossus.

30
More Arrays
  • Suppose we want to write a program that reads a
    set of test product names, prices, and quality
    scores prints them, marking the best value?
    (score/prices)
  • Digital 500X, 3499.00, score 73
  • ZEOS Pentium-III/500, 2545.00, score 70
  • Micro Express MF, 2195.00, score 72 lt - best
    value
  • Polywell Poly 450IP, 2099.00, score 30

31
Parallel Arrays
  • One solution, can you think of a better one?

32
Arrays of Objects
  • Easier to deal with - arrays hold references

33
Multidimensional Arrays
  • Arrays of arrays
  • Arrays are objects
  • Arrays hold references to objects
  • Ergo, arrays can hold arrays

34
Tables are 2D arrays
  • int size 5
  • int mtable new intsizesize
  • for(i0iltsizei)
  • for(int j0jltsizej)
  • mtableij (i1)(j1)
  • // how easy to modify code ????
  • int mtable new int55
  • for(i0ilt5i)
  • for(int j0jlt5j)
  • mtableij (i1)(j1)

35
Arrays of Arrays
  • int powers new int1010

36
Allocating arrays of arrays
  • int size 10
  • int mtable new intsize
  • for(i0iltmtable.lengthi)
  • mtablei new intsize
  • for(int j0jltmtablei.lengthj)
  • mtableij (i1)(j1)

37
Alternately
  • The following is legal.
  • What is the structure?
  • int b new int5
  • for (int i0iltb.lengthi)
  • bi new inti1

38
Alternately
  • int b new int5
  • for (int i0iltb.lengthi)
  • bi new inti1
  • This is known as a triangular array
  • Is b31 a legal reference or b13?

39
Passing Arrays
  • In JAVA
  • Primitives are passed by value
  • A copy of the variable is made used
  • Modifications made do not affect calling value
  • public void myAdd(int x) x
  • Objects are passed by reference
  • Since the reference is passed it is possible to
    change the value in the calling method
  • Public void myAdd (int x) x0

40
Pass by value
  • public class passArry
  • public static void main(String args)
  • int x1 3
  • System.out.println("x1 "x1)
  • myAdd(x1)
  • System.out.println("x1 "x1)
  • public static void myAdd(int x) x

41
Pass by reference
  • public class passArry
  • public static void main(String args)
  • int x2 4,5,6
  • System.out.println("x20 "x20)
  • myAdd(x2)
  • System.out.println("x20 "x20)
  • public static void myAdd(int x) x0

42
Exercise
  • Write a method that takes an integer array and
    prints the values

43
Exercise
  • Write a method that takes an integer array and
    prints the values
  • public static int myPrint(int x)
  • for (int i 0 iltx.lengthi)
  • System.out.println(x i xi)

44
Exercise
  • Write a method that takes an integer array, and
    two integer indices and swaps the value

45
Exercise
  • Write a method that takes an integer array, and
    two integer indices and swaps the value
  • public static void mySwap(int x, int i, int j)
  • int temp xi
  • xi xj
  • xj temp

46
Exercise
  • Write a method that takes an integer array and
    returns the index of the maximum value

47
Exercise
  • Write a method that takes an integer array and
    returns the index of the maximum value
  • public static int myMax(int x)
  • int rvalue 0
  • for (int i 0 iltx.lengthi)
  • if (xi gt xrvalue) rvalue i
  • return rvalue

48
Exercise
  • Overload the max finder to take an array and an
    index the method now returns the index of the
    maximum value gt the index passed in

49
Exercise
  • Overload the max finder to take an array and an
    index the method now returns the index of the
    maximum value gt the index passed in
  • public static int myMax(int x, int j)
  • int rvalue j
  • for (int i j iltx.lengthi)
  • if (xi gt xrvalue) rvalue i
  • return rvalue

50
Sorting
  • Write a method that takes an integer array
  • Loop through all the positions in the array, one
    after the other
  • At each (current) position, find the max from
    that position to the rest of the array.
  • Swap the value with the current position
  • This is termed selection sort

51
Selection Sort
  • Does it work?
  • public void selSort(int x)
  • for (int i 0 iltx.lengthi)
  • int temp myMax(x,i)
  • mySwap(x,i,temp)
  • Can you prove it works?
  • What is the loop invariant?

52
Sorting
  • Write a method that takes an integer array
  • Loop through all the positions in the array, one
    after the other
  • At each (current) position, compare the value to
    the next in line.
  • If the next is gt the current swap the value with
    the current position
  • How many times do I have to do this?
  • This is termed bubble sort

53
Bubble Sort
  • Does it work?
  • public void bubSort(int x)
  • for (int i 0 iltx.lengthi)
  • for (int j 0 jlt(x.length-1)j)
  • If (x j lt x j1 )
  • mySwap(x,j,j1)

54
Question to ponder
  • Which is better
  • bubble sort
  • selection sort
  • What do you mean by better?
  • Running time on same sized array
  • Number of swaps
  • Memory usage
  • Simplicity reliability
Write a Comment
User Comments (0)
About PowerShow.com