Array in C/C++/java/Python
One Page Complete Notes
Download the image and open it in your Windows Photo Viewer and Zoom in to see the details.
😉
Don't worry you will find everything in the note so read it carefully.
All About Array
👾
If you want to know more in details download the above one page note where you can find everything
Declare an Array
-
- Declaring array then allocation memory for it and then initializing it.
int intArray[];
intArray = new int[20];
-
- Declaration and allocation memory for array in one line then initializing it.
int intArray[] = new int[5];
arr1[0] = 10;
arr1[1] = 20;
arr1[2] = 30;
arr1[3] = 40;
arr1[4] = 50;
-
- Declaration , instantiation and initialization in one line.
int intArray[] = new int[]{10, 20, 30, 40, 50};
-
- Declaration , instantiation and initialization in one line.
int intArray[] = {10, 20, 30, 40, 50};
-
- 2D Array Declaration and initialization in one line.
int intArray[][] = {{1,2,3},{4,5,6},{7,8,9}};
-
- 2D Array first declaration then allocation memory for it and then initialization.
int intArray[][];
intArray = new int[3][3];
intArray[0][0] = 1;
intArray[0][1] = 2;
intArray[0][2] = 3;
intArray[1][0] = 4;
intArray[1][1] = 5;
intArray[1][2] = 6;
intArray[2][0] = 7;
intArray[2][1] = 8;
intArray[2][2] = 9;
-
- 2D Array first declaration then allocation memory for it and then initialization.
Access Elements of Arrays
-
- You can access elements with the help of the index at which you stored them
int[] arr = {1, 2, 3, 4, 5};
System.out.println(arr[0] + " " + arr[1] + " "+ arr[2] + " " + arr[3] + " " + arr[4]);
-
- You can access elements with the help of normal for loop
int[] arr = {1, 2, 3, 4, 5};
for (int i = 0; i < arr.length; i++) {
System.out.print(arr[i]+" ");
}
-
- You can access elements with the help of for-each loop
int[] arr = {1, 2, 3, 4, 5};
for (int i : arr) {
System.out.print(i + " ");
}
-
- You can access elements with the help of while loop
int[] arr = {1, 2, 3, 4, 5};
int i = 0;
while (i < arr.length) {
System.out.print(arr[i] + " ");
i++;
}
-
- You can access elements with the help of do-while loop
int[] arr = {1, 2, 3, 4, 5};
int j = 0;
do {
System.out.print(arr[j] + " ");
j++;
} while (j < arr.length);
-
- You can access elements with the help of Passing Arrays to Methods
class CodeXam {
public static void printArray (int[] arr) {
for (int i = 0; i < arr.length; i++) {
System.out.println(arr[i]);
}
}
public static void main(String args[]) {
int[] arr = {1, 2, 3, 4, 5};
printArray(arr);
}
}
-
- 2D Array Accessing Elements
int[][] arr = {{1,2,3},{4,5,6},{7,8,9}};
System.out.println(arr[0][0] + " " + arr[0][1] + " "+ arr[0][2] + " " + arr[1][0] + " " + arr[1][1] + " " + arr[1][2] + " " + arr[2][0] + " " + arr[2][1] + " " + arr[2][2]);
-
- 2D Array Accessing Elements with the help of for loop
int[][] arr = {{1,2,3},{4,5,6},{7,8,9}};
for (int i = 0; i < arr.length; i++) {
for (int j = 0; j < arr[i].length; j++) {
System.out.print(arr[i][j] + " ");
}
}
-
- 2D Array Accessing Elements with the help of for-each loop
int[][] arr = {{1,2,3},{4,5,6},{7,8,9}};
for (int[] i : arr) {
for (int j : i) {
System.out.print(j + " ");
}
}
-
- 2D Array Accessing Elements with the help of while loop
int[][] arr = {{1,2,3},{4,5,6},{7,8,9}};
int i = 0;
while (i < arr.length) {
int j = 0;
while (j < arr[i].length) {
System.out.print(arr[i][j] + " ");
j++;
}
i++;
}
-
- 2D Array Accessing Elements with the help of do-while loop
int[][] arr = {{1,2,3},{4,5,6},{7,8,9}};
int i = 0;
do {
int j = 0;
do {
System.out.print(arr[i][j] + " ");
j++;
} while (j < arr[i].length);
i++;
} while (i < arr.length);
-
- 2D Array Accessing Elements with the help of Passing Arrays to Methods
class CodeXam {
public static void printArray (int[][] arr) {
for (int i = 0; i < arr.length; i++) {
for (int j = 0; j < arr[i].length; j++) {
System.out.print(arr[i][j] + " ");
}
}
}
public static void main(String args[]) {
int[][] arr = {{1,2,3},{4,5,6},{7,8,9}};
printArray(arr);
}
}
Traversal
CodeXam.cpp
#include <bits/stdc++.h>
using namespace std;
int main()
{
int n;
cout << "Enter no. of elements you want in array:";
cin >> n;
int a[n];
cout << "Enter all the elements:" << endl;
for (int i = 0; i < n; i++)
{
cin >> a[i];
}
cout << "Elements in array are:" << endl;
for (int i = 0; i < n; i++)
{
cout << a[i] << endl;
}
return 0;
}
output
Enter the number of elements in array: 5
Enter the element: 2
Enter the element: 2
Enter the element: 3
Enter the element: 3
Enter the element: 2
Elements in array are:
2
2
3
3
2
Insertion
CodeXam.cpp
#include <bits/stdc++.h>
using namespace std;
int main(){
int arr[] = {45,25,26,75};
int n = sizeof(arr)/sizeof(arr[0]);
cout << "Before Array" << endl;
for (int i = 0 ; i < n ; i++){
cout << arr[i] << " ";
}
cout << endl;
int pos = 2;
int element = 69;
int newArr[n+1];
for (int i = 0 ; i < pos ; i++){
newArr[i] = arr[i];
}
newArr[pos] = element;
for (int i = pos; i <n ; i++){
newArr[i+1] = arr[i];
}
cout << "After Array" << endl;
for (int i = 0 ; i < n+1 ; i++){
cout << newArr[i] << " ";
}
cout << endl;
return 0;
}
output
Before Array[45, 25, 26, 75]
After Array[45, 25, 69, 26, 75]
Deletion
CodeXam.cpp
#include <bits/stdc++.h>
using namespace std;
int main(){
int arr[] = {12, 212, 30, 44, 56};
int pos = 3;
int n = sizeof(arr)/sizeof(arr[0]);
cout << "Before deletion" << endl;
for(int i = 0; i < n; i++){
cout << arr[i] << " ";
}
cout << endl;
int newArr[n-1];
for(int i = 0; i < pos-1; i++){
newArr[i] = arr[i];
}
for(int i = pos; i < n; i++){
newArr[i-1] = arr[i];
}
cout << "After deletion" << endl;
for(int i = 0; i < n-1; i++){
cout << newArr[i] << " ";
}
cout << endl;
return 0;
}
output
Before deletion[12, 212, 30, 44, 56]
After deletion[12, 212, 44, 56]
Searching
CodeXam.cpp
#include <bits/stdc++.h>
using namespace std;
int main(){
int arr[] = {1,2,3,4,5};
int n = sizeof(arr)/sizeof(arr[0]);
cout<<"The array is: ";
for(int i = 0; i < n; i++){
cout<<arr[i]<<" ";
}
cout<<endl;
cout<<"Enter the element to be searched: ";
int element;
cin>>element;
int index = -1;
for(int i = 0; i < n; i++){
if(arr[i] == element){
index = i;
break;
}
}
if(index == -1){
cout<<"Element not found";
}
else{
cout<<"Element found at index "<<index;
}
return 0;
}
output
The array is: [1, 2, 3, 4, 5]
Enter the element to be searched: 4
Element found at index 3
Update
CodeXam.cpp
#include <bits/stdc++.h>
using namespace std;
int main(){
int arr[] = {1,2,3,4,5};
int index = 2;
int newValue = 6;
cout<<"Before update: ";
for(int i=0; i<5; i++){
cout<<arr[i]<<" ";
}
cout<<endl;
for(int i=0; i<5; i++){
if(i == index){
arr[i] = newValue;
break;
}
}
cout<<"After update: ";
for(int i=0; i<5; i++){
cout<<arr[i]<<" ";
}
cout<<endl;
return 0;
}
output
Before update: [1, 2, 3, 4, 5]
After update: [1, 2, 6, 4, 5]
Sort
CodeXam.cpp
#include <bits/stdc++.h>
using namespace std;
int main(){
int arr[] = { 21 , 52 , 83 , 48 , 15};
int n = sizeof(arr)/sizeof(arr[0]);
sort(arr, arr+n);
cout << "Ascending order sorting of array" << endl;
for (int i = 0; i < n; i++) {
cout << arr[i] << " ";
}
cout << endl;
cout << "Descending order sorting of array" << endl;
for (int i = n-1; i >= 0; i--) {
cout << arr[i] << " ";
}
return 0;
}
output
Ascending order sorting of array
15 21 48 52 83
Descending order sorting of array
83 52 48 21 15
Array - 2D
CodeXam.cpp
#include <bits/stdc++.h>
using namespace std;
int main(){
int row, column;
cout << "Enter the number of rows" << endl;
cin >> row;
cout << "Enter the number of column" << endl;
cin >> column;
cout << "Enter the elements" << endl;
int arr[row][column];
for (int i = 0 ; i < row ; i++){
for (int j = 0 ; j < column ; j++){
cin >> arr[i][j];
}
}
for (int i = 0 ; i < row ; i++){
for (int j = 0 ; j < column ; j++){
cout << arr[i][j] << " ";
}
cout << endl;
}
}
output
Enter the number of rows
2
Enter the number of column
3
Enter the elements
12
56
25
25
36
38
12 56 25
25 36 38
Basic Questions
Reverse an Array
Approach
Reverse an Array Using Swapping
Code
CodeXam.cpp
#include<iostream>
using namespace std;
int main() {
int n;
cout << "Enter the number of the elements you want to add in the array: ";
cin >> n;
int arr[n];
cout << "Enter the elements of the array: ";
for (int i = 0; i < n; i++) {
cin >> arr[i];
}
int l = sizeof(arr) / sizeof(arr[0]);
int k = l / 2;
int temp;
for (int i = 0; i < k; i++) {
temp = arr[i];
arr[i] = arr[l - i - 1];
arr[l - i - 1] = temp;
}
cout << "Reversed array is: ";
for (int i = 0; i < n; i++) {
cout << arr[i] << " ";
}
return 0;
}
output
Enter the number of the elements you want to add in the array:
5
Enter the elements of the array:
12
13
14
15
16
Reversed array is:
16 15 14 13 12
Maximum and Minimum
Approach
-
- Initialize max and min with the first element of the array.
-
- Traverse the array from the first index to the last index. 0->arr.length
-
- If the current element is greater than max, then update max.
-
- If the current element is smaller than min, then update min.
-
- Print the maximum and minimum element of the array.
Code
CodeXam.cpp
#include <iostream>
using namespace std;
int main()
{
int arr[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
int max = arr[0];
int min = arr[0];
for (int i = 0; i < 10; i++) {
if (arr[i] > max) {
max = arr[i];
}
if (arr[i] < min) {
min = arr[i];
}
}
cout << "Max element in the array is " << max << endl;
cout << "Min element in the array is " << min << endl;
return 0;
}
output
Max element in the array is 10
Min element in the array is 1
Check Sorted or Not
Approach
-
- Traverse the array from the first index to the last index. 0->arr.length
-
- If the current element is greater than the next element, then the array is not sorted.
-
- If the current element is smaller than the next element, then the array is sorted.
-
- Print the result.
Code
CodeXam.cpp
#include <iostream>
using namespace std;
int main()
{
int arr[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
for (int i = 0; i < sizeof(arr) / sizeof(arr[0]) - 1; i++)
{
if (arr[i] > arr[i + 1])
{
cout << "Not Sorted";
return 0;
}
}
cout << "Sorted";
return 0;
}
output
Sorted
Search Number in The Matrix
Approach
Take a matrix as input from the user and search for a given number in the matrix and print the position of the number if found.
-
- Take the number of rows and columns as input from the user.
-
- Take the elements of the matrix as input from the user.
-
- Take the number to be searched as input from the user.
-
- Traverse the matrix and check if the number is present in the matrix.
-
- If the number is present in the matrix, then print the position of the number.
-
- If the number is not present in the matrix, then print that the number is not present in the matrix.
Code
CodeXam.cpp
#include <iostream>
using namespace std;
int main()
{
int row, column, number, flag = 0;
cout << "Enter the number of rows and columns of the matrix" << endl;
cin >> row >> column;
int matrix[row][column];
cout << "Enter the elements of the matrix" << endl;
for (int i = 0; i < row; i++)
{
for (int j = 0; j < column; j++)
{
cin >> matrix[i][j];
}
}
cout << "Enter the number to be searched" << endl;
cin >> number;
for (int i = 0; i < row; i++)
{
for (int j = 0; j < column; j++)
{
if (matrix[i][j] == number)
{
cout << "The number is found at position " << i + 1 << "," << j + 1 << endl;
flag = 1;
}
}
}
if (flag == 0)
{
cout << "The number is not found" << endl;
}
return 0;
}
output
Enter the number of rows and columns of the matrix
2
2
Enter the elements of the matrix
12
13
14
25
Enter the number to be searched
14
The number is found at position 2,1
Any Matrix Addition
Approach
-
- Take the number of rows and columns of the matrix as input from the user.
-
- Take the elements of the first matrix as input from the user.
-
- Take the elements of the second matrix as input from the user.
-
- Add the elements of the first matrix and the second matrix and store the result in a third matrix.
-
- Print the third matrix.
Code
CodeXam.cpp
#include <iostream>
using namespace std;
int main()
{
int row, column, i, j;
cout << "Enter the number of rows" << endl;
cin >> row;
cout << "Enter the number of columns" << endl;
cin >> column;
int matrix1[row][column];
int matrix2[row][column];
int sum[row][column];
cout << "Enter the elements of matrix1" << endl;
for (i = 0; i < row; i++) {
for (j = 0; j < column; j++) {
cin >> matrix1[i][j];
}
}
cout << "Enter the elements of matrix2" << endl;
for (i = 0; i < row; i++) {
for (j = 0; j < column; j++) {
cin >> matrix2[i][j];
}
}
for (i = 0; i < row; i++) {
for (j = 0; j < column; j++) {
sum[i][j] = matrix1[i][j] + matrix2[i][j];
}
}
cout << "Sum of the matrices:" << endl;
for (i = 0; i < row; i++) {
for (j = 0; j < column; j++) {
cout << sum[i][j] << " ";
}
cout << endl;
}
return 0;
}
output
Enter the number of rows: 2
Enter the number of columns: 2
Enter the elements of matrix1
12
13
14
15
Enter the elements of matrix2
1
2
3
4
Sum of the matrices:
13 15
17 19