Tag Archives: Matrix

C program to interchange diagonals of a matrix

Write a C program to read elements in a matrix and interchange elements of primary(major) diagonal with secondary(minor) diagonal. C program for interchanging diagonals of a matrix. Logic to interchange diagonals of a matrix in C programming.

Example

Input

Input matrix elements: 
1 2 3
4 5 6
7 8 9

Output

Matrix after interchanging its diagonal:
3 2 1
4 5 6
9 8 7

Continue reading C program to interchange diagonals of a matrix

C program to find sum of each row and columns of a matrix

Write a C program to read elements in a matrix and find the sum of elements of each row and columns of matrix. C program to calculate sum of rows and columns of matrix. Logic to find sum of each row and columns of a matrix in C programming.

Example

Input

Input elements in array: 
1 2 3
4 5 6
7 8 9

Output

Sum of row 1 = 6
Sum of row 2 = 15
...
...
Sum of column 3 = 18

Continue reading C program to find sum of each row and columns of a matrix

C program to find the sum of opposite diagonal elements of a matrix

Write a C program to read elements in a matrix and find the sum of minor diagonal (opposite diagonal) elements. C program to calculate sum of minor diagonal elements. Logic to find sum of opposite diagonal elements of a matrix in C programming.

Example

Input

Input elements in array:
1 2 3
4 5 6
7 8 9

Output

Sum of minor diagonal elements = 15

Continue reading C program to find the sum of opposite diagonal elements of a matrix

C program to find sum of main diagonal elements of a matrix

Write a C program to read elements in a matrix and find the sum of main diagonal (major diagonal) elements of matrix. Find sum of all elements of main diagonal of a matrix. Logic to find sum of main diagonal elements of a matrix in C programming.

Example

Input

Input array elements: 
1 2 3
4 5 6
7 8 9

Output

Sum of main diagonal elements = 15

Continue reading C program to find sum of main diagonal elements of a matrix

C program to check whether two matrices are equal or not

Write a C program to enter elements in two matrices and check whether both matrices are equal or not. C program to check whether elements of two matrices are equal or not. Logic to check if two matrices are equal or not in C programming.

Example

Input

Input elements of matrix1:
1 2 3
4 5 6
7 8 9

Input elements of matrix2:
1 2 3
4 5 6
7 8 9

Output

Both matrices are equal

Continue reading C program to check whether two matrices are equal or not

C program to perform Scalar matrix multiplication

Write a C program to read elements in a matrix and perform scalar multiplication of matrix. C program for scalar multiplication of matrix. How to perform scalar matrix multiplication in C programming. Logic to perform scalar matrix multiplication in C program.

Example

Input

Input elements of matrix A:
1 2 3
4 5 6
7 8 9
Input multiplier: 2

Output

2  4  6
8 10 12
14 16 18

Required knowledge

Basic C programming, For loop, Array

Scalar multiplication of matrix

Scalar multiplication of matrix is the simplest and easiest way to multiply matrix. Scalar multiplication of matrix is defined by -
(cA)ij = c . Aij (Where 1 ≤ i ≤ m and 1 ≤ j ≤ n)

Scalar multiplication of matrix

Read more - Program to multiply two matrices

Program to perform scalar matrix multiplication

/**
 * C program to perform scalar matrix multiplication
 */
 
#include <stdio.h>

#define SIZE 3 // Maximum size of the array

int main()
{
    int A[SIZE][SIZE]; 
    int num, row, col;

    /* Input elements in matrix from user */
    printf("Enter elements in matrix of size %dx%d: \n", SIZE, SIZE);
    for(row=0; row<SIZE; row++)
    {
        for(col=0; col<SIZE; col++)
        {
            scanf("%d", &A[row][col]);
        }
    }

    /* Input multiplier from user */
    printf("Enter any number to multiply with matrix A: ");
    scanf("%d", &num);

    /* Perform scalar multiplication of matrix */
    for(row=0; row<SIZE; row++)
    {
        for(col=0; col<SIZE; col++)
        {
            /* (cAij) = c . Aij */
            A[row][col] = num * A[row][col];
        }
    }

    /* Print result of scalar multiplication of matrix */
    printf("\nResultant matrix c.A = \n");
    for(row=0; row<SIZE; row++)
    {
        for(col=0; col<SIZE; col++)
        {
            printf("%d ", A[row][col]);
        }
        printf("\n");
    }

    return 0;
}

Output

Enter elements in matrix of size 3x3:
1 2 3
4 5 6
7 8 9
Enter any number to multiply with matrix A: 2

Resultant matrix c.A =
2  4  6
8 10 12
14 16 18

Happy coding 😉