what is an array static dynamic

18
Page 1 of 18 Question No. 1: Explain briefly about the arrays in programming. Explain what the difference is between Static Arrays & Dynamic Arrays. Answer: What is an Array? Arrays are a vital part of programming, as they allow the programmer to store more than one value in a variable, whilst retaining a single reference. If we think of a variable as a single slot in memory (or a box) that can contain data of a certain type - number, character, etc. - then an array is the equivalent of a box divided into partitions, each containing a piece of data. Of course, because the array box is storing more information than a single variable box, it is much bigger: it needs more memory. We can use the same name to access the variable, but we need some way to differentiate between the individual slots. To this we use an index into the array. For example, supposing we have an array that is 100 units wide, we might access the hundredth unit thus:

Upload: abid-ali

Post on 27-Oct-2014

29 views

Category:

Documents


0 download

DESCRIPTION

computer literacy program , university of karachi

TRANSCRIPT

Page 1: What is an Array Static Dynamic

Page 1 of 13

Question No. 1: Explain briefly about the arrays in programming. Explain what the

difference is between Static Arrays & Dynamic Arrays.

Answer:

What is an Array?

Arrays are a vital part of programming, as they allow the programmer to store more than one

value in a variable, whilst retaining a single reference. If we think of a variable as a single slot in

memory (or a box) that can contain data of a certain type - number, character, etc. - then an array

is the equivalent of a box divided into partitions, each containing a piece of data. Of course,

because the array box is storing more information than a single variable box, it is much bigger: it

needs more memory.

We can use the same name to access the variable, but we need some way to differentiate between

the individual slots. To this we use an index into the array. For example, supposing we have an

array that is 100 units wide, we might access the hundredth unit thus:

myArray[99] = 3;

This example also illustrates another feature of most arrays - the index is usually zero based. In

other words, the index to the first item is [0] and the index to the last item is [number of elements

- 1].

n many programming languages, a string is treated as an array of characters. Usually these arrays

are terminated with a null character to indicate the end of the string. This allows software to

process strings without knowing the dimension of the array at design time. A collection of strings

is still possible, however, by using a multidimensional array.

Page 2: What is an Array Static Dynamic

Page 2 of 13

Multi-Dimensional Arrays

A multi-dimensional array is an array of arrays. If we think of a one-dimensional array as a row

of slots in a box, then a two dimensional array is a grid of slots, and a three dimensional array is

a cube of slots. Beyond three dimensions it becomes difficult to conceptualize, but theoretically

at least, arrays can have any dimension.

If we wish to represent a simple grid (a chessboard for example), we could define the array as:

myArray[8][8]

The square at row 3, column 5 would be referenced thus:

myArray[3][5] = 1;

We note from this example that the convention for accessing arrays is often [row, column]. The

reason for this is that if we want to store an array of strings, it makes sense to access an

individual character in a single string as:

myStringArray[3][1]

This references the second character in the fourth string in the array.

Static and dynamic arraysStatic Arrays

Basically, we can create either static or dynamic arrays. Static arrays must include a fixed

number of items, and this number must be known at compile time so that the compiler can set

Page 3: What is an Array Static Dynamic

Page 3 of 13

aside the necessary amount of memory. We create a static array using a Dim statement with a

constant argument:

'This is a static array.

Dim Names(100) As String

Visual Basic starts indexing the array with 0. Therefore, the preceding array actually holds 101

items.

Most programs don't use static arrays because programmers rarely know at compile time how

many items we need and also because static arrays can't be resized during execution. Both these

issues are solved by dynamic arrays. We declare and create dynamic arrays in two distinct steps.

In general, we declare the array to account for its visibility (for example, at the beginning of a

module if we want to make it visible by all the procedures of the module) using a Dim command

with an empty pair of brackets. Then we create the array when we actually need it, using a

ReDim statement:

' An array defined in a BAS module (with Private scope)

Dim Customers() As String

...

Sub Main()

' Here we create the array.

