Advertisement
Guest User

Untitled

a guest
Apr 2nd, 2017
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.79 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7.  
  8.  
  9. namespace ConsoleApplication4
  10. {
  11.     class Program
  12.     {
  13.         public static void modulus(int[,] matrix, int n,int m)
  14.         {
  15.             for(int i=0;i<n;i++)
  16.             {
  17.                 for(int j=0;j<m;j++)
  18.                 {
  19.                     if (matrix[i, j] % 2 == 0) matrix[i, j] = 0;
  20.                     else matrix[i, j] = 1;
  21.                 }
  22.             }
  23.         }
  24.         public static void beir(int[,] matrix,int n,int m) //mátrix elemeinek megadása
  25.         {
  26.             for(int i = 0; i < n; i++)
  27.             {
  28.                 for(int j = 0; j < m; j++)
  29.                 {
  30.                     Console.Write("Adja meg a matrix {0},{1}. elemet: ", i + 1, j + 1);matrix[i, j] = Convert.ToInt32(Console.ReadLine());
  31.                 }
  32.                 Console.WriteLine();
  33.             }
  34.         }
  35.  
  36.         public static void beir_rnd(int[,] matrix, int n, int m) //pszeudo randomszám generátor
  37.         {
  38.             Random rnd = new Random();
  39.             for (int i = 0; i < n; i++)
  40.             {
  41.                 for (int j = 0; j < m; j++)
  42.                 {
  43.                    
  44.                     matrix[i, j] = rnd.Next(0, 100);
  45.                 }
  46.                 Console.WriteLine();
  47.             }
  48.         }
  49.  
  50.         public static void kiir(int[,] matrix, int n, int m) //mátrix kiíratása
  51.         {
  52.             for (int i = 0; i < n; i++)
  53.             {
  54.                 for (int j = 0; j < m; j++)
  55.                 {
  56.                     Console.Write("{0} \t",matrix[i,j]);
  57.                 }
  58.                 Console.WriteLine("\n");
  59.             }
  60.         }
  61.         public static void szorzas(int[,] matrix,int[,] matrix1,int n,int m)
  62.         {
  63.             int[,] matrix2 = new int[100, 100];
  64.             for(int i=0;i<n;i++)
  65.             {
  66.                
  67.                 for(int j=0;j<m;j++)
  68.                 {
  69.                    
  70.                     for (int k=0;k<m;k++)
  71.                     {
  72.                        
  73.                         matrix2[i, j] += matrix[i, k] * matrix[k, j];
  74.                     }
  75.                 }
  76.                
  77.             }
  78.             //modulus(matrix2, n, m);
  79.             for(int i=0;i<n;i++)
  80.             {
  81.                 for(int j=0;j<m;j++)
  82.                 {
  83.                     Console.Write("{0} \t",matrix2[i, j]);
  84.                 }
  85.                 Console.WriteLine("\n");
  86.             }
  87.         }
  88.  
  89.  
  90.         static void Main(string[] args)
  91.         {
  92.  
  93.             int[,] matrix = new int[100, 100];
  94.             int[,] matrix1 = new int[100, 100];
  95.             int sorszam, oszlopszam, sorszam1, oszlopszam1;
  96.             byte uj = 1;
  97.  
  98.             do
  99.             {
  100.  
  101.  
  102.  
  103.                 do //sor és oszlopszám megadása amig nem NxK, KxM alakban vannak
  104.                 {
  105.                     Console.WriteLine("Adja meg az elso matrix sorszamat: ");
  106.                     sorszam = Convert.ToInt32(Console.ReadLine());
  107.                     Console.WriteLine("Adja meg az elso matrix oszlopszamat: ");
  108.                     oszlopszam = Convert.ToInt32(Console.ReadLine());
  109.                     Console.WriteLine("Adja meg a masodik matrix sorszamat: ");
  110.                     sorszam1 = Convert.ToInt32(Console.ReadLine());
  111.                     Console.WriteLine("Adja meg a masodik matrix oszlopszamat: ");
  112.                     oszlopszam1 = Convert.ToInt32(Console.ReadLine());
  113.                     if (oszlopszam != sorszam1) Console.WriteLine("Ilyen alaku matrixokat nem lehet szorozni!");
  114.                 } while (oszlopszam != sorszam1);
  115.  
  116.                 Console.WriteLine("elemek megadásának módja: ");
  117.                 Console.WriteLine("1 - kézi/2 - randomszám generátor");
  118.                 byte mod = Convert.ToByte(Console.ReadLine());
  119.                 switch (mod)
  120.                 {
  121.                     case 1:
  122.                         beir(matrix, sorszam, oszlopszam);
  123.                         beir(matrix1, sorszam1, oszlopszam1);
  124.                         break;
  125.                     case 2:
  126.                         beir_rnd(matrix, sorszam, oszlopszam);
  127.                         beir_rnd(matrix1, sorszam1, oszlopszam1);
  128.                         break;
  129.                 }
  130.  
  131.  
  132.                 Console.WriteLine("Elso matrix: ");
  133.                 kiir(matrix, sorszam, oszlopszam);
  134.                 Console.WriteLine("Masodik matrix: ");
  135.                 kiir(matrix1, sorszam1, oszlopszam1);
  136.                 Console.WriteLine("Ket matrix szorzata: ");
  137.                 szorzas(matrix, matrix1, sorszam, oszlopszam1);
  138.  
  139.                 Console.WriteLine("Uj matrix: 1/Kilepes: 0");
  140.                 uj = Convert.ToByte(Console.ReadLine());
  141.             } while (uj == 1);  
  142.         }
  143.     }
  144. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement