Sie sind auf Seite 1von 23

DEPARTMENT OF TECHNICAL EDUCATION

ANDHRA PRADESH
Name : K SREE RAMACHARYULU
Designation : LECTURER
Branch : Commercial & Comp. Practice
Institute : Govt. Polytechnic, SRIKAKULAM
Year / Semester :V
Subject : Visual Basic-I
Subject Code : CCP-503
Topic : Programming Fundamentals
Duration : 50 Minutes
Sub. Topic : Multi Dimensional arrays in VB
Teaching Aids : Animation Clips & Images, MS-Office
CCP503.28 to 29 1
Objectives

On completion of this period, you would be


able to know

 The concept of Multi dimensional Arrays

 Sample Programs on multi-Dimensional Arrays

CCP503.28 to 29 2
RECAP

 The concept of Single Dimensional Arrays

 Sample programs on single dimensional arrays

CCP503.28 to 29 3
MULTI DIMENSIONAL ARRAY

 When data has different groups, Multi-dimensional


arrays are used
 A Multi-dimensional array consists of elements
arranged in the form of rows and columns
 A row is used to store one group of data
 Columns in a row are used to store elements in each
group (row)
 If a multidimensional has only 1 set of rows each row
having two or more columns, it is called two
dimensional arrays.

CCP503.28 to 29 4
Two dimensional Array
 Suppose it becomes necessary to store ages of 10 students of
CCP Iyear and CCP II year students

 You can declare the two dimensional array to store the above
data as follows

 Dim Age (2,10) as integer


 Above array reserves storage space in memory as
follows 0 1 2 3 4 5 6 7 8 9

0th
Rows

CCP503.28 to 29 5
 In the above, the age of CCPI year are stored
in 0th row, from 0th column to 9th column and
CCPI year students data are stored in 1st row

CCP503.28 to 29 6
Two-dimensional ARRAY
 Elements in the two dimensional array can be accessed
with the row and column address
 Age(0,0) is the address at 1st column in 1st row
 Age (0,1) to Age (0,9) are remained elements in first row
 Age (1,0) is the address at 1st column in second row
 Age (1,1) to Age (1,9) are the address of remaining
column in 2nd row

CCP503.28 to 29 7
 Nested loops can be used to generate address of
elements of two dimensions for storing and retrieving
data from the arrays
 The following code can help in storing data into the two
dimensional array
Dim age (2,10) as integer
Dim I as Integer, j as integer
For I = 0 to 1
For j = 0 to 9
Age (i, j) = input box (“give age:”)
Next j
Next i
CCP503.28 to 29 8
Two dimensional Arrays
 The following code declare two dimension array and one
dimensional array to store names and ages of CCPI and
CCPII year students and shows the output on the screen
Dim Sname (2) as string
Dim age (2,10) as Integer
Dim I as Integer, J as Integer
For I = 0 to 1
sname (I) = Input box (“Give name:”)
For J = 0 to 9
Age (I,J) = Input box (“Give marks:”)
Next J
Next I
For I = 0 to 1
PRINT Sname (I);
For J = 0 to 9
PRINT Age (I,J);
Next J
Next I
PRINT
CCP503.28 to 29 9
(2) Dynamic Arrays
 Sometimes we may not know how large to make
an array

 Instead of making it large enough to hold the


maximum number of data, we can declare a
dynamic array

 The size of a dynamic array can vary during the


course of the program

 We can also say that, we might need an array


until the user has entered a bunch of data and the
application has processed it and displayed the
results

CCP503.28 to 29 10
 If the data in memory is no longer needed, we can discard
the data and return the resources it occupies

 We can create dynamic array as usually with a Dim


statement ,but don’t specify its dimensions :
Dim DynArray()
 Later in the program, we know how many elements we
want to store in the array, then use ReDim

 Use ReDim statement to redimension the array, this time


with its actual size, as follows
ReDim DynArray(UserCount)

CCP503.28 to 29 11
 Redim statement can appear only once in code

 Unlike Dim statement, ReDim is executable, it


forces the application to carry out an action at run
time

 A Dynamic array can also be redimensioned into


multidimensional array, as follows
 Dim Matrix() as double
 using Redim in a procedure to declare a 3-
dimensional array as :
 Redim Matrix (9,9,9)
 Note that ReDim can’t change the type of the
array

CCP503.28 to 29 12
 The subsequent ReDim statements can change the
bounds of the array of matrix, but not number of its
dimensions

Preserve Keyword:

 Each time we execute the ReDim statement ,we lose


all the values currently stored in the array

 Using preserve keyword forces to resize the array


without discarding the existing data
Example:
ReDim preserve
DynamicArray(UBound(DynArray)+1)

CCP503.28 to 29 13
Constants
 Some variables don’t change the value during the
execution of a program,Thes values are called
constants
 For instance, if our program does math calculations,
the value of pi(3.14159) may appear many times in
our code

 These values are best presented by constant,


instead type each time the 3.14159 over and over
again

CCP503.28 to 29 14
DECLARATION OF CONSTANTS

Public const pi as double = 3.14159265358979


Const const_name [ as type] = value

 Here ‘as type’ is optional

 Constants also have a scope and they can be public or


private as above

CCP503.28 to 29 15
ADVANTAGES OF CONSTANTS

Constants don’t change value:

 This is a safety feature, once a constant is


declared, we can’t change its value in
subsequent statements

CCP503.28 to 29 16
Constants are processed faster than
variables:

 When program is running, the values of


constants don’t have to be looked up

 The compiler simply substitutes constant names


with their values, and program executes faster

CCP503.28 to 29 17
Summary

 Types of Arrays

 Two Dimensional Arrays

 Constants

 Small Application Programs on to Dimensional

Array

CCP503.28 to 29 18
Frequently Asked Questions
 What is a Multi Dimensional Array? How will you
declare it VB?

 How to assign values to a Multi Dimensional Array


during design time of VB code window ?

 Explain briefly about the representation of Multi


Dimensional Arrays in VB?

CCP503.28 to 29 19
Quiz

1. Which of the following is correct statement


as per syntax of VB ?

A. Dim Mat{1 to 3, 1 To 3}
B. Dim Mat (1 to 3, 1to 3)
C. Dim Mat[3][3]
D. Dim Mat [3, 3]

CCP503.28 to 29 20
Which of the following Statement is
valid in VB dynamic arrays concept?

A. Array size cannot be changed


B. Array can be changed during execution
C. DIM x(10)- its value can be changed during run
time
D. Dim x(2,2)- its value can be changed during run
time

CCP503.28 to 29 21
3. Which of the following is correct for constant?

A. Does not change the value throughout the


program

C. Value is constant after execution the program

E. Sometimes changes and sometimes constant

G. Value is constant before execution the


program

CCP503.28 to 29 22
Assignment

1. Write a program using two dimensional array to


store six marks of 5 students and show the
marks with the total

2. Write a program to show the use of redim


statement

CCP503.28 to 29 23

Das könnte Ihnen auch gefallen