0% found this document useful (0 votes)
28 views

2a. Arrays

Uploaded by

physicist sharma
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)
28 views

2a. Arrays

Uploaded by

physicist sharma
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/ 31

Arrays

Dr. K. Veningston
Department of Computer Science and Engineering
National Institute of Technology Srinagar
[email protected]
Introduction to Arrays
• An array is a collection of data items of the same type stored at
contiguous memory locations.
• Simple data structure -> supports random access
– contiguous, size of each element, starting address of an array
• NOTE:
– Random access is not possible with Linked List
Arrays in C/C++
• Sequential collection of elements of the same data type.
– As they are stored sequentially in memory, it gives the advantage to
access any element in constant time using the index.
– The elements are indexed from 0 to size – 1.
• For a type T, T[size] is the type, ‘array of size elements of type
T’
• Example:
int arr[2]; //an array of 2 integers --> arr[0], arr[1]
char* p[4]; //an array of 4 pointers to char i.e. array of 4 char pointers --> *p[0], *p[1], *p[2], *p[3]
Initialization of Arrays
• An array can be initialized by a list of values.
int arr[] = {4, 3, 2, 1};
char p[] = {‘x’, ‘y’, ‘z’};

• Correct/Incorrect?
– int arr1[3] = {1, 2, 3, 4};
– int arr2[6] = {1, 2, 3, 4};
– int arr3[6] = arr2;
Initialization of Arrays
• An array can be initialized by a list of values.
int arr[] = {4, 3, 2, 1};
char p[] = {‘x’, ‘y’, ‘z’};

• Correct/Incorrect?
– int arr1[3] = {1, 2, 3, 4}; ❌
• It cannot be done. We are trying to initialize an array of size 3 with 4 elements.
– int arr2[6] = {1, 2, 3, 4}; ✓
• Perfectly valid. The remaining 2 elements will be taken as 0.
– int arr3[6] = arr2; ❌
• In C/C++/Java, it is not allowed. We cannot expect all the elements to be copied from
arr2 to arr3. We have to write a loop to do it. We cannot do it directly using
assignments.
Pointers to Arrays
• The name of an array can be used as a pointer to its initial
elements.
int v[] = {1, 2, 3, 4};
int* p1 = v; // Pointer to initial element
int* p2 = &v[0]; // Pointer to initial element
int* p3 = v + 4;

p1 p2 p3

v 1 2 3 4
0 1 3 4 5
1-D Arrays
• How to reach 2nd element? How many elements we should cross?
A
0 1 2 3 4 5
– Address of A[2] is 2, if base of the array is 0.
– Location of element i i.e. A[i] = i
• If array is starting at the location address 100, then what will be the index?
– A[i] ==> i + base ==> i + 100 0 1 2 3 4 5 6 7 8 9
A
– A[5] ==> 5 + 100 = 105 100 101 102 103 104 105 106 107 108 109 110 111

• If index is starting from 1, not 0


– A[i] = ((i - 1) + 100) 1 2 3 4 5 6 7 8 9 10
A
– A[5] = ((5- 1) + 100) = 104 100 101 102 103 104 105 106 107 108 109 110 111
1-D Arrays
• If every element is of 2 bytes, how many elements we should
cross? A
0 1 2 3 4 5
– A[0] present starting at 0, A[1] present starting at 2, …
– Location of element i i.e. A[i] = (i * 2)
• If array is starting at the location address 100, then what will be the
index?
elements 0 1 2 3 4 5 6 7 8 9 10
A
bytes 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121

– A[i] = (i * 2) + 100
– A[2] = (2 * 2) + 100 = 104
Size of each element
1-D Arrays
• If index is starting from 1, not 0
– A[i] = ((i - 1) * 2) + 100
– A[2] = ((2 - 1) * 2) + 100 = 102

elements 1 2 3 4 5 6 7 8 9 10 11
A
bytes 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121
Memory view of 1-D array
Address Content
1000
i[0]
1001
int i[2]; i 1002
i[2]
double d[2]; 1003
1004
1005
1006
• Assume 1007
d 1008
– size of int is 2 bytes 1009
d[0]
– size of double is 8 bytes 1010
1011
1012
1013
1014
1015
1016
1017 d[1]
1018
1019
1020
1021
1022
2-D Arrays
• How to work with 2-D arrays?
– Conceptual look vs. the way they are physically stored in memory
• 2 ways we can generally look at arrays
– Indices starting from 0 0 1 2 3

