0% found this document useful (0 votes)
8 views63 pages

Array Lyst6498

The document provides an overview of arrays, including their definition, declaration, initialization, and types of indexing. It also covers one-dimensional and two-dimensional arrays, their memory storage methods (row-major and column-major), and includes various questions related to array operations and memory addressing. Additionally, it discusses the complexities of operations on arrays and the use of arrays in different data structures.

Uploaded by

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

Array Lyst6498

The document provides an overview of arrays, including their definition, declaration, initialization, and types of indexing. It also covers one-dimensional and two-dimensional arrays, their memory storage methods (row-major and column-major), and includes various questions related to array operations and memory addressing. Additionally, it discusses the complexities of operations on arrays and the use of arrays in different data structures.

Uploaded by

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

Array

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).

• Each element can be uniquely identified by their index in the array.


Q. Which of the following is the disadvantage of the array? [Asked in AMCAT
2020 ]

a. Stack and Queue data structures can be implemented through an array.


b. Index of the first element in an array can be negative
c. Wastage of memory if the elements inserted in an array are lesser than the
allocated size
d. Elements can be accessed sequentially.
How to declare an array in C

• datatype array Name[array Size];

• int data[100];
How to initialize an array

• It is possible to initialize an array during declaration. For example,


• int mark[5] = {19, 10, 8, 17, 9};

• You can also initialize an array like this.


• int mark[] = {19, 10, 8, 17, 9};

• 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};

• make the value of the third element to -1


• mark[2] = -1;

• make the value of the fifth element to 0


• mark[4] = 0;
Break
Size of an array
• Number of elements = (Upper bound – Lower Bound) + 1

• Lower bound index of the first element of the array

• Upper bound index of the last element of the array


• Types of indexing in array:
• 0 (zero-based indexing): The first element of the array is indexed by subscript of 0

• 1 (one-based indexing): The first element of the array is indexed by subscript of 1

• 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]

(A) An array of 50 numbers

(B) An array of 100 numbers

(C) An array of 500 numbers

(D) A dynamically allocated array of 550 numbers


Break
Two-Dimensional array
• The two-dimensional array can be defined as an array of arrays. The 2D array is organized as
matrices which can be represented as the collection of rows and columns.

• 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

Address of a[i][j] = B + W*[ (U2-L2+1) (i-L1) + (j-L2)]


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
(U2-L2+1) = numbers of columns
(i-L1) = number of rows before us
(j-L2) = number of elements before us in current row
Q. Consider a two dimensional array A[20][10]. Assume 4 words per memory cell,
the base address of array A is 100, elements are stored in row-major order and
first element is A[0][0]. What is the address of A[11][5] ? [Asked in Cognizant
2021]

a. 560
b. 460
c. 570
d. 575
Q Let A be a two dimensional array declared as follows:

A: array [1 ... 10] [1 ... 15] of integer;

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

(C) 10i+ j+ 89 (D) 10j+ i+ 89


Break
Column Major implementation of 2D array
• In Column major method elements of an array are arranged sequentially column by column.
Thus, elements of first column occupies first set of memory locations reserved for the array,
elements of second column occupies the next set of memory and so on.
Column 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
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

(B) best if both are in row-major order

(C) best if both are in column-major order

(D) independent of the storage scheme


Q An n x n array v is defined as follows:
v[i, j] = i-j for all i, j, 1 <= i <= n, 1 <= j <= n
The sum of the elements of the array v is (Gate-2000) (1 Marks)
(A) 0 (B) n-1 (C) n2 - 3n + 2 (D) n2 (n+1)/2
Break
3-Dimensional array
Break
N-Dimensional array

A([L1]---[U1]), ([L2]---[U2]), ([L3]---[U3]), ([L4]---[U4])--------([LN]---[UN])

Location of A [I, j, k, ----, x] =


N-Dimensional array

A([L1]---[U1]), ([L2]---[U2]), ([L3]---[U3]), ([L4]---[U4])--------([LN]---[UN])

Location of A [I, j, k, ----, x] =


B + (i-L1) (U2-L2+1) (U3-L3+1) (U4-L4+1) ----(Un-Ln+1)
+ (j-L2)(U3-L3+1) (U4-L4+1) ----(Un-Ln+1)
+ (k-L3)(U4-L4+1) ----(Un-Ln+1)
+
+
+
+ ( x-Ln)
Break
Q Let A be a square matrix of size n x n. Consider the following program. What is the expected output? (GATE -
2014) (1 Marks) [Asked in Hexaware 2017]
C = 100
for i = 1 to n do
{
for j = 1 to n do
{
Temp = A[i][j] + C
A[i][j] = A[j][i]
A[j][i] = Temp - C
}
}
for i = 1 to n do
for j = 1 to n do
Output(A[i][j]);

(A) The matrix A itself


(B) Transpose of matrix A
(C) Adding 100 to the upper diagonal elements and subtracting 100 from diagonal elements of A
(D) None of the above
Q Suppose you are given an array s[1..n] and a procedure reverse (s, i, j) which reverses the order
of elements in a between positions i and j (both inclusive). What does the following sequence do,
(GATE - 2014) (1 Marks)
where 1 <= k < n:
reverse(s, 1, k) ;
reverse(s, k + 1, n);
reverse(s, 1, n);

(A) Rotates s left by k positions

(B) Leaves s unchanged

(C) Reverses all elements of s

(D) None of the above


Q Consider a n*n square matrix A, such that
A[i][j] = 0 if(i<j)
a) find the space required to store the array in memory
b) derive an address access formula to access this matrix (if stored in row major order)
Break
Q In a compact one dimensional array representation for lower triangular matrix
(all elements above diagonal are zero) of size n x n, non zero elements of each row
are stored one after another, (i.e. elements of lower triangle) of each row are
stored one after another, starting from first row, the index of (i, j)th element of
the lower triangular matrix in this new representation is (GATE - 1994) (2 Marks)
(A) i+j (B) i + j-1 (C) j-1 + i(i-1)/2 (D) i+j(j-1)/2
Q Consider a n*n square matrix A, such that
A[i][j] = 0 if(i<j)
a) derive an address access formula to access this matrix (if stored in column major order)
Q Consider a n*n square matrix A, such that
A[i][j] = A[j][i]
find the space required to store the array in memory
Q Consider a n*n square matrix A, such that
A[i][j] = 0 if |i-j| > 1
find the space required to store the array in memory
Q Consider a n*n square matrix A, such that
A[i][j] = A[i-1][j-1] if i>0 && j>0, 0<=i,j<=n-1
find the space required to store the array in memory
Q Consider a n*n square matrix A, such that

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. Binary Search tree


b. Caching
c. Spatial locality
d. Scheduling of Processes
Q. An algorithm performs (logN)1/2 find operations, N insert operations, (logN)1/2 delete
operations, and (logN)1/2 decrease-key operations on a set of data items with keys drawn
from a linearly ordered set. For a delete operation, a pointer is provided to the record
that must be deleted. For the decrease-key operation, a pointer is provided to the record
that has its key decreased. Which one of the following data structures is the most suited
for the algorithm to use, if the goal is to achieve the best total asymptotic complexity
considering all the operations? [Asked in Cognizant 2016 ]

a. Unsorted array
b. Min-heap
c. Sorted array
d. Sorted doubly linked list

You might also like