This blog is under construction

Saturday 27 July 2013

C program to perform scalar matrix multiplication

Write a C program to perform scalar multiplication of matrices.


  #include <stdio.h>
  #define ROW 10
  #define COL 10

  int main() {
        int matrix[ROW][COL];
        int scalar, i, j, n;

        /* get the order of the matrix */
        printf("Enter the order of the matrix:");
        scanf("%d", &n);

        /* get the entries for input matrix */
        printf("Enter your matrix entries:\n");
        for (i = 0; i < n; i++) {
                for (j = 0; j < n; j++)  {
                        scanf("%d", &matrix[i][j]);
                }
        }

        /* get the scalar value from the user */
        printf("Enter your scalar value:");
        scanf("%d", &scalar);

        /* perform scalar matrix multiplication */
        for (i = 0; i < n; i++) {
                for (j = 0; j < n; j++) {
                        matrix[i][j] = scalar * matrix[i][j];
                }
        }

        /* print the output */
        printf("Resultant Matrix:\n");
        for (i = 0; i < n; i++) {
                for (j = 0; j < n; j++) {
                        printf("%d ", matrix[i][j]);
                }
                printf("\n");
        }

        return 0;
  }



  Output:
  jp@jp-VirtualBox:~/$ ./a.out
  Enter the order of the matrix:3
  Enter your matrix entries:
  1  2  3
  4  5  6
  7  8  9
  Enter your scalar value:10
  Resultant Matrix:
  10 20 30 
  40 50 60 
  70 80 90 


No comments:

Post a Comment