0% found this document useful (0 votes)
25 views18 pages

2-Dsa-Array Adt

Uploaded by

seharamjadnuml
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
25 views18 pages

2-Dsa-Array Adt

Uploaded by

seharamjadnuml
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 18

DATA STRUCTURES

1
Arrays ADT
Arrays

 An array is defined as
 Ordered collection of a fixed number of elements
 All elements are of the same data type

 Basic operations
 Direct access to each element in the array
 Values can be retrieved or stored in each element
Properties of an Array

 Ordered
 Every element has a well-defined position
 First element, second element, etc.
 Fixed size or capacity
 Total number of elements are fixed
 Homogeneous
 Elements must be of the same data type (and size)
 Use arrays only for homogeneous data sets
 Direct access
 Elements are accessed directly by their position
 Time to access each element is same
 Different to sequential access where an element is only accessed after the
preceding elements
Declaring Arrays in C/C++

datatype arrayName[intExp];
 datatype – Any data type, e.g., integer, character, etc.
 arrayName – Name of array using any valid identifier
 int Exp – Constant expression that evaluates to a positive integer
const i n t SIZE =10;
i nt list[SIZE];
 Compiler reserves a block of consecutive memory locations enough to
hold SIZE values of type i n t
Accessing Arrays in C/C++

arrayName[indexExp];

 indexExp – called index, is any expression that evaluates to a positive


integer
 In C/C++
 Array index starts at 0
 Elements of array are indexed 0, 1, 2, …, SI ZE- 1
 [ ] is called array subscripting operator
int value =list[2]
list[0]=value*2;
C/C++ Implementation of an Array ADT

As an ADT In C/C++
Ordered Index: 0 , 1 , 2 , … SIZE-1
Fixed Size intExp is constant
Homogeneous dataType is the type of all elements
Direct Access Array subscripting operator [ ]
Array Initialization in C/C++

dataType arrayName[intExp]={list ofvalues}

 In C/C++, arrays can be initialized at declaration


 intExp is optional: Not necessary to specify the size

 Example: Numeric arrays


 double s co re [ ] ={ 0 . 1 1 , 0 . 1 3 , 0 . 1 6 , 0 . 1 8 , 0.21}

0 1 2 3 4
scor
0.11 0.13 0.16 0.18
e
0.21
Array Initialization in C/C++
 Fewer values are specified than the declared size of an array
 Numeric arrays: Remaining elements are assigned zero
 Character arrays: Remaining elements contains null character ‘\0’ ASCII code of ‘\
0’ is zero
double score[5] = {0.11, 0.13, 0.16}
4
0 1 2 3
scor 0
0.11 0.13 0.16 0
e
char name[6]= {‘J’, ‘O’, ‘H’, ‘N’}

0 1 2 3 4
name J O H N 5
 If more values are specified than declared size\0
of an array
\0
 Error is occurred: Handling depends on compiler
Array Addressing

lis list[0 100


 Consider an array declaration: i n t l i s t [ 4 ] ={ 1 , 2 , 4 , 5 }t ] 0
100
 Compiler allocates a block of four memory spaces list[1
1
2 00
3
 Each memory space is large enough to store an i n t value ] 100
4
5
100
 Four memory spaces are contiguous 6
list[2 100
 Base address ] 7
100
 Address of the first byte (or word) in the contiguous block of memory 8
101
list[3 100
 Address of the memory location of the first array element 1901
] 2101
1 01
 Address of element list[0] 30
101
 4
Memory address associated with array_Name stores the base address 101
5
cout << list << endl;
cout << *list << endl;
 * is dereferencing operator Returns content of a memory location
Array Addressing
 Consider a statement: cout<<list[3];
 Requires array reference list[3]be translated into memory address
 Offset: Determines the address of a particular element w.r.t. base address
 Translation
 Base address + offset = 1000 + 3 x sizeof(int)=1012
 Content of address 1012 are retrieved & displayed
lis list[0 100
 An address translation is carried out each time t ] 0
