0% found this document useful (0 votes)
2 views3 pages

2D Array Program

The document provides two C programs: one for adding two matrices using 2D arrays and another for calculating the normal and trace of a matrix. The first program prompts the user for matrix dimensions and elements, computes their sum, and displays the result. The second program calculates the normal as the square root of the sum of squares of all elements and the trace as the sum of the main diagonal elements.
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)
2 views3 pages

2D Array Program

The document provides two C programs: one for adding two matrices using 2D arrays and another for calculating the normal and trace of a matrix. The first program prompts the user for matrix dimensions and elements, computes their sum, and displays the result. The second program calculates the normal as the square root of the sum of squares of all elements and the trace as the sum of the main diagonal elements.
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

1.

Implementation for addition of two matrixes in c using 2d array

CODE: -
#include <stdio.h>

// initiation of main
int main() {
int r, c;
int mat1[100][100], mat2[100][100], sum[100][100];

// Input dimensions
printf("Enter number of rows: ");
scanf("%d", &r);
printf("Enter number of columns: ");
scanf("%d", &c);

// Input first matrix


printf("Enter elements of Matrix 1:\n");
for (int i = 0; i < r; ++i) {
for (int j = 0; j < c; ++j) {
printf("Element [%d][%d]: ", i, j);
scanf("%d", &mat1[i][j]);
}
}

// Input second matrix


printf("Enter elements of Matrix 2:\n");
for (int i = 0; i < r; ++i) {
for (int j = 0; j < c; ++j) {
printf("Element [%d][%d]: ", i, j);
scanf("%d", &mat2[i][j]);
}
}

// Adding matrices
for (int i = 0; i < r; ++i) {
for (int j = 0; j < c; ++j) {
sum[i][j] = mat1[i][j] + mat2[i][j];
}
}

// Output result
printf("Sum of the two matrices:\n");
for (int i = 0; i < r; ++i) {
for (int j = 0; j < c; ++j) {
printf("%d ", sum[i][j]);
}
printf("\n");
}

return 0;
}
Input
Number of rows: 3
Number of columns: 3

Matrix 1:
012
345
678

Matrix 2:
9 10 11
12 13 14
15 16 17

Output
The sum of the two matrices:
9 11 13
15 17 19
21 23 25

2. Find Normal and Trace of a Matrix using 2D Array in C

/*
* C program to find the trace and normal of a matrix
*
* Trace is defined as the sum of main diagonal elements and
* Normal is defined as square root of the sum of all the elements
*/

#include <stdio.h>
#include <math.h>

void main ()
{

static int array[10][10];


int i, j, m, n, sum = 0, sum1 = 0, a = 0, normal;

printf("Enter the order of the matrix\n");


scanf("%d %d", &m, &n);

printf("Enter the n coefficients of the matrix \n");


for (i = 0; i < m; ++i)
{
for (j = 0; j < n; ++j)
{
scanf("%d", &array[i][j]);
a = array[i][j] * array[i][j];
sum1 = sum1 + a;
}
}

normal = sqrt(sum1);
printf("The normal of the given matrix is = %d\n", normal);
for (i = 0; i < m; ++i)
{
sum = sum + array[i][i];
}

printf("Trace of the matrix is = %d\n", sum);

Output: -
Enter the order of the matrix
33
Enter the coefficients of the matrix
379
2 6 10
859
The normal of the given matrix is = 21
Trace of the matrix is = 18

You might also like