0% found this document useful (0 votes)
4 views9 pages

2d Array

The document provides a comprehensive guide on 2D arrays in C, covering their declaration, initialization, and element access. It includes example programs for inputting and displaying a 2D array, performing matrix addition, calculating the transpose, finding the determinant and inverse of a matrix, and matrix multiplication. Each section is accompanied by code snippets demonstrating the respective operations.

Uploaded by

satyamydv0808
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)
4 views9 pages

2d Array

The document provides a comprehensive guide on 2D arrays in C, covering their declaration, initialization, and element access. It includes example programs for inputting and displaying a 2D array, performing matrix addition, calculating the transpose, finding the determinant and inverse of a matrix, and matrix multiplication. Each section is accompanied by code snippets demonstrating the respective operations.

Uploaded by

satyamydv0808
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

A 2D array in C is essentially an array of arrays.

It is commonly used to
represent data in a matrix or table format, such as a grid or
spreadsheet.

Declaration of a 2D Array

data_type array_name[row_size][column_size];

Example:

int matrix[3][4]; // A 2D array with 3 rows and 4 columns

🔹 Initialization of a 2D Array

int matrix[3][4] = { {1, 2, 3,4}, { 5, 6,7,8}, {9,10,11,12} }; // Row-wise

Compiled By: [Link] Mishra


Hod, computer department
k.m.c college
🔹 Accessing Elements in a 2D Array

matrix[0][2]; // Accesses the element in 1st row and 3rd column

Example Program to Input and Display a 2D Array

#include <stdio.h>

int main() {
int i, j;
int matrix[2][3];

// Input
printf("Enter elements of 2x3 matrix:\n");
for(i = 0; i < 2; i++) {
for(j = 0; j < 3; j++) {
Compiled By: [Link] Mishra
Hod, computer department
k.m.c college
printf("Enter element [%d][%d]: ", i, j);
scanf("%d", &matrix[i][j]);
}
}

// Output
printf("\nMatrix is:\n");
for(i = 0; i < 2; i++) {
for(j = 0; j < 3; j++) {
printf("%d\t", matrix[i][j]);
}
printf("\n");
}

return 0;
}

1. Matrix Addition (User-defined size)

#include <stdio.h>

int main() {
int i, j, rows, cols;

// Asking number of rows and columns


printf("Enter number of rows and columns: ");
scanf("%d %d", &rows, &cols);

int a[rows][cols], b[rows][cols], sum[rows][cols];

// Input elements of first matrix


printf("Enter elements of first matrix:\n");
for(i = 0; i < rows; i++) {
Compiled By: [Link] Mishra
Hod, computer department
k.m.c college
for(j = 0; j < cols; j++) {
printf("a[%d][%d] = ", i, j);
scanf("%d", &a[i][j]);
}
}

// Input elements of second matrix


printf("Enter elements of second matrix:\n");
for(i = 0; i < rows; i++) {
for(j = 0; j < cols; j++) {
printf("b[%d][%d] = ", i, j);
scanf("%d", &b[i][j]);
}
}

// Matrix addition
for(i = 0; i < rows; i++) {
for(j = 0; j < cols; j++) {
sum[i][j] = a[i][j] + b[i][j];
printf("%d\t", sum[i][j]);
}
printf("\n");
}

return 0;
}

Transpose of a matrix

#include <stdio.h>

int main() {
Compiled By: [Link] Mishra
Hod, computer department
k.m.c college
int rows, cols, i, j;

// Ask the user for the number of rows and columns


printf("Enter the number of rows and columns of the matrix: ");
scanf("%d %d", &rows, &cols);

int matrix[rows][cols], transpose[cols][rows];

// Input elements of the matrix


printf("Enter the elements of the matrix:\n");
for(i = 0; i < rows; i++) {
for(j = 0; j < cols; j++) {
printf("matrix[%d][%d] = ", i, j);
scanf("%d", &matrix[i][j]);
}
}

// Compute the transpose


for(i = 0; i < rows; i++) {
for(j = 0; j < cols; j++) {
transpose[j][i] = matrix[i][j];
}
}

// Display the original matrix


printf("\nOriginal Matrix:\n");
for(i = 0; i < rows; i++) {
for(j = 0; j < cols; j++) {
printf("%d\t", matrix[i][j]);
}
printf("\n");
}

// Display the transpose


Compiled By: [Link] Mishra
Hod, computer department
k.m.c college
printf("\nTranspose of the Matrix:\n");
for(i = 0; i < cols; i++) {
for(j = 0; j < rows; j++) {
printf("%d\t", transpose[i][j]);
}
printf("\n");
}

return 0;
}

Determinant of a matrix

#include <stdio.h>

int main() {
float a[2][2], inverse[2][2], determinant;
int i, j;

// Input matrix elements


printf("Enter elements of 2x2 matrix:\n");
for(i = 0; i < 2; i++) {
for(j = 0; j < 2; j++) {
printf("a[%d][%d] = ", i, j);
scanf("%f", &a[i][j]);
}
}

// Calculate determinant
determinant = (a[0][0] * a[1][1]) - (a[0][1] * a[1][0]);

if(determinant == 0) {
printf("Inverse not possible! Determinant is zero.\n");
return 0;
Compiled By: [Link] Mishra
Hod, computer department
k.m.c college
}

// Calculate inverse
inverse[0][0] = a[1][1] / determinant;
inverse[0][1] = -a[0][1] / determinant;
inverse[1][0] = -a[1][0] / determinant;
inverse[1][1] = a[0][0] / determinant;

// Display inverse
printf("\nInverse of the matrix:\n");
for(i = 0; i < 2; i++) {
for(j = 0; j < 2; j++) {
printf("%.2f\t", inverse[i][j]);
}
printf("\n");
}

return 0;
}

Matrix Multiplication

#include <stdio.h>

int main() {
int i, j, k, r1, c1, r2, c2;

// Input sizes
printf("Enter rows and columns of first matrix: ");
scanf("%d %d", &r1, &c1);
Compiled By: [Link] Mishra
Hod, computer department
k.m.c college
printf("Enter rows and columns of second matrix: ");
scanf("%d %d", &r2, &c2);

// Check for multiplication compatibility


if(c1 != r2) {
printf("Matrix multiplication not possible!\n");
return 0;
}

int a[r1][c1], b[r2][c2], result[r1][c2];

// Input first matrix


printf("Enter elements of first matrix:\n");
for(i = 0; i < r1; i++) {
for(j = 0; j < c1; j++) {
printf("a[%d][%d] = ", i, j);
scanf("%d", &a[i][j]);
}
}

// Input second matrix


printf("Enter elements of second matrix:\n");
for(i = 0; i < r2; i++) {
for(j = 0; j < c2; j++) {
printf("b[%d][%d] = ", i, j);
scanf("%d", &b[i][j]);
}
}

// Initialize result matrix


for(i = 0; i < r1; i++) {
for(j = 0; j < c2; j++) {
result[i][j] = 0;
Compiled By: [Link] Mishra
Hod, computer department
k.m.c college
}
}

// Multiply matrices
for(i = 0; i < r1; i++) {
for(j = 0; j < c2; j++) {
for(k = 0; k < c1; k++) {
result[i][j] += a[i][k] * b[k][j];
}
}
}

// Display result
printf("Product of the matrices:\n");
for(i = 0; i < r1; i++) {
for(j = 0; j < c2; j++) {
printf("%d\t", result[i][j]);
}
printf("\n");
}

return 0;
}

Compiled By: [Link] Mishra


Hod, computer department
k.m.c college

You might also like