Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- namespace Macierze
- {
- class Program
- {
- static void Main(string[] args)
- {
- int[,] tabA = new int[3, 3] { { 1, 3, 2 }, { -2, 0, 1 }, { 5, -3, 2 } };
- int[,] tabB = new int[2, 3] { { 2, 1, 3 }, { -1, 4, 0 } };
- int[,] result = Multiply(tabA, tabB);
- Draw(result);
- Console.ReadKey();
- }
- public static int[,] Multiply (int[,] tabA, int[,] tabB)
- {
- if (tabA.GetLength(1) == tabB.GetLength(1))
- {
- int[,] result;
- int row = new int();
- bool aFirst = true;
- if (tabA.Length > tabB.Length)
- {
- result = new int[tabB.GetLength(0), tabA.GetLength(1)];
- aFirst = false;
- }
- else
- {
- result = new int[tabA.GetLength(0), tabB.GetLength(1)];
- aFirst = false;
- }
- for (int i = 0; i < tabB.GetLength(0); i++)
- {
- for (int j = 0; j < tabA.GetLength(0); j++)
- {
- for (int k = 0; k < tabB.GetLength(1); k++)
- {
- try
- {
- if (aFirst)
- row += tabA[i, k] * tabB[k, j];
- else
- row += tabB[i, k] * tabA[k, j];
- }
- catch (IndexOutOfRangeException ex)
- {
- Console.WriteLine(ex.Message + " (k - " + k + " j - " + j + " i - " + i + ") aFirst = " + aFirst);
- }
- }
- result[i, j] = row;
- row = 0;
- }
- }
- return result;
- }
- else
- return new int[0,0];
- }
- public static void Draw(int[,] macierz)
- {
- for (int i = 0; i < macierz.GetLength(0); i++)
- {
- for (int j = 0; j < macierz.GetLength(1); j++)
- {
- Console.Write(macierz[i, j] + " ");
- }
- Console.WriteLine();
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement