0% found this document useful (0 votes)
19 views12 pages

Assigment 8 Demonstration of 2d - Array

Uploaded by

HarishPatil
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views12 pages

Assigment 8 Demonstration of 2d - Array

Uploaded by

HarishPatil
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 12

Assigment 8 Demonstration of 2-D Arrays

Two-dimensionalArrays
The simplest form of multidimensional array is the two-dimensional
array. A two-dimensional array is, in
essence, a list of one-dimensional arrays. To declare a two-dimensional
integer array of size [x][y], you
would write something as follows −

type arrayName [ x ][ y ];
Where type can be any valid C data type and arrayName will be a valid
C identifier. A two-dimensional
array can be considered as a table which will have x number of rows and
y number of columns. A two
dimensional array a, which contains three rows and four columns can be
shown as follows –
int a[3][4];

#include <stdio.h>
#include<conio.h>

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

// Input the matrix dimensions from the user

printf("Enter the number of rows: ");


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

int matrix[rows][cols];

// Input the matrix elements


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

// Initialize max and min with the first element of the matrix
int max_element = matrix[0][0];
int min_element = matrix[0][0];

// Find the maximum and minimum elements


for (i = 0; i < rows; i++) {
for (j = 0; j < cols; j++) {
if (matrix[i][j] > max_element)
{
max_element = matrix[i][j];
}
if (matrix[i][j] < min_element)
{
min_element = matrix[i][j];
}
}
}

// Print the results


printf("Maximum element in the matrix: %d\n", max_element);
printf("Minimum element in the matrix: %d\n", min_element);

Getch();
}

2) Write a program to calculate sum of all elements of a matrix.

#include <stdio.h>
#include<conio.h>
void main() {
int rows, cols, i, j, sum = 0;

// Input the matrix dimensions from the user


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

int matrix[rows][cols];

// Input the matrix elements


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

// Calculate the sum of all elements in the matrix


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

// Print the sum


printf("Sum of all elements in the matrix: %d\n", sum);

Getch();
}
3) Write a program to calculate sum of all even elements of a matrix
#include <stdio.h>
#include<conio.h>
void main() {
int rows, cols, i, j, sum = 0;

// Input the matrix dimensions from the user


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

int matrix[rows][cols];

// Input the matrix elements


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

// Calculate the sum of all even elements in the matrix


for (i = 0; i < rows; i++) {
for (j = 0; j < cols; j++) {
if (matrix[i][j] % 2 == 0) {
sum =sum+matrix[i][j];
}
}
}

// Print the sum of even elements


printf("Sum of all even elements in the matrix: %d\n", sum);

Getch();
}
4) Write a program to calculate sum of all upper triangular matrix
elements
#include <stdio.h>
#include<conio.h>
void main() {
int rows, cols, i, j, sum = 0;

// Input the matrix dimensions from the user


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

// Ensure it's a square matrix


if (rows != cols) {
printf("Matrix must be square to have an upper triangular part.\n");
return 1;
}

int matrix[rows][cols];

// Input the matrix elements


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

// Calculate the sum of upper triangular elements (including diagonal)


for (i = 0; i < rows; i++) {
for (j = i; j < cols; j++) { // Start from the diagonal (i = j) to the end
sum = sum+matrix[i][j];
}
}

// Print the sum


printf("Sum of all upper triangular elements in the matrix: %d\n",
sum);

Getch();
}

#include <stdio.h>
#include<conio.h>
void main() {
int rows, cols, i,j, sum = 0;

// Input the matrix dimensions from the user


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

// Ensure it's a square matrix


if (rows != cols) {
printf("Matrix must be square to have diagonal elements.\n");
return 1;
}

int matrix[rows][cols];

// Input the matrix elements


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

// Calculate the sum of diagonal elements (main diagonal)

for (i = 0; i < rows; i++) {


sum =sum+ matrix[i][i]; // Main diagonal elements (i == j)
}

// Print the sum


printf("Sum of all diagonal elements in the matrix: %d\n", sum);

Getch();
}

6) Write a program to calculate addition of two matrices of order m*n.

#include <stdio.h>
#include<conio.h>
void main() {
int rows, cols, i, j;

// Input the matrix dimensions from the user


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

int matrix1[rows][cols], matrix2[rows][cols], result[rows][cols];

// Input the elements of the first matrix


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

// Input the elements of the second matrix


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

// Calculate the sum of the two matrices


for (i = 0; i < rows; i++) {
for (j = 0; j < cols; j++) {
result[i][j] = matrix1[i][j] + matrix2[i][j];
}
}

// Print the result matrix


printf("The sum of the two matrices is:\n");
for (i = 0; i < rows; i++) {
for (j = 0; j < cols; j++) {
printf("%d ", result[i][j]);
}
printf("\n");
}

Getch();
}