0 [0,0] [0,1] [0,2] [0,3]


• Ex: A:(3x4)
1 [1,0] [1,1] [1,2] [1,3]

2 [2,0] [2,1] [2,2] [2,3]

– Indices starting from 1 1 2 3 4

1 [1,1] [1,2] [1,3] [1,4]


• Ex: A:(3x4)
2 [2,1] [2,2] [2,3] [2,4]

3 [3,1] [3,2] [3,3] [3,4]


2-D Arrays
• How 2-D arrays are stored?
– Random access is possible only if the array is stored in contiguous
locations.
– How to put it in a contiguous manner?
• we have to convert this 2-D array into 1-D array.
• How can we convert it?
– Row Major Order
• Stores the elements row by row
– Column Major Order
• Stores the elements column by column
2-D Arrays
• Row Major Order 0 1 2 3

– Ex: A:(3x4) 0 [0,0] [0,1] [0,2] [0,3]


1 [1,0] [1,1] [1,2] [1,3]
2 [2,0] [2,1] [2,2] [2,3]
0 1 2 3 4 5 6 7 8 9 10 11
… … [0,0] [0,1] [0,2] [0,3] [1,0] [1,1] [1,2] [1,3] [2,0] [2,1] [2,2] [2,3] … … … …
… …. 100 101 102 … … … … … … … … … … … …
?

– How to reach A[2][1]? What is the address of A[2][1]?


• Convert array reference to memory reference.
– How to convert the array reference in 2-D array into memory
reference?
2-D Arrays 0 1 2 3

0 [0,0] [0,1] [0,2] [0,3]


1 [1,0] [1,1] [1,2] [1,3]
• Row Major Order 2 [2,0] [2,1] [2,2] [2,3]
– To find the location of A[2][1]
0 1 2 3 4 5 6 7 8 9 10 11
A [0,0] [0,1] [0,2] [0,3] [1,0] [1,1] [1,2] [1,3] [2,0] [2,1] [2,2] [2,3] … …
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123

• cross 2 rows (or) 8 elements provided that #columns is 4 --> (2*4)


• cross 1 element in the row in which A[2][1] is present --> ((2*4) + 1) = 9
– Assume,
• Array index is starting from 0
• Each element is of 2 bytes
• Array is loaded at the starting address 100
2-D Arrays
• Row Major Order
– Address of A[2][1] ==> ((2*4) + 1) * Size of each element i.e. 2 bytes + starting address 100
==> = 9 * 2 + 100 = 118
– To find address of A[i][j], given size of array A is m*n
• The number of rows to cross is --> i
• The number of elements to cross is --> i * #elements in each row
• To reach the required row --> (i * n)
• To reach the required element, how many to cross? --> ((i * n) + j)
• If Size of each element “size“ --> ((i * n) + j) * size
• If array A is loaded at “base“ --> ((i * n) + j) * size + base
– If array index is starting from 1
• (((i-1) * n) + (j-1)) * size + base
A:(3x4)
2-D Arrays 0 1 2 3

0 [1,1] [1,2] [1,3] [1,4]


1 [2,1] [2,2] [2,3] [2,4]
• Row Major Order 2 [3,1] [3,2] [3,3] [3,4]

– If array index is starting from 1


• A[i][j] ==> A[3][2] ==> (((3-1) * 4) + (2-1)) * 2 + 100
==> 9 * 2 + 100 = 118

0 1 2 3 4 5 6 7 8 9 10 11
A [1,1] [1,2] [1,3] [1,4] [2,1] [2,2] [2,3] [2,4] [3,1] [3,2] [3,3] [3,4] … …
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123
2-D Arrays
• Column Major Order 0 1 2 3

0 [0,0] [0,1] [0,2] [0,3]


– Ex: A:(3x4) 1 [1,0] [1,1] [1,2] [1,3]
2 [2,0] [2,1] [2,2] [2,3]
0 1 2 3 4 5 6 7 8 9 10 11
… … [0,0] [1,0] [2,0] [0,1] [1,1] [2,1] [0,2] [1,2] [2,2] [0,3] [1,3] [2,3] … … … …
100 101 102 103 104 105 106 107 108 109 110 111
– Assume,
• Array index is starting from 0
• Each element is of 2 Bytes
• Array is loaded at the starting address 100
0 1 2 3 4 5 6 7 8 9 10 11
A [0,0] [1,0] [2,0] [0,1] [1,1] [2,1] [0,2] [1,2] [2,2] [0,3] [1,3] [2,3]
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123
2-D Arrays 0
0 1 2

