Arrays and Matrix Operations in C
What is an Array?
An array is a collection of elements stored in contiguous memory locations, and all
elements must be of the same data type.
Declaration:
int arr[5];
float marks[10];
Initialization:
int arr[5] = {1, 2, 3, 4, 5};
Array Operations
Below are the common array operations in C:
1. Traversing an Array
To access and print each element of the array:
#include <stdio.h>
int main() {
int arr[] = {10, 20, 30, 40, 50};
int n = 5;
for(int i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
return 0;
}
2. Inserting an Element
#include <stdio.h>
int main() {
int arr[10] = {1, 2, 4, 5};
int n = 4, pos = 2, val = 3;
for(int i = n; i > pos; i--) {
arr[i] = arr[i-1];
}
arr[pos] = val;
n++;
for(int i = 0; i < n; i++)
printf("%d ", arr[i]);
return 0;
}
3. Deleting an Element
#include <stdio.h>
int main() {
int arr[] = {1, 2, 3, 4, 5};
int n = 5, pos = 2;
for(int i = pos; i < n - 1; i++) {
arr[i] = arr[i + 1];
}
n--;
for(int i = 0; i < n; i++)
printf("%d ", arr[i]);
return 0;
}
4. Searching an Element
#include <stdio.h>
int main() {
int arr[] = {10, 20, 30, 40, 50}, key = 30;
int n = 5, found = 0;
for(int i = 0; i < n; i++) {
if(arr[i] == key) {
printf("Element found at index %d\n", i);
found = 1;
break;
}
}
if(!found)
printf("Element not found.\n");
return 0;
}
What is a Matrix (2D Array)?
A matrix is a 2D array with rows and columns.
Declaration:
int mat[3][3];
Initialization:
int mat[2][3] = {
{1, 2, 3},
{4, 5, 6}
};
Matrix Operations
1. Traversing a Matrix
#include <stdio.h>
int main() {
int mat[2][3] = {{1, 2, 3}, {4, 5, 6}};
for(int i = 0; i < 2; i++) {
for(int j = 0; j < 3; j++) {
printf("%d ", mat[i][j]);
}
printf("\n");
}
return 0;
}
2. Addition of Two Matrices
#include <stdio.h>
int main() {
int a[2][2] = {{1, 2}, {3, 4}};
int b[2][2] = {{5, 6}, {7, 8}};
int sum[2][2];
for(int i = 0; i < 2; i++) {
for(int j = 0; j < 2; j++) {
sum[i][j] = a[i][j] + b[i][j];
}
}
printf("Sum:\n");
for(int i = 0; i < 2; i++) {
for(int j = 0; j < 2; j++)
printf("%d ", sum[i][j]);
printf("\n");
}
return 0;
}
3. Transpose of a Matrix
#include <stdio.h>
int main() {
int mat[2][3] = {{1, 2, 3}, {4, 5, 6}};
int trans[3][2];
for(int i = 0; i < 2; i++)
for(int j = 0; j < 3; j++)
trans[j][i] = mat[i][j];
printf("Transpose:\n");
for(int i = 0; i < 3; i++) {
for(int j = 0; j < 2; j++)
printf("%d ", trans[i][j]);
printf("\n");
}
return 0;
}
4. Multiplication of Two Matrices
#include <stdio.h>
int main() {
int a[2][2] = {{1, 2}, {3, 4}};
int b[2][2] = {{5, 6}, {7, 8}};
int mult[2][2] = {0};
for(int i = 0; i < 2; i++)
for(int j = 0; j < 2; j++)
for(int k = 0; k < 2; k++)
mult[i][j] += a[i][k] * b[k][j];
printf("Product:\n");
for(int i = 0; i < 2; i++) {
for(int j = 0; j < 2; j++)
printf("%d ", mult[i][j]);
printf("\n");
}
return 0;
}
5. Diagonal Sum of a Matrix
#include <stdio.h>
int main() {
int mat[3][3] = {
{1, 2, 3},
{4, 5, 6},
{7, 8, 9}
};
int mainDiag = 0, secDiag = 0;
for(int i = 0; i < 3; i++) {
mainDiag += mat[i][i];
secDiag += mat[i][2 - i];
}
printf("Main diagonal sum: %d\n", mainDiag);
printf("Secondary diagonal sum: %d\n", secDiag);
return 0;
}
Pratice sums:
1.Maximum Product Pair in an Array
Find the pair of elements in the array whose product is maximum.
Input: arr[] = {1, 20, -1, 0, -10, 2}
Output: Max Product Pair = (20, 2) → Product = 40
Code:
#include <stdio.h>
#include <limits.h>
int main() {
int arr[] = {-10, -3, 5, 6, -2};
int n = sizeof(arr) / sizeof(arr[0]);
int max1 = INT_MIN, max2 = INT_MIN;
int min1 = INT_MAX, min2 = INT_MAX;
for (int i = 0; i < n; i++) {
// For max values
if (arr[i] > max1) {
max2 = max1;
max1 = arr[i];
} else if (arr[i] > max2) {
max2 = arr[i];
}
// For min values
if (arr[i] < min1) {
min2 = min1;
min1 = arr[i];
} else if (arr[i] < min2) {
min2 = arr[i];
}
}
int prod1 = max1 * max2;
int prod2 = min1 * min2;
if (prod1 > prod2)
printf("Max product pair is (%d, %d) = %d\n", max1, max2, prod1);
else
printf("Max product pair is (%d, %d) = %d\n", min1, min2, prod2);
return 0;
}
2. Left Rotate an Array by k Positions
Write a program in C to left rotate an array of size n by k positions.
Input : arr[] = {1, 2, 3, 4, 5}, k = 2
Output : {3, 4, 5, 1, 2}
Code:
void reverse(int arr[], int start, int end) {
while (start < end) {
int temp = arr[start];
arr[start] = arr[end];
arr[end] = temp;
start++;
end--;
void leftRotate(int arr[], int n, int k) {
k = k % n; // Handle k > n
reverse(arr, 0, k - 1);
reverse(arr, k, n - 1);
reverse(arr, 0, n - 1);
}
3. Find the maximum product of consecutive pair
A matrix will be given. Find the maximum product of consecutive pair and print it's pair.
Input : [[1 4 5][7 8 2][4 3 11]]
Output : 56- [7 8]
code
#include <stdio.h>
int main() {
int mat[3][3] = {
{1, 4, 5},
{7, 8, 2},
{4, 3, 11}
};
int rows = 3, cols = 3;
int maxProduct = -100000;
int pair1 = 0, pair2 = 0;
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
if (j + 1 < cols) {
int rowProd = mat[i][j] * mat[i][j + 1];
if (rowProd > maxProduct) {
maxProduct = rowProd;
pair1 = mat[i][j];
pair2 = mat[i][j + 1];
}
}
if (i + 1 < rows) {
int colProd = mat[i][j] * mat[i + 1][j];
if (colProd > maxProduct) {
maxProduct = colProd;
pair1 = mat[i][j];
pair2 = mat[i + 1][j];
printf("Maximum product = %d - [%d %d]\n", maxProduct, pair1, pair2);
return 0;
4.Diagonal Traversal
Given an m x n matrix mat, return an array of all the elements of the array in a
diagonal order
Code:
#include <stdio.h>
#include <stdlib.h>
int* findDiagonalOrder(int** matrix, int matrixSize, int* matrixColSize, int* returnSize) {
if (matrix == NULL || matrixSize == 0 || *matrixColSize == 0) {
*returnSize = 0;
return NULL;
int rows = matrixSize;
int cols = *matrixColSize;
int* result = (int*)malloc(rows * cols * sizeof(int));
int r = 0, c = 0;
int i;
for (i = 0; i < rows * cols; i++) {
result[i] = matrix[r][c];
if ((r + c) % 2 == 0) { // Move Up
if (c == cols - 1) {
r++;
} else if (r == 0) {
c++;
} else {
r--;
c++;
} else { // Move Down
if (r == rows - 1) {
c++;
} else if (c == 0) {
r++;
} else {
r++;
c--;
*returnSize = rows * cols;
return result;