Array Lyst6498
Array Lyst6498
Array
• An array is a collection of items stored at contiguous memory locations.
• The idea is to store multiple items of the same type together. This makes it easier to calculate
the position of each element by simply adding an offset to a base value, i.e., the memory
location of the first element of the array (generally denoted by the name of the array).
• int data[100];
How to initialize an array
• Here, we haven't specified the size. However, the compiler knows its size is 5 as we are
initializing it with 5 elements.
• Change Value of Array elements
• int mark[5] = {19, 10, 8, 17, 9};
• n (n-based indexing): The base index of an array can be freely chosen. Usually
programming languages allowing n-based indexing also allow negative index values and
other scalar data types like enumerations, or characters may be used as an array index.
• Size = number of elements * Size of each elements in bytes
Q. Assuming int is of 4bytes, what is the size of int arr[15];? [Asked in
Capgemini 2016 ]
a. 15
b. 19
c. 11
d. 60
One Dimensional array
• Address of the element at kth index
• a[k] =
One Dimensional array
• Address of the element at kth index
• a[k] = B + W*k
• a[k] = B + W*(k – Lower bound)
• B is the base address of the array
• W is the size of each element
• K is the index of the element
• Lower bound index of the first element of the array
• Upper bound index of the last element of the array
Q Let the base address of the first element of the array is 250 and each
element of the array occupies 3 bytes in the memory, then address of
the fifth element of a one- dimensional array a[10] ?
Q An array has been declared as follows
A: array [-6--------6] of elements where every element takes 4 bytes, if
the base address of the array is 3500 find the address of array[0]?
Q A program P reads in 500 integers in the range [0...100] experimenting the
scores of 500 students. It then prints the frequency of each score above 50. What
would be the best way for P to store the frequencies? (GATE - 2005) (2 Marks)
[Asked in Cognizant 2016]
• However, 2D arrays are created to implement a relational database look a like data structure.
It provides ease of holding the bulk of data at once which can be passed to any number of
functions wherever required.
Two-Dimensional array
data_type array_name[rows][columns];
int disp[2][4] = {
{10, 11, 12, 13},
{14, 15, 16, 17}
};
OR
int disp[2][4] = { 10, 11, 12, 13, 14, 15, 16, 17};
Implementation of 2D array
• In computing, row-major order and column-major order are methods for
storing multidimensional arrays in linear storage such as random access memory.
• The difference between the orders lies in which elements of an array are contiguous in
memory.
Row Major implementation of 2D array
• In Row major method elements of an array are arranged sequentially row by row.
• Thus, elements of first row occupies first set of memory locations reserved for the array,
elements of second row occupies the next set of memory and so on.
Q. If row-major order is used, how is the following matrix stored in
memory? [Asked in Accenture 2021 ]
abc
def
ghi
a. ihgfedcba
b. abcdefghi
c. cfibehadg
d. adgbehcfi
Row Major implementation of 2D array
Address of a[i][j] =
B = Base address
W = Size of each element
L1 = Lower bound of rows
U1 = Upper bound of rows
L2 = Lower bound of columns
U2 = Upper bound of columns
Row Major implementation of 2D array
a. 560
b. 460
c. 570
d. 575
Q Let A be a two dimensional array declared as follows:
Assuming that each integer takes one memory location, the array is stored in row-
major order and the first element of the array is stored at location 100, what is the
address of the element a[i][j] ? (Gate-1998) (2 Marks)
(A) 15i+ j+ 84 (B) 15j+ i+ 84
Address of a[i][j] =
B = Base address
W = Size of each element
L1 = Lower bound of rows
U1 = Upper bound of rows
L2 = Lower bound of columns
U2 = Upper bound of columns
Column Major implementation of 2D array
Address of a[i][j] = B + W*[ (U1-L1+1) (j-L2) + (i-L1)]
B = Base address
W = Size of each element
L1 = Lower bound of rows
U1 = Upper bound of rows
L2 = Lower bound of columns
U2 = Upper bound of columns
(U1-L1+1) = numbers of rows
(j-L2) = number of columns before us
(i-L1) = number of elements before us in current column
Q An array VAL[1…15][1…10] is stored in the memory with each element requiring
4 bytes of storage. If the base address of the array VAL is 1500, determine the
location of VAL[12][9] when the array VAL is stored
(i) Row wise (ii) Column wise.
Q A[5.....15,-8.....8] is A[11][17] and we are supposed to find A[8][5] -
Row Major Order Base Address:800, each element occupies 4 memory
cells?
Q Two matrices M1 and M2 are to be stored in arrays A and B respectively. Each
array can be stored either in row-major or column-major order in contiguous
memory locations. The time complexity of an algorithm to compute M1 × M2 will
be (Gate-2004) (2 Marks)
(A) best if A is in row-major, and B is in column- major order
0 1 2 3
0 00 01 02 03
1 10 11 12 13
2 20 21 22 23
3 30 31 32 33
Break
Q. What is the worst case time complexity of inserting an element into
the sorted array? [Asked in CoCubes 2016 ]
a. O(nlogn)
b. O(logn)
c. O(n)
d. O(n2)
Q. The minimum number of comparisons required to determine if an integer
appears more than n/2 times in a sorted array of n integers is [Asked in TCS NQT
2018 ]
a. Θ(n)
b. Θ(logn)
c. Θ(log*n)
d. Θ(1)
Q. Which of the following highly uses the concept of an array? [Asked in
Accenture 2018]
a. Unsorted array
b. Min-heap
c. Sorted array
d. Sorted doubly linked list