Answers for "2d matrix multiplication"

9

how to do matrix multiplication in c

double[][] c = new double[N][N];
for (int i = 0; i < N; i++)
{
 for (int j = 0; j < N; j++)
 	{
 for (int k = 0; k < N; k++)
 		{
		 c[i][j] += a[i][k] * b[k][j];
 		}
 	}
}
Posted by: Guest on April-15-2020
1

matrix multiplication in c

#include <stdio.h>

// function to get matrix elements entered by the user
void getMatrixElements(int matrix[][10], int row, int column) {

   printf("\nEnter elements: \n");

   for (int i = 0; i < row; ++i) {
      for (int j = 0; j < column; ++j) {
         printf("Enter a%d%d: ", i + 1, j + 1);
         scanf("%d", &matrix[i][j]);
      }
   }
}

// function to multiply two matrices
void multiplyMatrices(int first[][10],
                      int second[][10],
                      int result[][10],
                      int r1, int c1, int r2, int c2) {

   // Initializing elements of matrix mult to 0.
   for (int i = 0; i < r1; ++i) {
      for (int j = 0; j < c2; ++j) {
         result[i][j] = 0;
      }
   }

   // Multiplying first and second matrices and storing it in result
   for (int i = 0; i < r1; ++i) {
      for (int j = 0; j < c2; ++j) {
         for (int k = 0; k < c1; ++k) {
            result[i][j] += first[i][k] * second[k][j];
         }
      }
   }
}

// function to display the matrix
void display(int result[][10], int row, int column) {

   printf("\nOutput Matrix:\n");
   for (int i = 0; i < row; ++i) {
      for (int j = 0; j < column; ++j) {
         printf("%d  ", result[i][j]);
         if (j == column - 1)
            printf("\n");
      }
   }
}

int main() {
   int first[10][10], second[10][10], result[10][10], r1, c1, r2, c2;
   printf("Enter rows and column for the first matrix: ");
   scanf("%d %d", &r1, &c1);
   printf("Enter rows and column for the second matrix: ");
   scanf("%d %d", &r2, &c2);

   // Taking input until
   // 1st matrix columns is not equal to 2nd matrix row
   while (c1 != r2) {
      printf("Error! Enter rows and columns again.\n");
      printf("Enter rows and columns for the first matrix: ");
      scanf("%d%d", &r1, &c1);
      printf("Enter rows and columns for the second matrix: ");
      scanf("%d%d", &r2, &c2);
   }

   // get elements of the first matrix
   getMatrixElements(first, r1, c1);

   // get elements of the second matrix
   getMatrixElements(second, r2, c2);

   // multiply two matrices.
   multiplyMatrices(first, second, result, r1, c1, r2, c2);

   // display the result
   display(result, r1, c2);

   return 0;
}
Posted by: Guest on October-18-2020
0

2d matrix multiplication

import java.util.Arrays;
import java.util.Scanner;
public class Matrix {

    private static int counter =0;
    private static Scanner scanner = new Scanner(System.in);
    private int rawForArr1 =0 , columnForArr1=0, rawForArr2=0, ColumnForArr2=0;

    public Matrix(){
    }

    public Matrix(int rawForArr1, int columnForArr1, int rawForArr2, int columnForArr2) {
        this.rawForArr1 = rawForArr1;
        this.columnForArr1 = columnForArr1;
        this.rawForArr2 = rawForArr2;
        ColumnForArr2 = columnForArr2;
    }

    public void sizeOfRawsAndColumns(int r1, int c1, int r2, int c2){
        System.out.println("Enter numbers of raws for array#1");
        r1=scanner.nextInt();
        this.rawForArr1 =r1;
        System.out.println("Enter numbers of columns for array#1");
        c1=scanner.nextInt();
        this.columnForArr1=c1;
        System.out.println("Enter numbers of raws for array#2");
        r2=scanner.nextInt();
        this.rawForArr2 = r2;
        System.out.println("Enter numbers of columns for array#2");
        c2=scanner.nextInt();
        this.ColumnForArr2 = c2;
        checkIfMatrixIsValid(c1,r2,r1,c2);
    }
    public static void printValue(int[][] arr)
    {
                System.out.print(Arrays.deepToString(arr));
    }

    public static void checkIfMatrixIsValid(int c1,int r2,int r1, int c2)
    {
        while(c1 != r2)
        {
            System.out.println("The columns in the left array is not equal to the raw's in the right"+"\n"+
                    "array enter other values"+"\n"+
                    "enter a value and it will be assigned for valid Matrix");
            Scanner scanner = new Scanner(System.in);
            c1 = scanner.nextInt();
            r2 = c1;
        }
        int [][] firstArr = initilizeMatrix(r1,c1);
        int [][] secondArr = initilizeMatrix(r2,c2);
        EnterElements(firstArr);
        EnterElements(secondArr);
        int[][] multplied =multiplieMatrix(firstArr,secondArr);
        printValue(multplied);



    }

    public static int [][] initilizeMatrix(int n , int m)
    {

        int [][] newMatrix = new int[m][n];
        return  newMatrix;

    }

    public static int [][] multiplieMatrix(int [][] leftArr,int[][] rightArr){
        int m = leftArr[0].length;
        int n = rightArr.length;
        int [][] sumOfMultiplie = new int[m][n];
        int i=0,j=0,k=0;
        int sum = 0 ;
        for( i = 0 ; i < leftArr.length ; ++i)
        {
            for(j = 0 ; j < rightArr.length ; ++j)
            {
                for(k = 0 ; k < rightArr[0].length ; ++k)
                {
                    sum+= leftArr[i][k]*rightArr[k][j];
                }
                sumOfMultiplie[i][j]=sum ;
                sum=0;
            }
        }
        return sumOfMultiplie ;
    }

    public static int[][] EnterElements(int[][] arr)
    {

        ++counter;
        if(counter==1){
            System.out.println("Enter the elements for the left array"+'\n');
        }else{
            System.out.println("Enter the elements for the right array");
        }

        for(int i = 0 ; i < arr.length ; ++i)
        {
            for(int j = 0 ; j<arr.length ; ++j)
            {
                System.out.println("Enter element in raw #"+(i+1)+" column #"+(j+1));
                arr[i][j] = scanner.nextInt();
            }
        }
        return arr ;

    }



}
Posted by: Guest on July-23-2021

Code answers related to "2d matrix multiplication"

Code answers related to "Java"

Java Answers by Framework

Browse Popular Code Answers by Language