100
an array element is accessed 1
2 00
list[1 3
 What will be printed and why? ] 100
4
5
cout<<*(list+3)<<endl; 100
list[2 6
100
] 7
100
8
101
list[3 100
1901
] 2101
1 01
30
101
4
101
5
Multidimensional Arrays

 Most languages support arrays with more than one dimension


 High dimensions capture characteristics/correlations associated with
data

 Example: A table of test scores for different students on several


tests
 2D array is suitable forTest 1 Test 2 Test 3 Test
storage and processing4of data
Student 99.0 93.5 89.0 91.0
1
Student 66.0 68.0 84.5 82.0
2
Student 88.5 78.5 70.0 65.0
3
: : : : :
: : : : :
Two Dimensional Arrays – Declaration

dataType arrayName[intExp1][intExp2];
 intExp1 – constant expression specifying number of rows
 intExp2 – constant expression specifying number of columns
 Example:
const int NUM_ROW =2, NUM_COLUMN = 4;
double scoreTable [NUM_ROW][NUM_COLUMN];
 Initialization:
Double scoreTable [ ] [ 4 ] = { {0.5, 0.6,0.3},
{0.6,0.3,0.8}};
 List the initial values in braces, row by row
 May use internal braces for each row to improve readability
Two Dimensional Arrays – Processing

arrayName[indexExp1][indexExp2];
 indexExp1 – row index
 indexExp2 – column index
 Rows and columns are numbered from 0
 Use nested loops to vary two indices
 Row-wise or column-wise manner
Test 1 Test 2 Test 3 Test
4
Student 99.0 93.5 89.0 91.0
1
Student 66.0 68.0 84.5 82.0
2
Student 88.5 78.5 70.0 65.0
3
: : : : :
Higher Dimensional Arrays
 Example: Store and process a table of test scores
 For several different students
 On several different tests
 Belonging to different semesters
const int SEMS =10, STUDENTS = 30,
TESTS = 4;
double ThreeDimArray[SEMS][STUDENTS][TESTS];
 What is represented by g ra d e b o o k [ 4 ] [ 2 ] [ 3 ] ?
 Score of 3rd student belonging to 5th semester on
4th test
Array of Arrays scor
[0] [1] [2] [3]
e [0]
 Consider the declaration [1]
 double score[10][4];
[2
]
[3]
 Another way of declaration

.
..
.
..

.
..

.
..
 One-dimensional (1D) array of rows
[9
]
double RowOfTable[4];
RowOfTable score[10];
score [2 [3
[0] [1]
 [0] ] ]
In detail
 Declare score as 1D array containing 10 [1]
elements [2]
 Each of 10 elements is 1D array of 4 [3
real numbers (i.e., double) ]

.
..

.
..

.
..

.
..
[9
]
Array of Arrays

 Score[i]
 Indicates ith row of the table

 Score[i][j]
 Can be thought of as ( s c o r e [ i ] ) [ j ]
 Indicates jth element of s c o r e [ i ]

An n-dimensional array can be viewed (recursively) as a 1D array whose elements


are (n-1)-dimensional arrays
Implementing Multidimensional Arrays

 More complicated than one dimensional arrays


 Memory is organized as a sequence of memory locations
 One-dimensional (1D) organization
 How to use a 1D organization to store multidimensional data?
 Example:
A B C D

E F G H
J
I
 A character requires single byte K L
 Compiler request to reserve 12 consecutive bytes
 Two way to store consecutively, i.e., row-wise and column-wise
Two-dimensional Arrays in Memory
 Two ways to be represented in memory
 Column majored
 Column by column
 Row majored
 Row by row
 Representation depends upon the programming language

(1,1) (1,1)
(2,1) Column 1 (1,2) Row 1
(3,1) (1,3)
(1,4)
(2,1)
(1,2) (2,2) Row 2
(2,2) Column 2 (2,3)
(3,2) (2,4)
(1,3) (3,1)
(2,3) Column 3 (3,2) Row 3
(3,3) (3,3)
(1,4) (3,4)
(2,4) Column 4

You might also like