C Program to Perform Scalar Matrix Multiplication

How to write a C Program to Perform Scalar Matrix Multiplication?. Or, How to write a C program to perform Scalar Multiplication on Multi-Dimensional Array with an example.

C Program to Perform Scalar Matrix Multiplication Example 1

C Program to Perform Scalar Matrix Multiplication

This program allows the user to enter the number of rows and columns of a Matrix. Next, this C program to perform Scalar Multiplication on this matrix using For Loop.

/* C Program to Perform Scalar Matrix Multiplication */

#include<stdio.h>
 
int main()
{
 	int i, j, rows, columns, Multiplication[10][10], Number;
  
 	printf("\n Please Enter Number of rows and columns\n");
 	scanf("%d %d", &i, &j);
 
 	printf("\n Please Enter the Matrix Elements \n");
 	for(rows = 0; rows < i; rows++)
  	{
   		for(columns = 0;columns < j;columns++)
    	{
      		scanf("%d", &Multiplication[rows][columns]);
    	}
  	}
   
 	printf("\n Please Enter the Multiplication Value  :  ");
 	scanf("%d", &Number);
 	  
 	for(rows = 0; rows < i; rows++)
  	{
   		for(columns = 0; columns < j; columns++)
    	{
      		Multiplication[rows][columns] = Number * Multiplication[rows][columns];    
   	 	}
  	}
 
 	printf("\n The Result of a Scalar Matrix Multiplication is : \n");
 	for(rows = 0; rows < i; rows++)
  	{
   		for(columns = 0; columns < j; columns++)
    	{
      		printf("%d \t ", Multiplication[rows][columns]);
    	}
    	printf("\n");
  	}
 	return 0;
}
C Program to Perform Scalar Matrix Multiplication Example 2

In this C Program to Perform Scalar Matrix Multiplication example, We declared single Two-dimensional arrays Multiplication of size of 10 * 10.

Below statements asks the User to enter the Multiplication Matrix size (Number of rows and columns. For instance 2 Rows, 3 Columns = Multiplication[2][3] )

printf("\n Please Enter Number of rows and columns  :  ");
scanf("%d %d", &i, &j);

Next, we used for loop to iterate each cell present in Multiplication[3][3] matrix. Conditions inside the for loops ((rows < i) and (columns < j)) will ensure the C Programming compiler, not to exceed the matrix limit. Otherwise, the Matrix will overflow

scanf statement inside the for loop will store the user entered values in every individual array element such as Multiplication[0][0], Multiplication[0][1], …..

for(rows = 0; rows < i; rows++).
{
  for(columns = 0; columns < j; columns++)
   {
     scanf("%d", &a[rows][columns]);
   }
}

Next, we asked the user to enter the Multiplication Number and saved it in the Number variable.

In the next program line, We have one more for loop to perform Scalar Multiplication.

for(rows = 0; rows < i; rows++)
  	{
   		for(columns = 0; columns < j; columns++)
    	{
      		Multiplication[rows][columns] = Number * Multiplication[rows][columns];    
   	 	}
  	}

User inserted values for Program to Perform Scalar Matrix Multiplication are: Multiplication[3][3] = {{10, 20, 30}, { 40, 50, 60}, {70, 80, 90}}

Row First Iteration: for(rows = 0; rows < 3; 0++)
The condition (0 < 3) is True. So, it will enter into second for loop

Column First Iteration: for(columns = 0; 0 < 3; 0++)
The condition (columns < 3) is True. So, it will start executing the statements inside the loop

Multiplication [rows][columns] =  Number * Multiplication[rows][columns]
Multiplication [0][0] =   3 *  Multiplication[0][0] => 3 * 10 = 30

Column Second Iteration: for(columns = 1; 1 < 3; 1++)
The condition (1 < 3) is True.

Multiplication [0][1]= 3 * Multiplication[0][1]
Multiplication[0][1]= 3 * 20 = 60

Column Second Iteration: for(columns = 2; 2 < 3; 2++)
The condition (2 < 3) is True.

Multiplication [0][2] = 3 *  Multiplication[0][2]
Multiplication [0][2]= 3 * 30 = 90

j value will increment. After the increment, the condition inside the second for loop (columns < 3) will fail. So it will exit from the loop.

Next, rows value incremented (rows will become 1) and starts the second-row iteration.

Please follow the same steps where rows = 1, and next rows = 2

At last, we used another for loop to print the Multiplication Matrix