12 Arrays XXX

You might also like

Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 34

Arrays

1
Learning Objects
At the end of this lecture you will be able to :
1. declare and use one-dimensional arrays,
for example: A[1:n]
2. show understanding of the use of one-
dimensional arrays, including the use of a
variable as an index in an array
3. read or write values in an array using a
FOR … TO … NEXT loop

Slide 2 of 48
Definition of Arrays

• An array is a consecutive group of memory


locations that all have the same name and
the same type

• To refer to a particular location or element in


the array we specify the array name and the
index value

Slide 3 of 48
Declare array
Pseudocode:
DECLARE <identifier> : ARRAY[<lbound>:<ubound>] OF <datatype>

DECLARE student : ARRAY [1:5] OF STRING

VB code for VB 200X:

Dim student(5) As String

Index from 0 to 5,assign 6 locations.


Lower bound is 0
Upper bound is 5

For VB 200X , lower bound always is 0


Slide 4 of 48
How do arrays work?
Numbers(0) 56
Name of Array
(all the same) Numbers(1) 34
Numbers(2) 12
Numbers(3) 32
Dim numbers(5)
As Integer Numbers(4) 45
Numbers(5) 23

Only the index number makes


the difference
Declaring Arrays

There are two ways to declare an array:


 Default
upperBound=10
Length=11
Dim num(10) As Integer lowerBound always
is 0 in visual basic.
Num(0)
Pseudocode:
Num(1) Num[0]
… Num[2]
Num[3]
Num(10)
Slide 6 of 48
Using Arrays--Pseudo

DECLARE surname:ARRAY [1:5] OF STRING


DECLARE i : INTEGER

FOR i  1 TO 5
OUTPUT "Please input name" & i & ": "
INPUT surname[ i ]
NEXT

FOR i  1 TO 5
OUTPUT surname[ i ]
NEXT
Using Arrays—vb.net code

Sub Main()
Dim surname(5) As String
Dim i As Integer

For i = 1 To 5
Console.Write("Please input name" & i & ": ")
surname(i) = Console.ReadLine()
Next

For i = 1 To 5
Console.WriteLine(surname(i))
Next

End Sub
Group Exercise
Num(4) 1. Write a statement to declare the array
2
2 Write a statement to print 8 from the array

1 3 Write a statement to total all elements in the


array
3 4 Write a statement to double each element in the
array i.e multiply each element by 2
6 5 What is num(0) + num(4) ?

Slide 9 of 48
Answer for Group Exercise
Num(4) 1. Write a statement to declare the array
2
Pseudo: DECLARE Num: ARRAY[0:4] OF
INTEGER
1
Code: Dim Num(4) As Integer
3 2 Write a statement to to print 8 from the array
Pseudo: OUTPUT Num[4]
6
Code: Console.WriteLine(Num(4))
8

Slide 10 of 48
Answer for Group Exercise
3 Write a statement to total all elements in the
array
Pseudo:
2
Total0
1 FOR i0 TO 4
TotalTotal+Num[i]
3
NEXT
6 Code:
Total=0
8
For i=0 to 4
Total=total+Num(i)
Slide 11 of 48
Answer for Group Exercise
Num(4) 4 Write a statement to double each element in the
2 array i.e multiply each element by 2
Pseudo:
1
For i0 TO 4
3 Num[i]Num[i] *2
NEXT
6
5 What is num[0] + num[4] ?
8 2+8=10

Slide 12 of 48
Program code using “Default” Arrays
Pseudo:
DECLARE num:ARRAY[1:5] OF INTEGER
FOR i1 TO 5
num[i]0 Initializing an array
NEXT We should assign an
initial value to all the
array elements. This
usually takes the form
Pseudo:
of some kind of “Null”
DECLARE name:ARRAY[1:5] OF STRING
value. For a string data
FOR i1 TO 5 type ,it could be the
name[i] ”” “Empty String”
NEXT

Slide 13 of 48
Array is used as frequency table
when you do the die roll,if you draw 100
times,Please calculate the frequency for every
number .

Slide 14 of 48
Solution--pseudo
Solution--code
Dim frequency(6), index, dieNo As Integer
For index=1 to 6
frequency(index)=0
Next
For index = 1 To 100
dieNo = 1 + Int(Rnd() * 6)
frequency(dieNo) = frequency(dieNo) + 1
Next index

Console.WriteLine( “These are the results of the die roll:”)


