0% found this document useful (0 votes)
18 views7 pages

Matrix Operations in C Programming

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)
18 views7 pages

Matrix Operations in C Programming

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

//primary diagonal sum

#include <stdio.h>

int main()
{
int r,c,i,j,pdiag_sum = 0;

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


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

int matrix[r][c];

printf("Enter %d elements of the matrix:\n",r*c);


for (i = 0; i < r; i++)
{
for (j = 0; j < c; j++)
{
scanf("%d", &matrix[i][j]);
}
}
printf("The Matirx are:\n");
for (i = 0; i < r; i++)
{
for (j = 0; j < c; j++)
{
printf("%d\t", matrix[i][j]);
}
printf("\n");
}

for (i = 0; i < r; i++)


{
for (j = 0; j < c; j++)
{
if(i==j)
{
pdiag_sum=pdiag_sum+matrix[i][j];
}

}
}
printf("The Sum of primary diagonal elements is : %d\n", pdiag_sum);

return 0;
}
// sum of secondary diagonal elements
#include <stdio.h>

int main()
{
int r,c,i,j,Sdiag_sum = 0;

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


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

int matrix[r][c];

printf("Enter %d elements of the matrix:\n",r*c);


for (i = 0; i < r; i++)
{
for (j = 0; j < c; j++)
{
scanf("%d", &matrix[i][j]);
}
}
printf("The Matirx are:\n");
for (i = 0; i < r; i++)
{
for (j = 0; j < c; j++)
{
printf("%d\t", matrix[i][j]);
}
printf("\n");
}

Sdiag_sum = 0;

for (i = 0; i < r && i < c; i++)


{
Sdiag_sum =Sdiag_sum + matrix[i][r-1-i];
}

printf("The Sum of Secondary diagonal elements is : %d\n", Sdiag_sum);

return 0;
}
//symmetric or not
#include <stdio.h>
int main()
{
int n, m, symmetric = 1;

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


scanf("%d", &n);
printf("Enter the number of columns (m): ");
scanf("%d", &m);
if (n != m)
{
printf("The matrix is not symmetric because it's not a square matrix.\n");
return 0;
}
int matrix[n][m];

printf("Enter %d elements of the matrix:\n",n*m);


for (int i = 0; i < n; i++)
{
for (int j = 0; j < m; j++)
{
scanf("%d", &matrix[i][j]);
}
}
printf("The given matrix is:\n");
for (int i = 0; i < n; i++)
{
for (int j = 0; j < m; j++)
{
printf("%5d", matrix[i][j]);
}
printf("\n");
}

// Check if matrix[i][j] == matrix[j][i]


for (int i = 0; i < n; i++)
{
for (int j = 0; j < m; j++)
{
if (matrix[i][j] != matrix[j][i])
{
symmetric = 0;
break;
}
}
}
// Output the result
if (symmetric==1)
{
printf("The above matrix is symmetric.\n");
}
else
{
printf("The above matrix is not symmetric.\n");
}

return 0;
}
// Addition of two matrix
#include<stdio.h>
int main()
{
int i,j,row1,col1,row2,col2;

printf("Enter the number of rows and columns for A matrix:");


scanf("%d %d", &row1, &col1);
printf("Enter the number of rows and columns for B matrix:");
scanf("%d %d", &row2, &col2);
int a[row1][col1],b[row2][col2],c[row1][col1];

if( (row1!= row2) || (col1!=col2) ) // checking the compatability for addition


{
printf("Error: addition is not possible\n");
}

else
{
printf("Enter %d elements for A \n",row1*col1);
for(i=0;i<row1;i++)
{
for(j=0;j<col1;j++)
{
scanf("%d",&a[i][j]);
}
}

printf("Enter %d elements for B \n",row2*col2);


for(i=0;i<row2;i++)
{
for(j=0;j<col2;j++)
{
scanf("%d",&b[i][j]);
}
}
printf("The Elements of Matrix A is\n");

for(i=0;i<row1;i++)
{
for(j=0;j<col1;j++)
{
printf("%d\t",a[i][j]);

}
printf("\n");
}
printf("The Elements of Matrix B is\n");

for(i=0;i<row2;i++)
{
for(j=0;j<col2;j++)
{
printf("%d\t",b[i][j]);
}
printf("\n");
}

for(i=0;i<row1;i++)
{
for(j=0;j<row2;j++)
{
c[i][j]=a[i][j]+b[i][j];

}
}

printf("The sum of matrix A and B is:\n");


for(i=0;i<row1;i++)
{
for(j=0;j<row2;j++)
{
printf("%d\t",c[i][j]);
}
printf("\n\n");

}
}
return 0;
}
// write c program to find the multiplication of two matrix
#include <stdio.h>

