Open In App

Program for scalar multiplication of a matrix

Last Updated : 19 Dec, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

Given a 2D matrix mat[][] with n rows and m columns and a scalar element k, the task is to find out the scalar product of the given matrix.

Examples: 

Input:
mat[][] = [[2, 3],
[5, 4]]
k = 5
Output: [[10, 15],
[25, 20]]

Input:
mat[][] = [[1, 2, 3],
[4, 5, 6],
[7, 8, 9]]
k = 4
Output: [[4, 8, 12],
[16, 20, 24],
[28, 32, 36]]

Approach:

The idea involves iterating through each element of the matrix and multiplying it by the scalar k, we need to update each matrix element mat[i][j] as follows: mat[i][j] = mat[i][j] × k.

C++
// C++ program to find the scalar product of a matrix
#include <iostream>
#include <vector>
using namespace std;

void scalarProductMat(vector<vector<int>>& mat, int k) {
  
    // number of rows
    int n = mat.size();      
  
    // number of columns
    int m = mat[0].size();   

    // Multiply each element by the scalar k
    for (int i = 0; i < n; i++)
        for (int j = 0; j < m; j++)
            mat[i][j] *= k;
}

int main() {
  
    vector<vector<int>> mat = { { 1, 2, 3 },
                                { 4, 5, 6 },
                                { 7, 8, 9 } };
    int k = 5;

    scalarProductMat(mat, k);
    for (const auto& row : mat) {
        for (int val : row)
            cout << val << " ";
        cout << endl;
    }

    return 0;
}
Java
// Java program to find the scalar product of a matrix
import java.util.Arrays;

class GfG {
  
    static void scalarProductMat(int[][] mat, int k) {
      
        // number of rows
        int n = mat.length;      
        
        // number of columns
        int m = mat[0].length;   

        // Multiply each element by the scalar k
        for (int i = 0; i < n; i++)
            for (int j = 0; j < m; j++)
                mat[i][j] *= k;
    }

    public static void main(String[] args) {
      
        int[][] mat = { { 1, 2, 3 },
                         { 4, 5, 6 },
                         { 7, 8, 9 } };
        int k = 5;

        scalarProductMat(mat, k);
        for (int[] row : mat) {
            for (int val : row)
                System.out.print(val + " ");
            System.out.println();
        }
    }
}
Python
# Python program to find the scalar 
# product of a matrix

def scalarProductMat(mat, k):
  
    # number of rows
    n = len(mat)     
    
    # number of columns
    m = len(mat[0])   

    # Multiply each element by the scalar k
    for i in range(n):
        for j in range(m):
            mat[i][j] *= k

mat = [ [ 1, 2, 3 ],
         [ 4, 5, 6 ],
         [ 7, 8, 9 ] ]
k = 5

scalarProductMat(mat, k)

for row in mat:
    print(' '.join(map(str, row)))
C#
// C# program to find the scalar 
// product of a matrix
using System;
using System.Linq;

class GfG {

    static void ScalarProductMat(int[][] mat, int k) {

        // number of rows
        int n = mat.Length;

        // number of columns
        int m = mat[0].Length;

        // Multiply each element by the scalar k
        for (int i = 0; i < n; i++) {

            for (int j = 0; j < m; j++) {

                mat[i][j] *= k;
            }
        }
    }

    static void Main() {

        int[][] mat = new int[][] {

            new int[] { 1, 2, 3 }, new int[] { 4, 5, 6 },
            new int[] { 7, 8, 9 }
        };
        int k = 5;

        ScalarProductMat(mat, k);

        foreach(var row in mat) {
            Console.WriteLine(string.Join(" ", row));
        }
    }
}
JavaScript
// Javascript program to find the scalar product 
// of a matrix

function scalarProductMat(mat, k) {

    // number of rows
    const n = mat.length;

    // number of columns
    const m = mat[0].length;

    // Multiply each element by the scalar k
    for (let i = 0; i < n; i++) {
        for (let j = 0; j < m; j++) {
            mat[i][j] *= k;
        }
    }
}

//driver code
const mat = [ [ 1, 2, 3 ], [ 4, 5, 6 ], [ 7, 8, 9 ] ];
const k = 5;
scalarProductMat(mat, k);
mat.forEach(row => { console.log(row.join(" ")); });

Output
5 10 15 
20 25 30 
35 40 45 

Time Complexity: O(n x m)
Auxiliary Space: O(1), since no extra space has been taken.



Next Article
Article Tags :
Practice Tags :

Similar Reads