Matriks Dinamis

Matriks

Matrik

Disini saya akan mencoba membuat matrik yang lebih dinamis kelanjutan dari matriks sebelumnya, disini saya banyak menekankan pada proses perulangan dan Array.

Demo Program

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace perkalianMatriks
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Perkalian matriks A dan Matriks B");
            int barisA, kolomA, barisB, kolomB;
            string input;
            Random randomInt = new Random();

            Console.Write("Baris : ");
            barisA = int.Parse(Console.ReadLine());
            Console.Write("Kolomn Baris B =");
            kolomA = barisB = int.Parse(Console.ReadLine());
            Console.Write("Kolom : ");
            kolomB = int.Parse(Console.ReadLine());
            int[,] matA = new int[barisA, kolomA];
            int[,] matB = new int[barisB, kolomB];
            int[,] matC = new int[barisA, kolomB];
            for (int i = 0; i < barisA; i++)
                for (int j = 0; j < kolomA; j++)
                    matA[i, j] = randomInt.Next(1, 3);

            for (int i = 0; i < barisB; i++)
                for (int j = 0; j < kolomB; j++)
                    matB[i, j] = randomInt.Next(1, 3);

            Console.Write("Matriks\n\n");
            {
                for (int i = 0; i < barisA; i++)
                {
                    for (int j = 0; j < kolomA; j++)
                        Console.Write(matA[i, j] + "\t");
                    Console.WriteLine();
                }
            }

            Console.Write("\niks B :\n");
            {
                for (int i = 0; i < barisB; i++)
                {
                    for (int j = 0; j < kolomB; j++)
                        Console.Write(matB[i, j] + "\t");
                    Console.WriteLine();
                }
            }

            Console.WriteLine("\n Hasil perkalian matriks 2D A dan B :");
            {
                for (int a = 0; a < barisA; a++)
                {
                    for (int b = 0; b < kolomB; b++)
                    {
                        matC[a, b] = 0;
                        for (int c = 0; c < kolomA; c++)
                        {
                            matC[a, b] += matA[a, c] * matB[c, b];
                        }
                        Console.Write(matC[a, b] + "\t");
                    }
                    Console.WriteLine();
                }
                Console.WriteLine("by radika");
                Console.ReadLine();
            }
            Console.ReadKey();
        }
    }
}

Tidak ada komentar:

Posting Komentar