Professional Documents
Culture Documents
12 Arrays XXX
12 Arrays XXX
12 Arrays XXX
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
Slide 3 of 48
Declare array
Pseudocode:
DECLARE <identifier> : ARRAY[<lbound>:<ubound>] OF <datatype>
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
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
Total0
1 FOR i0 TO 4
TotalTotal+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 i0 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 i1 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 i1 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
Slide 16 of
Group Exercise
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
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
Total0
FOR i 1 TO Ubound(name)
INPUT name[i]
INPUT score[i]
TotalTotal+score[i]
NEXT i
AverageTotal/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
Total0
FOR i 1 TO Ubound(name)
INPUT name[i]
INPUT score[i]
TotalTotal+score[i]
NEXT i
Biggest-1
FOR i 1 TO Ubound(score)
IF Biggest < score[i] THEN
Biggest score[i]
FirstNamename[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
Slide 30 of 48
Homework