Matrix Scalar Multiplication in C Language


Program Description:

Write a Program to perform the Matrix Scalar Multiplication in C programming language. The program should accept a Matrix and a scaler value from the user and multiply the all elements of the matrix with the provided scaler number.

Here is the example input and output of the program.

Example Input and Output:



Here is the Pictorial representation of the above Scaler and Matrix Multiplication Operation.


If you observe the above output, Each element in the input array has been multiplied by the scaler number ( 100).


It is recommended to know the basics of the Arrays and 2D Arrays and How to pass 2D arrays to function in order to better understand the following program

Matrix Scalar Multiplication in C Program Explanation:

Let’s look at the step-by-step instructions of the Matrix Scaler Multiplication program in C language.

  1. Create two integer constants called ROWS and COLUMNS with Value 100, The constant ROWS holds the max row size of the matrix and similarly, the COLUMNS constant holds the max size of the columns of the Matrix. ( Change this number if you want to use the large arrays)
  2. Declare two matrices X[ROWS][COLUMNS], Z[ROWS][COLUMNS] with the max rows and columns. Also, declare the num variable.
    • The Matrix-X is the input matrix and The num is the scaler number. Finally the Matrix-Z stores the results of the Matrix and Scaler Multiplication – ( num * X)
  3. Prompt the user to provide the desired Row size and Column Size, and store them in the variables rows and columns respectively.
  4. Check for array bounds using the (rows >= ROWS || rows <= 0 || columns >= COLUMNS || columns <= 0) condition, And display an error message if the row/column sizeis beyond the present limits.
  5. Take the user input for Matrix-X, We need to create two For Loops, One For Loop will iterate over the rows, and the Second For Loop will iterate over the columns
  6. Create the First For Loop ( Outer Loop), Start the iteration from i=0 and go till the i<rows (i.e for(i=0; i<rows; i++) ). At Each iteration of the Outer Loop,
    • Create the second For Loop ( Inner Loop). This loop is a Nested loop that will be inside the Outer Loop. Start the Loop from j=0 and go till the j<columns (i.e for(j=0; j<columns; j++) ). The Inner Loop will iterate over the columns. At each iteration of the Inner Loop
      • Prompt the user to provide the Matrix or 2D Array element and Read the element using the scanf function and Update the values[i][j] element.
      • Repeat the above step for all elements in the 2D array or matrix
  7. Prompt the user to provide the value for the Scaler and store it in num variable.
  8. To Calculate the Scaler and Matrix Multiplication, We again need to use two nested For loops to iterate over the matrix elements.
  9. Create Outer For Loop ( iterates over rows) – for(i=0; i<rows; i++)
    • Create Inner For loop (iterates over columns) – for(j=0; j<columns; j++)
      • Multiply the scaler num with the array element X[i][j] and store the result in Z[i][j]
      • which is equal to Z[i][j] = num * X[i][j];
  10. Once the above step-9 is finished, The Matrix-Z contains the Resultant Matrix ( i.e Scaler and Matrix Multiplication)
  11. Display the results using by going through the Matrix-Z elements using another two for loops.
  12. Stop the program.

Program to perform Matrix Scalar Multiplication in C Language:

let’s write the C program to perform the Matrix and scaler multiplication.

Program Output:

Compile and Run the Program using your favorite compiler. We are using GCC compiler.

$ gcc scaler-matrix-mul.c

Run the Program.

Test 1: Provide the Matrix elements and scaler value to the program.


Let’s try another example.

As you can see from the above outputs, The program is generating the proper results.

Test 2: Check the error message.

Provide the invalid sizes ( i.e size &gt; 100)

As excepted, The Program is displaying the error message ( Invalid Rows or Columns size, Please Try again) whenever the size is out of the limits( 1-100).

Matrix Scalar Multiplication using Functions in C Language:

In the above program, We have used a single function( main()) to calculate the matrix scaler multiplication. So let’s divide the above program into the sub-functions.

Here is the modified version of the above Matrix Scaler Multiplication program with User-Defined functions.

We have defined three user-defined functions in the above program (except the main() function), The functions are

  1. The readMatrix() function – Used to read the elements from the user and update the given matrix
    • Prototype of readMatrix function – void readMatrix(int rows, int columns, int X[rows][columns])
    • The readMatrix() function takes three formal arguments which are rows, columns, and Matrix X.
    • This function uses two For loops to iterate over the given matrix A and updates its elements by using the scanf function – scanf(“%d”, &X[i][j]);
  2. The displayMatrix() function is used to print the Matrix elements on the Standard Output.
    • The prototype of displayMatrix function is void displayMatrix(int rows, int columns, int X[rows][columns])
    • Similar to the readMatrix() function, The displayMatrix() function also takes three formal arguments. i.e rows, columns, and X[][] (2D Array)
    • The displayMatrix() function uses Two For loops to iterate over the X matrix and print the elements of the matrix. – printf("%d ", X[i][j]);
  3. The scalerMul() function – This function multiples the scaler( num) with Matrix pointed by X and Store the result in Matrix Z
    • Prototype – void scalerMul(int rows, int columns, int num, int X[rows][columns], int Z[rows][columns])
    • As you can see from the above prototype, This function takes Five Formal Arguments which are rows, columns, num, matrix X, and matrix Z.
    • The scalerMul() function uses two for loops to iterate over the elements of the Matrices X and Mutliplies the scaler num with the all elements of the Matrix-X and stores the results in the Matrix-ZZ[i][j] = num * X[i][j];

Program Output:

Let’s compile and Run the program.


Let’s try a few more examples.

The program is generating the excepted results.

Related Array Programs:


Hi Guys, I am Venkatesh. I am a programmer and an Open Source enthusiast. I write about programming and technology on this blog.

You may also like...

2 Responses

  1. […] Matrix and Scaler Multiplication Program in C […]

  2. […] Matrix and Scaler Multiplication Program in C […]

Leave a Reply