Download as pdf or txt
Download as pdf or txt
You are on page 1of 10

Downloaded from TVET lite

a
.z
COMPUTER PROGRAMMING L4

o
Topic 2: Advanced Data structures

.c
rs
pe
pa
et
tv

Prepared by: LM Mosina

Downloaded from TVET NCV Exam Papers


Downloaded from TVET lite

a
At the end of this module you will be

.z
able to:

o
 Identify and describe the concept of multi-dimensional

.c
data structures (2-D arrays, tables).
 Explain and illustrate the features of multi-dimensional

rs
data structures (2-D arrays, tables).
 Manipulate data in multi-dimensional data structures

pe
(inserting, deleting, modifying).
 Identify and describe the concept of lists of objects
(object arrays, collections, lists)
pa
 Explain and implement array of objects (2-D arrays,
Ready to dine IN???
tables)
et

 Manipulate data in objects collections and lists


(inserting, deleting, modifying)
tv

Downloaded from TVET NCV Exam Papers


Downloaded from TVET lite

a
1.

o .z
.c
Multidimensional data structures generally store data in a
tabular form. We will discuss the features of the following

rs
multidimensional data structures and demonstrate how to
use them in VB.Net:

 2D arrays

pe
 Tables
Components of 2D arrays
pa
2D arrays are similar to a
matrix and have rows and
et

columns. We have index


numbers for the rows and
tv

columns as seen in Figure 3.1.

Downloaded from TVET NCV Exam Papers


Downloaded from TVET lite

a
.z
2.Let’s break it down: 2D arrays explained

o
.c
1. 2D arrays consists of rows and columns which are stored under one name.
2. Data is stored in an array element. Each element has a row and column index value.

rs
3. The rows and column index values start from 0.
4. When declaring the array, the highest row and column index values are used.

pe
5. The array size indicates the total number of array elements: 2D array size = number of
row elements * number of column elements
6. The following statements describe the 2D array in Figure 3.1:
pa
The row index values range from 0–5 and the column index values range from 0–7.
The data “Letisha” is stored in array element row and column index values (3,1).
et

Array size = 6 rows * 8 columns = 48 elements. This means that the array can store 48
values
tv

Downloaded from TVET NCV Exam Papers


Downloaded from TVET lite

a
3.Implementing 2D arrays in VB.Net

.z
Programming arrays in VB.Net

o
.c
Let us take a look at a step-by-
step approach to programming Declaring a 2D array

rs
arrays in VB.Net: Declaring a 2D array is similar to declaring a 1D array covered
in PCP L3 except that we now have two index numbers
Step 1 Declare the array.

pe
representing the rows and columns
Step 2 Store data in the array.
Step 3 Perform calculations or
pa
processing with the data in the
array.
et

Step 4 Display the data stored in


the array
tv

Downloaded from TVET NCV Exam Papers


Downloaded from TVET lite

a
Let’s break it down: Declaring the array in

.z
Example 3.1 1.

o
.c
 The highest index numbers for the rows and
columns are used to declare a 2D array in VB.Net.
 The statement declares a 2D integer array named

rs
intNumbers( ) with 4 rows and 4 columns.
Declaring a 2D array is similar to declaring a 1D
array covered in PCP L3 except that we now have

pe
two index numbers representing the rows and
columns. We Syntax: Rules for declaring a 2D array
NameOfTheArray (HighestRowIndexNo, AS
DataType Declaring a 2D array of numbers with 4
rows and 4 columns Name of the array
pa
 The number of rows or columns in a 2D array will
be 1 greater than the highest index number. This is
because the array index numbers count from 0 to
the highest index number.
et

 The size of a 2D array is determined by multiplying


the number of rows by the number of columns. 5.
In Figure 3.3, the size of the array = 4 * 4 = 16
tv

elements.

Downloaded from TVET NCV Exam Papers


Downloaded from TVET lite

a
o .z
.c
rs
4.Manipulating data

pe
in 2D arrays
pa
et
tv

Downloaded from TVET NCV Exam Papers


Downloaded from TVET lite

a
5.Using loops to access data in 2D arrays

o .z
.c
In 1D arrays, we needed only one
loop to access each element of the

rs
array.
In 2D arrays, we have row index
and column index values for each

pe
array element. Therefore, we will
need two nested loops for
accessing each element in a 2D
pa
array.
The outside loop represents the
row index values and an inner loop
et

represents the column index


values.
tv

Downloaded from TVET NCV Exam Papers


Downloaded from TVET lite

a
Continues……..

o .z
.c
rs
pe
pa
et
tv

Downloaded from TVET NCV Exam Papers


Downloaded from TVET lite

a
The End

o .z
.c
“What you are

rs
buys what you
pe
pa do
programmers!!!
!!!”
et
tv

Downloaded from TVET NCV Exam Papers

You might also like