2a. Arrays
2a. 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
– 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 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
• Example:
Note: The address of the
element varies, depending
Row major vs. Column major on the order of storing.
1 2 3 4 5
100
5 4 3 2 1
200
6 7 8 9 0
300
0 9 8 7 6
400
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.