ReDim Customer(1000) As String

End Sub

Page 4: What is an Array Static Dynamic

Page 4 of 13

If we're creating an array that's local to a procedure, we can do everything with a single ReDim

statement:

Sub PrintReport()

' This array is visible only to the procedure.

ReDim Customers(1000) As String

' ...

End Sub

Dynamic Arrays

Dynamic arrays can be re-created at will, each time with a different number of items. When we

re-create a dynamic array, its contents are reset to 0 (or to an empty string) and we lose the data

it contains. If we want to resize an array without losing its contents, use the ReDim Preserve

command:

ReDim Preserve Customers(2000) As String

When we're resizing an array, we can't change the number of its dimensions nor the type of the

values it contains. Moreover, when we're using ReDim Preserve on a multidimensional array, we

can resize only its last dimension:

ReDim Cells(1 To 100, 10) As Integer

...

ReDim Preserve Cells(1 To 100, 20) As Integer ' This works.

ReDim Preserve Cells(1 To 200, 20) As Integer ' This doesn't.

Page 5: What is an Array Static Dynamic

Page 5 of 13

Finally, we can destroy an array using the Erase statement. If the array is dynamic, Visual Basic

releases the memory allocated for its elements (and we can't read or write them any longer); if

the array is static, its elements are set to 0 or to empty strings.

We can use the LBound and UBound functions to retrieve the lower and upper indices. If the

array has two or more dimensions, we need to pass a second argument to these functions to

specify the dimension we need.

Page 6: What is an Array Static Dynamic

Page 6 of 13

Question No.2: What is the data base management system? Write the difference between

rotational database management system and simple database management system.

Answer:

Data Base Management System

By a database base management system we mean keeping track and record of things at a place.

Keeping record of all the books that are present in our library is an example of database

management.

In the world f computer & information technology database management system refers to a

software package with computer programs that control the creation, maintenance, and use of a

database. It allows organizations to conveniently develop databases for various applications by

database administrators (DBAs) and other specialists. A database is an integrated collection of

data records, files, and other objects. A DBMS allows different user application programs to

concurrently access the same database. DBMSs may use a variety of database models, such as

the relational model or object model, to conveniently describe and support applications. It

typically supports query languages, which are in fact high-level programming languages,

dedicated database languages that considerably simplify writing database application programs.

Database languages also simplify the database organization as well as retrieving and presenting

information from it. A DBMS provides facilities for controlling data access, enforcing data

integrity, managing concurrency control, and recovering the database after failures and restoring

it from backup files, as well as maintaining database security.

Page 7: What is an Array Static Dynamic

Page 7 of 13

Rotational Database Management System:

A relational database management system (RDBMS) is a database management system (DBMS)

that is based on the relational model as introduced by E. F. Codd. Most popular databases

currently in use are based on the relational database model.

A short definition of an RDBMS is: a DBMS in which data is stored in tables and the

relationships among the data are also stored in tables. The data can be accessed or reassembled in

many different ways without having to change the table forms.

Relational databases connect data in different files by using common data elements or a key

field. Data in relational databases is stored in different tables, each having a key field that

uniquely identifies each row. Relational databases are more flexible than either the hierarchical

or network database structures. In relational databases, tables or files filled with data are called

relations, tuples designates a row or record, and columns are referred to as attributes or fields.

Relational databases work on the principle that each table has a key field that uniquely identifies

each row, and that these key fields can be used to connect one table of data to another. Thus, one

table might have a row consisting of a customer account number as the key field along with

address and telephone number. The customer account number in this table could be linked to

another table of data that also includes customer account number (a key field), but in this case,

contains information about product returns, including an item number (another key field). This

key field can be linked to another table that contains item numbers and other product information

Page 8: What is an Array Static Dynamic

Page 8 of 13

such as production location, color, quality control person, and other data. Therefore, using this

database, customer information can be linked to specific product information.

The leading RDBMS products are Oracle, IBM's DB2 and Microsoft's SQL Server. Despite

repeated challenges by competing technologies, as well as the claim by some experts that no

current RDBMS has fully implemented relational principles, the majority of new corporate

databases are still being created and managed with an RDBMS.

Simple Database Management System: On the other hand simple database management

system refers to a Manual database management system. The technique of data management

used in our Mahmud Husain Library is an example. We keep track of all the books, maps,

gazettes (make data) that are present in the library.

Page 9: What is an Array Static Dynamic

Page 9 of 13

Question No.3: Write a short note on Variable types in Visual Basic. Give examples of

some important Data types.

Introduction

A variable is temporary storage space for numbers, text, and objects. Variables are constantly

being created and destroyed and will not hold any values after your program has ended. If you

want to save the values of variables or other data. Before allocating storage space for a variable,

decide what the variable's lifetime will be, or in other words, which procedures and which

modules should have access to the variable's value.

There are many types of data used in VB; some of them are discussed below:

Local Variables: A local variable is one that is declared inside a procedure. This variable is only

available to the code inside the procedure and can be declared using the Dim statements as given

below.

Dim sum As Integer

The local variables exist as long as the procedure in which they are declared, is executing. Once

a procedure is executed, the values of its local variables are lost and the memory used by these

variables is freed and can be reclaimed. Variables that are declared with keyword Dim exist only

as long as the procedure is being executed.

Static Variables: Static variables are not reinitialized each time Visual Invokes a procedure and

therefore retain or preserve value even when a procedure ends. In case we need to keep track of

Page 10: What is an Array Static Dynamic

Page 10 of 13

the number of times a command button in an application is clicked, a static counter variable has

to be declared. These static variables are also ideal for making controls alternately visible or

invisible. A static variable is declared as given below.

Static intPermanent As Integer

Variables have a lifetime in addition to scope. The values in a module-level and public variables

are preserved for the lifetime of an application whereas local variables declared with Dim exist

only while the procedure in which they are declared is still being executed. The value of a local

variable can be preserved using the Static keyword. The follwoing procedure calculates the

running total by adding new values to the previous values stored in the static variable value.

Function RunningTotal ( )

Static Accumulate

Accumulate = Accumulate + num

RunningTotal = Accumulate

End Function

If the variable Accumulate was declared with Dim instead of static, the previously accumulated

values would not be preserved accross calls to the procedure, and the procedure would return the

same value with which it was called. To make all variables in a procedure static, the Static

keyword is placed at the beginning of the procedure heading as given in the below statement.

Static Function RunningTotal ( )

Page 11: What is an Array Static Dynamic

Page 11 of 13

Example

The following is an example of an event procedure for a CommandButton that counts and

displays the number of clicks made.

Private Sub Command1_Click ( )

Static Counter As Integer

Counter = Counter + 1

Print Counter

End Sub

The first time we click the CommandButton, the Counter starts with its default value of zero.

Visual Basic then adds 1 to it and prints the result.

Module Levele Variables: A module level variable is available to all the procedures in the

module. They are declared using the Public or the Private keyword. If you declare a variable

using a Private or a Dim statement in the declaration section of a module—a standard BAS

module, a form module, a class module, and so on—you're creating a private module-level

variable. Such variables are visible only from within the module they belong to and can't be

accessed from the outside. In general, these variables are useful for sharing data among

procedures in the same module.

Page 12: What is an Array Static Dynamic

Page 12 of 13

Question No.4: construct an algorithm to calculate the roots of quadratic equation by

using if-then conditional statement.

Answer:

Set A, B, C

Set D=B2-4*A*C

If D=0 Then

Set X1= −b2a

, X2=−b2a

Write X1, X2

Else If

Set X1= −b−√D

2a

X2=

−b+√D2a

Write X1, X2

End If

If D=0 Then

Page 13: What is an Array Static Dynamic

Page 13 of 13

Write Set is equal

Else If D>0 Then

Write Real

Else If D<0 Then

Write Set is imaginary

End If