C code for for matrices


 

Example(a): Program to Add Two Matrices

  1. #include
  2. int main(){
  3. int r, c, a[100][100], b[100][100], sum[100][100], i, j;
  4. printf("Enter number of rows (between 1 and 100): ");
  5. scanf("%d", &r);
  6. printf("Enter number of columns (between 1 and 100): ");
  7. scanf("%d", &c);
  8. printf(" Enter elements of 1st matrix: ");
  9. for(i=0; i<r; ++i)
  10. for(j=0; j<c; ++j)
  11. {
  12. printf("Enter element a%d%d: ",i+1,j+1);
  13. scanf("%d",&a[i][j]);
  14. }
  15. printf("Enter elements of 2nd matrix: ");
  16. for(i=0; i<r; ++i)
  17. for(j=0; j<c; ++j)
  18. {
  19. printf("Enter element a%d%d: ",i+1, j+1);
  20. scanf("%d", &b[i][j]);
  21. }
  22. // Adding Two matrices
  23. for(i=0;i<r;++i)
  24. for(j=0;j<c;++j)
  25. {
  26. sum[i][j]=a[i][j]+b[i][j];
  27. }
  28. // Displaying the result
  29. printf(" Sum of two matrices: ");
  30. for(i=0;i<r;++i)
  31. for(j=0;j<c;++j)
  32. {
  33. printf("%d ",sum[i][j]);
  34. if(j==c-1)
  35. {
  36. printf(" ");
  37. }
  38. }
  39. return 0;
  40. }

Output

Enter number of rows (between 1 and 100): 2
Enter number of columns (between 1 and 100): 3

Enter elements of 1st matrix:
Enter element a11: 5
Enter element a12: 3
Enter element a13: 4
Enter element a21: 1
Enter element a22: 2
Enter element a23: 3
Enter elements of 2nd matrix:
Enter element a11: -4
Enter element a12: 6
Enter element a13: 3
Enter element a21: 5
Enter element a22: 6
Enter element a23: 3

Sum of two matrices: 
1   9   7   

6   8   6 

Example{b}: Program to Multiply Two Matrices

  1. #include
  2. int main()
  3. {
  4. int a[10][10], b[10][10], result[10][10], r1, c1, r2, c2, i, j, k;
  5. printf("Enter rows and column for first matrix: ");
  6. scanf("%d %d", &r1, &c1);
  7. printf("Enter rows and column for second matrix: ");
  8. scanf("%d %d",&r2, &c2);
  9. // Column of first matrix should be equal to column of second matrix and
  10. while (c1 != r2)
  11. {
  12. printf("Error! column of first matrix not equal to row of second. ");
  13. printf("Enter rows and column for first matrix: ");
  14. scanf("%d %d", &r1, &c1);
  15. printf("Enter rows and column for second matrix: ");
  16. scanf("%d %d",&r2, &c2);
  17. }
  18. // Storing elements of first matrix.
  19. printf(" Enter elements of matrix 1: ");
  20. for(i=0; i<r1; ++i)
  21. for(j=0; j<c1; ++j)
  22. {
  23. printf("Enter elements a%d%d: ",i+1, j+1);
  24. scanf("%d", &a[i][j]);
  25. }
  26. // Storing elements of second matrix.
  27. printf(" Enter elements of matrix 2: ");
  28. for(i=0; i<r2; ++i)
  29. for(j=0; j<c2; ++j)
  30. {
  31. printf("Enter elements b%d%d: ",i+1, j+1);
  32. scanf("%d",&b[i][j]);
  33. }
  34. // Initializing all elements of result matrix to 0
  35. for(i=0; i<r1; ++i)
  36. for(j=0; j<c2; ++j)
  37. {
  38. result[i][j] = 0;
  39. }
  40. // Multiplying matrices a and b and
  41. // storing result in result matrix
  42. for(i=0; i<r1; ++i)
  43. for(j=0; j<c2; ++j)
  44. for(k=0; k<c1; ++k)
  45. {
  46. result[i][j]+=a[i][k]*b[k][j];
  47. }
  48. // Displaying the result
  49. printf(" Output Matrix: ");
  50. for(i=0; i<r1; ++i)
  51. for(j=0; j<c2; ++j)
  52. {
  53. printf("%d ", result[i][j]);
  54. if(j == c2-1)
  55. printf(" ");
  56. }
  57. return 0;
  58. }

Output

Enter rows and column for first matrix: 3
2
Enter rows and column for second matrix: 3
2
Error! column of first matrix not equal to row of second.

Enter rows and column for first matrix: 2
3
Enter rows and column for second matrix: 3
2

Enter elements of matrix 1:
Enter elements a11:2 
Enter elements a12:1 
Enter elements a13:2 
Enter elements a21:3 
Enter elements a22:1 
Enter elements a23:4 

Enter elements of matrix 2:
Enter elements b11:2 
Enter elements b12:-1 
Enter elements b21:2 
Enter elements b22:3 
Enter elements b31:1 
Enter elements b32:1 

Output Matrix:
8  3

12  4

Example{c} Program to Find Transpose of a Matrix

This program takes a matrix of order r*c from the user and computes the transpose of that matrix.

To understand this example, you should have the knowledge of following C programming topics:

In this program, user is asked to entered the number of rows r and columns c. The value of r and cshould be less than 10 in this program.

The user is asked to enter elements of the matrix (of order r*c).

Then, the program computes the transpose of the matrix and displays it on the screen.