7) Write a program to calculate multiplication of two matrices.

#include <stdio.h>
#include<conio.h>
void main() {
int rows1, cols1, rows2, cols2, i, j, k;

// Input dimensions of the first matrix


printf("Enter the number of rows for the first matrix: ");
scanf("%d", &rows1);
printf("Enter the number of columns for the first matrix: ");
scanf("%d", &cols1);

// Input dimensions of the second matrix


printf("Enter the number of rows for the second matrix: ");
scanf("%d", &rows2);
printf("Enter the number of columns for the second matrix: ");
scanf("%d", &cols2);

// Check if multiplication is possible


if (cols1 != rows2) {
printf("Matrix multiplication not possible! Number of columns in
the first matrix must be equal to the number of rows in the second
matrix.\n");
return 1;
}

int matrix1[rows1][cols1], matrix2[rows2][cols2], result[rows1]


[cols2];

// Initialize result matrix to 0


for (i = 0; i < rows1; i++) {
for (j = 0; j < cols2; j++) {
result[i][j] = 0;
}
}

// Input elements of the first matrix


printf("Enter the elements of the first matrix:\n");
for (i = 0; i < rows1; i++) {
for (j = 0; j < cols1; j++) {
printf("Element [%d][%d]: ", i+1, j+1);
scanf("%d", &matrix1[i][j]);
}
}

// Input elements of the second matrix


printf("Enter the elements of the second matrix:\n");
for (i = 0; i < rows2; i++) {
for (j = 0; j < cols2; j++) {
printf("Element [%d][%d]: ", i+1, j+1);
scanf("%d", &matrix2[i][j]);
}
}

// Matrix multiplication
for (i = 0; i < rows1; i++) {
for (j = 0; j < cols2; j++) {
for (k = 0; k < cols1; k++) {
result[i][j] += matrix1[i][k] * matrix2[k][j];
}
}
}

// Print the result matrix


printf("The product of the two matrices is:\n");
for (i = 0; i < rows1; i++) {
for (j = 0; j < cols2; j++) {
printf("%d ", result[i][j]);
}
printf("\n");
}

Getch();
}

8) Write a program to display transpose of a matrix

#include <stdio.h>
#include<conio.h>
void main() {
int rows, cols, i, j;

// Input the matrix dimensions from the user


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

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

// Input the matrix elements


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

// Calculate the transpose of the matrix


for (i = 0; i < rows; i++) {
for (j = 0; j < cols; j++) {
transpose[j][i] = matrix[i][j];
}
}
// Print the transpose of the matrix
printf("Transpose of the matrix is:\n");
for (i = 0; i < cols; i++) {
for (j = 0; j < rows; j++) {
printf("%d ", transpose[i][j]);
}
printf("\n");
}

Getch();
}

9) Write a program to calculate sum of elements of each row and column.

#include <stdio.h>
#include<conio.h>
void main() {
int rows, cols, i, j;

// Input the matrix dimensions from the user


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

int matrix[rows][cols];
int rowSum[rows], colSum[cols];

// Initialize row and column sums to 0


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

// Input the matrix elements


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

// Calculate the sum of each row and column


for (i = 0; i < rows; i++) {
for (j = 0; j < cols; j++) {
rowSum[i] += matrix[i][j]; // Sum for each row
colSum[j] += matrix[i][j]; // Sum for each column
}
}

// Print the sum of each row


printf("\nSum of each row:\n");
for (i = 0; i < rows; i++) {
printf("Row %d: %d\n", i + 1, rowSum[i]);
}

// Print the sum of each column


printf("\nSum of each column:\n");
for (j = 0; j < cols; j++) {
printf("Column %d: %d\n", j + 1, colSum[j]);
}

Getch();
}
10) Write a program to check whether a input matrix is identity matrix or
not.

#include <stdio.h>
#include<conio.h>
void main() {
int matrix[3][3], isIdentity = 1;

// Get input from the user for the matrix elements


printf("Enter the elements of the 3x3 matrix:\n");
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
scanf("%d", &matrix[i][j]);
}
}
// Check if the matrix is identity
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
if (i == j && matrix[i][j] != 1) {
isIdentity = 0;
break;
} else if (i != j && matrix[i][j] != 0) {
isIdentity = 0;
break;
}
}
}

if (isIdentity) {
printf("The matrix is an identity matrix.\n");
} else {
printf("The matrix is not an identity matrix.\n");
}

Getch();
}

You might also like