Arrays In C: Understanding Matrix Applications With Examples

Matrix Applications In C

Matrix is mathematical term. A Matrix is the "arrangement of numbers in row and column" like an 2-D Array. We use matrix computation examples to better understand array handling for examples addition,subtraction,multiplication on matrices.


Program 1 : 
Write a program to addition/subtraction/multiplication of two matrix into third matrix.


#include <stdio.h>

void  main() {

int A[3][3], B[3][3], C[3][3];


printf(" Enter 1st matrix value - ");


for(int i =0;i<=2; i++ ){

   for(int j=0; j<=2;j++){

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

   }

 }


printf("\nEnter 2nd matrixvalue - ");


for(int i =0;i<=2; i++ ){

   for(int j=0; j<=2;j++){

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

   }

 }

printf ("Adding A and B into C matrix\n");


for(int i =0;i<=2; i++ ){

   for(int j=0; j<=2;j++){

    C[i][j] = A[i][j] + B[i][j];

    printf ("%d  ",C[i][j]);

   }

   printf("\n");

  }

}


Note: Same as we can find subtraction and multiplication of matrix. 

Program 2 : 
Write a program to add diagonals of a matrix.

#include <stdio.h>

void  main() {

int A[3][3], sum=0;


printf(" Enter matrix value - ");


for(int i =0;i<=2; i++ ){

   for(int j=0; j<=2;j++){

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

   }

 }


printf ("Getting sum of diagonals of matrix A\n");


for(int i =0;i<=2; i++ ){

    sum = sum + A[i][i]; 
    // row and column position would be same for diagonals.

  }

 printf(" Sum of diagonals of A metrics - %d -",sum);
}

Note: Make sure your matrix should be square i.e. same number of rows and columns. 

Program 3 : 
Write a program to get the transpose of the matrix.

#include <stdio.h>

void  main() {
  
  int A[3][4];


  printf(" Enter matrix value - ");

  for(int i =0;i<=2; i++ ){

    for(int j=0; j<=3;j++){

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

    }
  }

  printf("Print matrix value -\n");

  for(int i =0;i<=2; i++ ){

    for(int j=0; j<=3;j++){

    printf("%d  ",A[i][j]);

    }

    printf("\n");

  }

 printf ("Print transpose of the matrix\n");

  for(int i =0;i<=3; i++ ){

    for(int j=0; j<=2;j++){

    printf ("%d ",A[j][i]);

    }

    printf("\n");

  }

}

Note: Transpose is to exchange row and column values of matrix, column values will be placed in rows and vice-versa. 

As you can see in the above example that we have 3 rows and 4 columns

1 2 3 4
5 6 7 8
4 7 9 1

After transpose matrix should display  
1 5 4
2 6 7
3 7 9
4 8 1

each column value is placed row wise. To get solution follow few steps given below

1.
1st loop (that runs for number of row) will run for number of columns. 

2.
2nd loop (that runs for number of column) will run for number of rows.

3.
Read column wise values and print on screen. 


All examples will let you understand how array works, these examples are just for practicing on arrays, I am sure after this you would be able to create your examples on arrays. :)

Thanks for reading,please do share and comment your suggestions.

No comments

Powered by Blogger.