For index = 1 To 6
Console.WriteLine( “No. “ & x & “ = “ & frequency(index))
Next index

Slide 16 of
Group Exercise

Build a program that constructs an array of 20


integers, in which the elements of the array are
initialized to the first 20 even numbers.
After the initialization process, multiply all the
elements of the array with 3.
Print the elements of the array before and after.

Slide 17 of 48
Solution--pseudo
Solution--code
Sub main()
Dim numbers(20) As Integer
Dim index as Integer
For index = 1 To 20
numbers(index) = 2*index
Console.Write(numbers(index) & “ “)
Next index
Console.Writeline
For index = 1 To 20
numbers(index) = numbers(index) * 3
Console.Write(numbers(index) & “ “)
Next index
End Sub
Slide 19 of 48
Group Exercise
• Write a loop so that you can input each of your five
best friends and it will output them in the reverse
order you input them. For example:
Code Output
Insert best friends:
1: Nell
2: Al
3: Sean
4: Paley
5: Jon
You listed: Jon,Paley, Sean,Al,Nell
Slide 20 of 48
solution
dim befr(5) as string
Dim index as integer
console.writeline("Insert best friends:")
for index = 1 to 5
console.write(index & ": ")
befr(index) = Console.ReadLine()
next
console.write ("You listed:")
for index = 5 to 1 step -1
console.write(befr(index) & ", ")
next

Slide 21 of 48
Example of String Arrays

Arrays can contain any data types, including strings.


Fill the blanks, Generate the fastestcar and print its
name randomly
DECLARE Cars:ARRAY[1:3] OF STRING

Cars[1]  “Proton Satria”


Cars[2]  “Nissan Sentra”
Cars[3]  “Honda Accord”
Solution --code

Dim Cars(3) As String


Dim fastestCar As Integer

Cars(1) = “Proton Satria”


Cars(2) = “Nissan Sentra”
Cars(3) = “Honda Accord”
Randomize()
fastestCar = 1+ Int(Rnd() * 3)
Console.WriteLine(“The fastest car is the “ & Cars(fastestCar))

Slide 23 of 48
Homework

The table below gives names and test scores from a math
contest. Write a program to display the names of the students
scoring above the average for these eight students.

Richard 135
Geraldine 114
James 92
John 91
Paul 150
Max 114
Robert 91
Barbara 124
Slide 24 of 48
Solution (Please code it)
DECLARE name:ARRAY [1:8] OF STRING
DECLARE score:ARRAY[1:8] OF INTEGER
Total0
FOR i 1 TO Ubound(name)
INPUT name[i]
INPUT score[i]
TotalTotal+score[i]
NEXT i
AverageTotal/Ubound(name)
FOR i 1 TO Ubound(name)
IF score[i]>average THEN
OUTPUT name[i], score[i]
ENDIF
NEXT i
Slide 25 of 48
DECLARE name:ARRAY [1:8], FirstName OF STRING
DECLARE score:ARRAY[1:8] OF INTEGER
DELCARE Total ,i ,Biggest:INTEGER
Total0
FOR i 1 TO Ubound(name)
INPUT name[i]
INPUT score[i]
TotalTotal+score[i]
NEXT i

Biggest-1
FOR i 1 TO Ubound(score)
IF Biggest < score[i] THEN
Biggest score[i]
FirstNamename[i]
ENDIF
NEXT
Slide 26 of 48
OUTPUT FirstName
Group Exercise
The table below gives names and test scores from a math contest.
1. Write a program to store the names and marks into two 1D arrays
2. Display the names of the students scoring above the average mark

Richard 135
Geraldine 114
James 92
John 91
Paul 150
Max 114
Robert 91
Barbara 124
Homework

• Write a program to store random numbers


from 1 to 10 into a one dimensional array
and determine the largest number

Slide 30 of 48
Homework

100 students were asked to rate the quality of a


particular lecturer in their class. They give a rating on
a scale of 1 to 10 (1 being terrible and 10 being
excellent). Place the 100 responses in an array of
integers and summarize the results of the poll.
Note: The responses are to be randomized.
Solution
Dim frequency(10), x, poll As Integer
Private Sub main()
For x = 1 To 100
poll = 1 + Int(Rnd() * 10)
frequency(poll) = frequency(poll) + 1
Next x
Console.WriteLine( "These are the results of the poll:“)
For x = 1 To 10
Console.WriteLine( "No. " & x & " = " & frequency(x))
Next x
End Sub
Slide 34 of 48

You might also like