Answers for "matric multiplication"

C#
0

matric multiplication

using System;

class MainClass {
  public static void Main (string[] args) {
    var marixOne = new Matrix(2,3,new int[6]{3,2,1,2,0,-1});
    var marixTwo = new Matrix(3,2,new int[6]{3,1,-2,0,1,2});
    var idea = marixOne * marixTwo;
    marixOne.Print();
    marixTwo.Print();
    idea.Print();
  }
}

// Using one dimensional array .
public class Matrix{

  public int row;
  public int column;
  public int[] arrayContainer;

  public Matrix(int numberOfrow , int numberOfcol, int[] containerArray){
    row = numberOfrow;
    column = numberOfcol;
    arrayContainer = new int [row * column];
    for(int i = 0; i < row*column ;i++){arrayContainer[i]= containerArray[i];}
  }



  public static Matrix operator *(Matrix a , Matrix b){

    var ispossible = a.column == b.row;
    if(!ispossible) return null;
    var numberofItemsInArray = a.row * b.column ;
    var resultArray = new int[numberofItemsInArray];
    var count = 0;
    var resultitem = 0;

      for(int currentRow =0 ; currentRow < a.row ; currentRow++)
      {
        for(int currentColoumn = 0 ; currentColoumn < b.column ; currentColoumn++ )
        {
          for(int index = 0 ; index < b.row;index++){

            var row_item = a.arrayContainer[index + a.column * currentRow];
            var coloumn_item = b.arrayContainer[currentColoumn + b.column * index ];
            resultitem += row_item * coloumn_item ;
            /*
            Console.WriteLine($"row_item = {row_item},coloumn_item = {coloumn_item},multiplication = {row_item * coloumn_item } resultitem = {resultitem }");
            */
          }
         
          /*Console.WriteLine("--\n--"); */
          resultArray[count] = resultitem ;
          resultitem = 0;
          count++;
         
        }
      }
      return new Matrix(a.row,b.column,resultArray);
    }


  public void Print(){
    Console.WriteLine("\n");
    for(int currentRow = 0 ; currentRow<row ; currentRow++ ){
      var item = "";
      for(int currentcoloumn = 0  ; currentcoloumn<column ; currentcoloumn++)
      {
        item +=" "+arrayContainer[currentcoloumn+column*currentRow];
      }
      Console.WriteLine(item);
      item = string.Empty;
    }
    Console.WriteLine("\n\n");
  }

}
Posted by: Guest on August-15-2021

C# Answers by Framework

Browse Popular Code Answers by Language