Applied Programming Fall 2020: 3. Arrays ADT and C++ Implementation
Applied Programming Fall 2020: 3. Arrays ADT and C++ Implementation
Fall 2020
3-Arrays ADT 1
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
3-Arrays ADT 2
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
3-Arrays ADT 3
Recap: Declaring Arrays in C/C++
dataType arrayName[intExp];
3-Arrays ADT 4
Recap: 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, …, SIZE-1
– [ ] is called array subscripting operator list[0] 7
list[1]
• Example list[2] 5
– int value = list[2]; list[3]
– list[0] = value + 2;
...
list[9]
3-Arrays ADT 5
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 [ ]
3-Arrays ADT 6
Array Initialization in C/C++ (1)
3-Arrays ADT 7
Array Initialization in C/C++ (2)
3-Arrays ADT 8
Array Addressing (1)
• Consider an array declaration: int list [4] = { 1, 2, 4, 5}
– Compiler allocates a block of four memory spaces
– Each memory space is large enough to store an int value
– Four memory spaces are contiguous
• Base address
– Address of the first byte (or word) in the contiguous block of memory
– Address of the memory location of the first array element
⮚ Address of element list[0]
list list[0] 1000
• Memory address associated with 1001
1002
arrayName stores the base address list[1] 1003
1004
• Example 1005
1006
1007
– cout << list << endl; (Print list[2] 1008
1000) 1009
1010
1011
– cout << *list << endl; (Print 1) list[3] 1012
1013
• * is dereferencing operator 1014
1015
– Returns content of a memory location
3-Arrays ADT 9
Array Addressing (2)
• Translation
– Base address + offset = 1000 + 3 x sizeof(int) = 1012
– Content of address 1012 are retrieved & displayed
list[0] 1000
1001
• An address translation is carried out 1002
1003
each time an array element is accessed list[1] 1004
1005
1006
1007
list[2] 1008
• What will be printed and why? 1009
1010
1011
– cout << *(list+3) << endl; list[3] 1012
1013
1014
1015
3-Arrays ADT 10
Questions
3-Arrays ADT 11
Multidimensional Arrays
• Most languages support arrays with more than one dimension
– High dimensions capture characteristics/correlations associated with data
3-Arrays ADT 12
Two Dimensional Arrays – Declaration
dataType arrayName[intExp1][intExp2];
• 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
3-Arrays ADT 13
Two Dimensional Arrays – Processing
arrayName[indexExp1][indexExp2];
• indexExp1 – row index
• indexExp2 – column index
...
...
...
...
[9]
3-Arrays ADT 14
Higher Dimensional Arrays
3-Arrays ADT 15
Array of Arrays (1)
score [0] [1] [2] [3]
• Consider the declaration
[0]
– double score[10][4];
[1]
[2]
• Another way of declaration [3]
– One-dimensional (1D) array of rows
...
...
...
...
[9]
typedef double RowOfTable[4];
RowOfTable score[10];
score [0] [1] [2] [3]
• In detail [0]
– Declare score as 1D array containing [1]
10 elements [2]
– Each of 10 elements is 1D array of 4 [3]
real numbers (i.e., double)
...
...
...
...
[9]
3-Arrays ADT 16
Array of Arrays (2)
• Score[i]
– Indicates ith row of the table
• Score[i][j]
– Can be thought of as (score[i])[j]
– Indicates jth element of score[i]
Generalization:
An n-dimensional array can be viewed (recursively) as a 1D array whose elements
are (n-1)-dimensional arrays
3-Arrays ADT 17
Array of Arrays – Address Translation
• Address of score[5][3]
– Address of score[5] + 3 x sizeof(double) = 0x124E8 + 3 x 8
= 0x12400
• Example:
A B C D
E F G H
I J K L
– A character requires single byte
– Compiler request to reserve 12 consecutive bytes
– Two way to store consecutively, i.e., row-wise and column-wise
3-Arrays ADT 19
Two-dimensional Arrays in Memory
3-Arrays ADT 20
Any Question So Far?
2-ADTs 21
I’ve few questions ☺
Write down a simple program that stores number of pages, and number of authors for each book
available in the library. Initialize the array properly. Library has total of 20000 books available.
1. Write down a function that finds the book with largest number of pages in the library
2. Write down a function that finds the book with smallest number of pages in the library
3. Update the first function, and output the number of authors as well ( Note that authors will be a
separate array, but one to one correspondence is assumed)
4. Write down a function that outputs average number of pages and average number of authors
• Guidelines: Name the functions properly, and add comments where required. At the beginning of the
file, you are required to add your information i.e., Name and Roll#
• Cheating will lead to 0 marks
3-Arrays ADT 22