[0,0] [0,1] [0,2] [0,3]


3

1 [1,0] [1,1] [1,2] [1,3]

• Column Major Order


2 [2,0] [2,1] [2,2] [2,3]

– To find the location of A[2][1]


0 1 2 3 4 5 6 7 8 9 10 11
A [0,0] [1,0] [2,0] [0,1] [1,1] [2,1] [0,2] [1,2] [2,2] [0,3] [1,3] [2,3]
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123

• cross 1 column (or) 3 elements provided that #rows is 3 --> (1*3)


• cross 2 elements in the column in which A[2][1] is present --> ((1*3) + 2) = 5
• If size of each element is 2 bytes --> ((1 * 3) + 2) * 2
• If array A is loaded at the base address 100 --> ((1 * 3) + 2) * 2 +100
--> 110
2-D Arrays
• Column Major Order
– To find the location of A[i][j], given size of array A is m*n
• To reach the required column --> (j * m)
• Total number of elements we need to cross --> (j * m) + i
• If the size of each element is “size" --> ((j * m) + i ) * size
• If Array A is loaded at the address “base“ --> ((j * m) + i ) * size + base
• If array index is starting from 1
–?
• NOTE:
– Coming to C programming, base == Name of the Array
Multidimensional Arrays
• Arrays of arrays are the multidimensional arrays
int marr[3][5]; // 3 arrays of 5 elements each

• Example:
Note: The address of the
element varies, depending
Row major vs. Column major on the order of storing.

• There are 2 methods of storing multidimensional arrays


– Row major order: The elements are arranged consecutively along the
row.
Address 0 1 2 3 4 5 6 7 8
Element a11 a12 a13 a21 a22 a23 a31 a32 a33

– Column major order: The elements are arranged consecutively along


the column.
Address 0 1 2 3 4 5 6 7 8
Element a11 a21 a31 a12 a22 a32 a13 a23 a33
Passing Arrays
• Arrays cannot be directly passed by value.
– Instead, it is passed as a pointer to its first element.
– 3 ways (argument as a pointer):
• void func(int* param) {…..}
• void func(int param[50]) {…..}
• void func(int* param[]) {…..}
C++ Code - Example
2-D Array using Dynamic Memory Allocation

1 2 3 4 5
100
5 4 3 2 1
200
6 7 8 9 0
300
0 9 8 7 6
400

100 200 300 400


1000 1001 1002 1003

1000

ip
To Find
• *(*(ip+0)+0) --> ? 1 2 3 4 5
100
• **ip --> ? 5 4 3 2 1
200

• **(ip+1)+1 --> ? 6
300
7 8 9 0

• **(ip+1+1) --> ? 0
400
9 8 7 6

• **ip+1 --> ?
• **ip+1+1 --> ? 100 200 300 400
1000 1001 1002 1003

1000

ip
To Find
• *(*(ip+0)+0) --> 1 1 2 3 4 5
100
• **ip --> 1 5 4 3 2 1
200

• **(ip+1)+1 --> 6 6
300
7 8 9 0

• **(ip+1+1) --> 6 0
400
9 8 7 6

• **ip+1 --> 2
• **ip+1+1 --> 3 100 200 300 400
1000 1001 1002 1003

1000

ip
Arrays in C++ - Practice problem 1
• Find the majority element in the array.
– A majority element in an array A[] of size n is an element that
appears more than n/2 times.
• Input : A[]={3, 3, 4, 2, 4, 4, 2, 4, 4}
• Output : 4
– Explanation: The frequency of 4 is 5 which is greater than the half of the size of the array
size.
• Input : A[] = {3, 3, 4, 2, 4, 4, 2, 4}
• Output : No Majority Element
– Explanation: There is no element whose frequency is greater than the half of the size of
the array size.
Arrays in C++ - Practice problem 1 – Code
Arrays in C++ - Practice problem 2
• Given an array of positive integers. All numbers occur an even
number of times except one number which occurs an odd
number of times.
– Input : arr = {1, 2, 3, 2, 3, 1, 3}
Output : 3
– Input : arr = {5, 7, 2, 7, 5, 2, 5}
Output : 5
Arrays in C++ - Practice problem 2 – Code
Arrays in C++ - Practice problem 3 – Code
• Write a C++ code for finding the 2nd largest element in the
array.

You might also like