Example[d]: Program to Find Transpose of a Matrix

  1. #include
  2. int main()
  3. {
  4. int a[10][10], transpose[10][10], r, c, i, j;
  5. printf("Enter rows and columns of matrix: ");
  6. scanf("%d %d", &r, &c);
  7. // Storing elements of the matrix
  8. printf(" Enter elements of matrix: ");
  9. for(i=0; i<r; ++i)
  10. for(j=0; j<c; ++j)
  11. {
  12. printf("Enter element a%d%d: ",i+1, j+1);
  13. scanf("%d", &a[i][j]);
  14. }
  15. // Displaying the matrix a[][] */
  16. printf(" Entered Matrix: ");
  17. for(i=0; i<r; ++i)
  18. for(j=0; j<c; ++j)
  19. {
  20. printf("%d ", a[i][j]);
  21. if (j == c-1)
  22. printf(" ");
  23. }
  24. // Finding the transpose of matrix a
  25. for(i=0; i<r; ++i)
  26. for(j=0; j<c; ++j)
  27. {
  28. transpose[j][i] = a[i][j];
  29. }
  30. // Displaying the transpose of matrix a
  31. printf(" Transpose of Matrix: ");
  32. for(i=0; i<c; ++i)
  33. for(j=0; j<r; ++j)
  34. {
  35. printf("%d ",transpose[i][j]);
  36. if(j==r-1)
  37. printf(" ");
  38. }
  39. return 0;
  40. }

Output

Enter rows and columns of matrix: 2
3

Enter element of matrix:
Enter element a11: 5
Enter element a12: 6
Enter element a13: 8
Enter element a21: 3
Enter element a22: 9
Enter element a23: 1

Entered Matrix: 
5  6  8 

3  9  1  


Transpose of Matrix:
5  3 

6  9  

8  1  

Example[E] Inverse Matrix of 3x3 in C  

C code for inverse of 3×3matrix

  1. #include
  2.  
  3. int main(){
  4.  
  5. int a[3][3],i,j;
  6. float determinant=0;
  7.  
  8. printf("Enter the 9 elements of matrix: ");
  9. for(i=0;i<3;i++)
  10. for(j=0;j<3;j++)
  11. scanf("%d",&a[i][j]);
  12.  
  13. printf(" The matrix is ");
  14. for(i=0;i<3;i++){
  15. printf(" ");
  16. for(j=0;j<3;j++)
  17. printf("%d ",a[i][j]);
  18. }
  19.  
  20. for(i=0;i<3;i++)
  21. determinant = determinant + (a[0][i]*(a[1][(i+1)%3]*a[2][(i+2)%3] - a[1][(i+2)%3]*a[2][(i+1)%3]));
  22.  
  23. printf(" Inverse of matrix is: ");
  24. for(i=0;i<3;i++){
  25. for(j=0;j<3;j++)
  26. printf("%.2f ",((a[(i+1)%3][(j+1)%3] * a[(i+2)%3][(j+2)%3]) - (a[(i+1)%3][(j+2)%3]*a[(i+2)%3][(j+1)%3]))/ determinant);
  27. printf(" ");
  28. }
  29.  
  30. return 0;
  31. }
  32.  
  33. Type this code on your computer and execute input and output. Best of luck 

Program to Find the Trace & Normal of a given Matrix

This is a program to find the trace & normal of a given matrix.

Problem Description

This C Program find the trace & normal of a given matrix. Here trace of the matrix is the sum of the elements of the main diagonal i.e the diagonal from the upper left to the lower right of a matrix. Normal of the matrix is the square root of the sum of all the elements.

Problem Solution

1. Create a matrix and define all its elements.
2. To evaluate normal of the matrix, take sum of all the elements of the array and calculate the square root of it.
3. To evaluate trace of the matrix, take sum of the main diagonal elements.

Program/Source Code

Here is source code of the C program to find the trace & normal of a given matrix. The program is successfully compiled and tested using Turbo C compiler in windows environment. The program output is also shown below.

  1.     /*
  2.      * C program to find the trace and normal of a matrix
  3.      *
  4.      * Trace is defined as the sum of main diagonal elements and
  5.      * Normal is defined as square root of the sum of all the elements
  6.      */
  7.  
  8.     #include 
  9.     #include 
  10.  
  11.     void main ()
  12.     {
  13.  
  14.         static int array[10][10];
  15.         int i, j, m, n, sum = 0, sum1 = 0, a = 0, normal;
  16.  
  17.         printf("Enter the order of the matrix
    ");
  18.         scanf("%d %d", &m, &n);
  19.  
  20.         printf("Enter the n coefficients of the matrix 
    ");
  21.         for (i = 0; i < m; ++i) 
  22.         {
  23.             for (j = 0; j < n; ++j)
  24.             {
  25.                 scanf("%d", &array[i][j]);
  26.                 a = array[i][j] * array[i][j];
  27.                 sum1 = sum1 + a;
  28.             }
  29.         }
  30.  
  31.         normal = sqrt(sum1);
  32.         printf("The normal of the given matrix is = %d
    ", normal);
  33.         for (i = 0; i < m; ++i) 
  34.         {
  35.             sum = sum + array[i][i];
  36.         }
  37.  
  38.         printf("Trace of the matrix is = %d
    ", sum);
  39.  
  40.     }
Program Explanation

1. Declare a matrix and define all its elements.
2. Declare variables for storing the normal and trace of the matrix.
3. Find the sum of all the elements of the matrix using nested for loop.
4. Evaluate normal by passing the above calculated sum to sqrt() function.
5. Take sum of all the main diagonal elements of the array to calculate trace.
6. Print trace and normmal

Runtime Test Cases

Enter the order of the matrix
3 3
Enter the coefficients of the matrix
3  7 9
2 6 10
8 5 9
The normal of the given matrix is = 21
Trace of the matrix is = 18

6 comments:

C code for if &amp; if else statement with diagram

If statement in C programming with example BY er RAVI BHADANA  |  FILED UNDER:  C-PROGRAMMING When we need to execute a block of statements ...