Sei sulla pagina 1di 4

QR/ACA 6.

0-03

Toc H Institute of Science & Technology


Arakkunnam - 682 313.

FORMAT FOR CLASS ROOM TEACHING (FCT)

(Based on Blooms Taxonomy)


Code: IT 405 Name of faculty: Time: 1 Hrs

Subject Title : Data Structures & Algorithms Dept : I.T. Topic General Objectives Module : I : :

Semester: IV Ajeesh G Krishnan

One dimensional Arrays Array Operations Data types, Working of looping statements.

Prerequisite Knowledge :

Specific Objectives : After this discourse, the students will have the following cognitive inputs

1. Recall that an array is a list structure that contains elements of the same type
2. Define Arrays.

3. Explain the terminology of array.


4. Summarize the methods for array declaration and initialization

5. Describe the memory allocation in one dimensional array with help of a diagram.
Key words introduced during the lecture:


Key points:

Arrays Array dimension (size) Array subscripts.

Arrays An array is a data structure consisting of a group of elements that are accessed by indexing. An array use set of consecutive memory locations used to store data Yaswant Kanitkar An array can be defined is a finite, ordered and collection of homogeneous data elements. Dennis Ritchie Each item in the array is called an element. Array is finite because it contains only limited number of elements and ordered

as all elements are stored one by one in contiguous locations of computer memory in a linear fashion. All the elements of an array are of the same data type only and hence it is termed The number of elements in an array is called the dimension of the array. as collection of homogeneous elements.

QR/ACA 6.0-03

If only one subscript is required to reference all the elements in an array then the

array will be termed as one dimensional array or simply an array. Terminology

Size: Number of elements in an array is called the size of the array. It is also known as length or dimension Type: Represents the kind of data type it is meant for. Base: It is the address of memory location where the first element in the array is located. Index: All the elements in the array can be referenced by a subscript like Ai or A[i], this subscript is known as index.

Array declaration and initialization Before it can be used, a variable must be both declared (to specify its type) and initialized (to give it an initial value). For a variable of an array type, there are three steps we must perform: declaration, allocation, and initialization. First, we must declare the variable. For example:
int[] nums;

The second step is to allocate space for the array. We have to tell Java how many elements the array will have so that it can set aside the appropriate amount of memory. The syntax for allocation uses the keyword new:
nums = new int[4];

The array now exists, but the elements themselves have not yet been initialized. They have default values in this case, they are all 0. We often use a for loop to initialize the elements:
for (int i = 0; i < nums.length; i++) { nums[i] = i * 2; }

Memory allocation for an array Let the memory location where the first element can be stored is M. if each element requires one word then the location for any element say A[i] in the array can be obtained as : Address (A[i]) = M + (i-1) In general an array can be written as A[L.U], where L and U denote the lower and upper bounds for index. If it is stored starting from memory location M, and for each element it requires w number of words, then the address for A[i] will be
Address (A[i]) = M + (i L) * w

The above formula is known as indexing formula.

QR/ACA 6.0-03

Summary:

An array is used to represent elements of same types like integers; characters etc. The effects of array declaration and initialization steps can summarize can.
UML instance diagram

and the elements are stored in the consecutive memory area.


Code
// Declaration int[] nums; // Allocation nums = new int[4]; // Initialization of elements for (int i = 0; i < nums.length; i++) { nums[i] = i * 2; }

Key Diagrams:

Memory allocation for an array

Analogies:

An analogy for single-dimension array is the pigeonhole system. In a hotel, for example, there are a number of pigeonhole slots where the staff can pin any number of items for guests to 2 3 4 5 6 7 8 9 10 collect at 1 their leisure. Lets say our hotel has only ten rooms:

I can put anything I like into those little pigeonholes: a Java primitive data type (an int or a double, for example) or an object (String, for example).
Stimulating Questions:

Is it valid to address one element beyond the end of an array?

QR/ACA 6.0-03

Do array subscripts always start with zero?

New Ideas or Concepts:

Potrebbero piacerti anche