Header Ads

36 - WAP to determine Matrix Multiplication in C

This program determines Matrix Multiplication in C

Program Code:

//to find determine Matrix Multiplication.
#include <stdio.h>
#include <stdlib.h>

//Function to print matrix
void print(int matrix[][10], int row, int col)
{
 int idx, jdx;
 printf("\n Answer:\n");
 for (idx = 0; idx < row; idx++)
 {
  for (jdx = 0; jdx < col; jdx++)
  {
   printf(" %d ", matrix[idx][jdx]);
  }
  printf("\n");
 }
}

//function for multiplying matrices
int multiply(int matrix[][10], int row, int col)
{
 //Declaring variables
 int choice, num, idx, jdx, kdx, row_2, col_2, matrix_2[10][10], ans_matrix[10][10], sum = 0;
 //reading choice of multiplication from user
 printf(" Multiplying Factor : ");
 scanf("%d", &num);
 //loop to multiply all the elements
 for (idx = 0; idx < row; idx++)
  for (jdx = 0; jdx < col; jdx++)
  {
   matrix[idx][jdx] *= num;
  }
 //printing the answer matrix
 print(matrix, row, col);
 return 0;
}

int main()
{
 //declaring variables
 int matrix[10][10], row, col, idx, jdx, Det;
 char ch;
 do
 {
  //reading an action from the user and the order of the matrix
  printf(" =================================");  
  printf(" \n    Multiplication for Matrix \n");
  printf(" =================================\n");
  printf(" Enter the Size for Matrix\n Row's = ");
  scanf("%d", &row);
  printf(" Col's = ");
  scanf("%d", &col);

  //reading the matrix
  for (idx = 0; idx < row; idx++)
  {
   printf(" Enter %d numbers for Row %d : ", col, idx + 1);
   for (jdx = 0; jdx < col; jdx++)
   {
    scanf("%d", &matrix[idx][jdx]);
   }
  }
  multiply(matrix, row, col);
 
  printf("\n To Continue press [Y/n] : ");
  getchar();
 } while ((ch = getchar()) && (ch == 'y' || ch == 'Y'));
 
 return 0; 
}

Program Output:

No comments