Perform Scalar matrix multiplication with C.
#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
0 Comments