one & two dimensional arrays CLASS ACTVITY SELF TEST
LOOP:
You can use it for one-dimensional array, for either row or
column.
Dim a(2) AS Integer
Use loop to initialize all
locations to zero (0)
– Declare 2 dimensional integer array but not initialized:
Dim a(,) As Integer
– Declare 3 dimensional integer array but not initialized:
Dim b(,,) As Integer
– In the following example you need to perform two steps
to create an array declare and Initialize.
– Declare and initialize two-dimensional integer array
Dim c(2,1) As Integer
c(0,0) = 33
c(0,1) = 71
c(1,0) = -16
c(1,1) = 45
c(2,0) = 99
c(2,1) = -27
In two-dimensional array, you can use the following
command to get the length of row and column.
For rows c.GetUpperBound(0)
For columns c.GetUpperBound(1)
NESTED LOOP (loop within a loop)
You can use it for two-dimensional array. In Visual Basic,
you can declare arrays with up to 32 dimensions.
The following statement declares a two-dimensional array
of 3 rows and 3 columns.
Dim a(2,2) As Integer
1st loop for row,
2nd loop for column
For row = 0 to 2
For col = 0 to 2
0 1 2
0
1
2
Q1: Initialize with "0" the two-dim array using nested loop
Q2: Fill the two-dimensional array using nested loop
0 1 2
0 0 1 1
1 1 0 1
2 1 1 0
Q3: Fill the two-dimensional array with the following
pattern using nested loop
0 1 2
0 1 1 0
1 1 0 1
2 0 1 1
Try yourself Homework ( ‫م‬‫س‬‫ق‬‫آ‬, NO COPY)
Q4: Write a code to count the total number of even
and odd elements in an array {8, 9, 22, 25, 26}
0
1
2
0 1 2
one & two dimensional arrays CLASS ACTVITY SELF TEST
Q5: write a program to add 2 one-dimensional
arrays.
{8,7,3,5} + {1,2,7,10} = {9,9,10,15}
Q6: write a program to find sum of each row of a
matrix (Hint: use nested loop)
10 15 20 45
15 20 30 = 65
20 10 40 70
Q7: write a program to print the sum of the
following matrix (Hint: use nested loop)
Q8: Write a program to copy all elements from an
array to another array.
Q9: Write a program to put even and odd elements
of array in two separate array.
Q10: Write a program to find sum of each row and
column of a matrix.
Q11: Write a program that asks the user to type 10
integers of an array. The program must compute
and write how many integers are greater than or
equal to 10.
Q12: Challenge: Write a program to multiply the
following 2 matrices (Hint: use 3 nested loop)
Input
Processing
Output
1 2
3 4
4 3
5 6
1x4 +2x5 1x3 +2x6
3x4 +4x5 3x3 +4x6
14 15
32 33
4 5 6
7 4 1
6 5 3
=+
x ==
1 1 1
2 2 2
3 3 3
5 6 7
9 6 3
9 8 6

two dimensional array

  • 1.
    one & twodimensional arrays CLASS ACTVITY SELF TEST LOOP: You can use it for one-dimensional array, for either row or column. Dim a(2) AS Integer Use loop to initialize all locations to zero (0) – Declare 2 dimensional integer array but not initialized: Dim a(,) As Integer – Declare 3 dimensional integer array but not initialized: Dim b(,,) As Integer – In the following example you need to perform two steps to create an array declare and Initialize. – Declare and initialize two-dimensional integer array Dim c(2,1) As Integer c(0,0) = 33 c(0,1) = 71 c(1,0) = -16 c(1,1) = 45 c(2,0) = 99 c(2,1) = -27 In two-dimensional array, you can use the following command to get the length of row and column. For rows c.GetUpperBound(0) For columns c.GetUpperBound(1) NESTED LOOP (loop within a loop) You can use it for two-dimensional array. In Visual Basic, you can declare arrays with up to 32 dimensions. The following statement declares a two-dimensional array of 3 rows and 3 columns. Dim a(2,2) As Integer 1st loop for row, 2nd loop for column For row = 0 to 2 For col = 0 to 2 0 1 2 0 1 2 Q1: Initialize with "0" the two-dim array using nested loop Q2: Fill the two-dimensional array using nested loop 0 1 2 0 0 1 1 1 1 0 1 2 1 1 0 Q3: Fill the two-dimensional array with the following pattern using nested loop 0 1 2 0 1 1 0 1 1 0 1 2 0 1 1 Try yourself Homework ( ‫م‬‫س‬‫ق‬‫آ‬, NO COPY) Q4: Write a code to count the total number of even and odd elements in an array {8, 9, 22, 25, 26} 0 1 2 0 1 2
  • 2.
    one & twodimensional arrays CLASS ACTVITY SELF TEST Q5: write a program to add 2 one-dimensional arrays. {8,7,3,5} + {1,2,7,10} = {9,9,10,15} Q6: write a program to find sum of each row of a matrix (Hint: use nested loop) 10 15 20 45 15 20 30 = 65 20 10 40 70 Q7: write a program to print the sum of the following matrix (Hint: use nested loop) Q8: Write a program to copy all elements from an array to another array. Q9: Write a program to put even and odd elements of array in two separate array. Q10: Write a program to find sum of each row and column of a matrix. Q11: Write a program that asks the user to type 10 integers of an array. The program must compute and write how many integers are greater than or equal to 10. Q12: Challenge: Write a program to multiply the following 2 matrices (Hint: use 3 nested loop) Input Processing Output 1 2 3 4 4 3 5 6 1x4 +2x5 1x3 +2x6 3x4 +4x5 3x3 +4x6 14 15 32 33 4 5 6 7 4 1 6 5 3 =+ x == 1 1 1 2 2 2 3 3 3 5 6 7 9 6 3 9 8 6