int main()
{

int row1, col1, row2, col2,i,j,k;

printf("Enter the number of rows and columns for A matrix:");


scanf("%d %d", &row1, &col1);
printf("Enter the number of rows and columns for B matrix:");
scanf("%d %d", &row2, &col2);
int A[row1][col1],B[row2][col2],C[row1][col2];

if (col1 != row2)
{
printf("Error:Multipilication is not possible\n");

}
else
{
printf("Enter %d elements of A matrix: \n",row1*col1);
for (i = 0; i < row1; i++)
{
for (j = 0; j < col1; j++)
{

scanf("%d", &A[i][j]);
}
}

printf("Enter %d elements of B matrix:\n",row2*col2);


for (i = 0; i < row2; i++)
{
for (j = 0; j < col2; j++)
{

scanf("%d", &B[i][j]);
}
}

// Matrix multiplication
for (i = 0; i < row1; i++)
{
for (j = 0; j < col2; j++)
{
C[i][j]=0;
for (k = 0; k < col1; k++)
{
C[i][j] = C[i][j] + A[i][k] * B[k][j];
}
}
}

// Display the result matrix


printf("\nThe result of matrix multiplication is:\n");
for (i = 0; i < row1; i++)
{
for (j = 0; j < col2; j++)
{
printf("%d\t", C[i][j]);
}
printf("\n");
}
}
return 0;
}

Common questions

Powered by AI

Nested loops in matrix handling facilitate traversals over two-dimensional arrays, enabling access to each element through row-column index combinations. They simplify operations like initialization, reading inputs, and computing arithmetic operations, due to their inherent structured iteration over matrix indices. This structure is crucial for implementing algorithms that rely on element-wise operations such as addition, multiplication, or checks for properties like symmetry .

Matrix addition differs from multiplication in that each element in matrices A and B contributes directly to a single element in the result matrix C by addition A[i][j] + B[i][j], whereas multiplication requires iterating across columns of A and rows of B to calculate each C[i][j] as a sum of products. Addition simply checks for dimension equality upfront; multiplication requires validation of conformability (col1 = row2) and involves deeper nested loops to perform dot products across row-column pairs .

Incorrectly assuming matrix dimension compatibility in arithmetic operations can lead to runtime errors or logical errors. For addition, dimensions must be identical; failure can result in unfillability of the resultant matrix, erroneous summation results, or segmentation faults due to out-of-bounds errors. In multiplication, incorrect dimension assumptions (col1 != row2) prevent valid matrix creation, resulting in either program termination or incorrect computations due to misaligned row-column computation .

In order for two matrices to be added in a C program, they must be of the same dimensions, i.e., both matrices must have equal number of rows and columns. Specifically, if matrix A has dimensions row1 x col1 and matrix B has dimensions row2 x col2, the condition row1 == row2 and col1 == col2 must be satisfied. If this condition is not met, addition is not possible and an error message is returned .

Matrix multiplication in a C program involves several key steps: First, it checks whether the number of columns in the first matrix (col1) equals the number of rows in the second matrix (row2), which is necessary for multiplication. If the condition is satisfied, it proceeds to read the matrix elements into matrices A and B. The multiplication itself involves nested loops; for each element in the resulting matrix C, the sum of the products of corresponding elements from a row of A and a column of B is computed. Specifically, element C[i][j] is the sum of A[i][k] * B[k][j] over all valid k .

The primary diagonal sum of a matrix in the C program is calculated by iterating through the matrix and adding elements where the row index equals the column index. This is achieved using a loop where if the condition 'i == j' is satisfied, the element at matrix[i][j] is added to a running total, pdiag_sum, which maintains the sum of the primary diagonal elements .

To determine if a matrix is symmetric in C programming, the matrix must first be square, meaning it has the same number of rows and columns. After this check, the matrix is evaluated by verifying that all elements satisfy the condition matrix[i][j] == matrix[j][i] for all indices i and j. If any element does not satisfy this condition, the matrix is declared not symmetric. Otherwise, it is symmetric .

The sum of the secondary diagonal elements in a C program is calculated by iterating through the matrix and summing elements where the column index is the reverse of the row index. Specifically, the element matrix[i][r-1-i] is added for each row index i, where r is the total number of rows. This involves a single loop iterating over the possible indices for i .

To ensure that a matrix is suitable for a symmetry check, the program first verifies that the matrix is square by checking if the number of rows (n) equals the number of columns (m). If n != m, the matrix is automatically deemed not symmetric without further checks. This initial check is essential because only square matrices can potentially be symmetric .

Possible coding errors in implementing matrix addition in C include mismatching matrix dimensions where row1 != row2 or col1 != col2, attempting to sum matrices of different sizes. Additionally, errors can occur during input where incorrect numbers of elements are entered for the specified dimensions. The output might also be mismanaged if loops do not correctly iterate through all necessary indices for filling the result matrix .

You might also like