Arrays
Presented By
Kanika Sharma
Topics to be covered…
• one dimensional array,
• Array initialization,
• 2 D array,
• initialization and matrices related concepts
Arrays
• A linear array is a list of a finite number of homogeneous data
elements such that:
a. All elements are indexed.
b. Elements are stored in successive memory locations.
• To obtain the length of data elements of the array we can subtract the
largest index by the smallest called as upper bound UB and lower
bound LB respectively and adding 1
• Length = UB - LB + 1
Address calculation
• If we know the base address of the array we can easily figure out the
address of any location of the index of the array by simply using this
method.
• Location arr[k] = BaseLocation(arr[0]) + w x(k - LowerBound)
• w = Number of words per memory cell
Arrays in C++
++
Array Pointer
• An array name is a constant pointer to the first element of the array.
for ( int i = 0; i < 5; i++ ) {
cout << "*(p + " << i << ") : ";
#include <iostream> cout << *(p + i) << endl;
using namespace std; }
cout << "Array values using array
int main () { as address " << endl;
int arr[5] = {1,2,3,4,5};
int *p; for ( int i = 0; i < 5; i++ ) {
cout << "*(arr+ " << i << ") : ";
p = arr; cout << *(arr + i) << endl;
cout << "Array values using pointer " << endl; }
return 0;
}
2- D arrays
Initialization of 2-D arrays
• Method 1 : int x[3][4] = {0, 1 ,2 ,3 ,4 , 5 , 6 , 7 , 8 , 9 , 10 , 11}
• Method 2: int x[3][4] = {{0,1,2,3}, {4,5,6,7}, {8,9,10,11}};
• Method 3:using nested for loop
• Method 4: Dynamic memory allocation
int** x = new int*[3];
for(int i = 0; i < 3; i++)
{ x[i] = new int[4];
for(int j = 0; j < 4; j++)
{ cin >> x[i][j];
}
}
Matrix operations
• Addition
• Subtraction
• Multiplication
• Sum of diagonal elements matrix
Predict the output
#include <iostream>
using namespace std;
int main()
{
int a[] = {0, 0X4, 4, 9};
int i = 2;
cout<< a[i] <<i[a];
return 0;
}
Solution
•44
Predict the output
#include <iostream>
using namespace std;
int main()
{
int a[] = {10, 20, 30, 40, 50};
int *p;
p = a;
cout<< *((int *)p + 3);
return 0;
}
Solution
• 40
Predict the output
#include <iostream>
using namespace std;
int main()
{
int array[] = {10, 20, 30};
cout <<-1[array];
return 0;
}
Solution
• -20
Debug the code
#include <iostream>
using namespace std;
int main()
int a = 5, b = 10, c = 15;
int arr[3] = {&a, &b, &c};
cout << *arr[*arr[1] - 8];
return 0;
}
Error
• error: invalid conversion from ‘int*’ to ‘int’
Predict the output
#include <iostream>
using namespace std;
int main()
{
int arr[] = {4, 5, 6, 7};
cout << *arr + 9;
return 0;
}
Answer
• 13
Finding the largest element from array
#include<bits/stdc++.h>
using namespace std;
int main(){
int arr[]={10, 89, 67, 56, 45, 78};
int n = sizeof(arr)/sizeof(arr[0]);
int max_element = INT_MIN;
for(int i=0; i<n; i++)
{
if(arr[i]>max_element)
max_element = arr[i];
}
cout<<max_element;
}
Hands On Exercise 1
• A chocolate factory is packing chocolates into the packets. The
chocolate packets here represent an array of N number of integer
values. The task is to find the empty packets(0) of chocolate and push
it to the end of the conveyor belt(array).
• Example 1 :
• N=7 and arr = [4,5,0,1.9,0,5,0].
• There are 3 empty packets in the given set. These 3 empty packets
represented as O should be pushed towards the end of the array
Exercise 2
• A supermarket maintains a pricing format for all its products. A value
N is printed on each product. When the scanner reads the value N on
the item, the product of all the digits in the value N is the price of the
item. The task here is to design the software such that given the code
of any item N the product (multiplication) of all the digits of value
should be computed(price).
References
• https://www.geeksforgeeks.org/top-50-array-coding-problems-for-
interviews/
Thanks.