Advertisement
ntamas

gauss elimináció

Jun 5th, 2014
223
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.41 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5.  
  6. namespace matrixfeltoltes
  7. {
  8.   class Program
  9.   {
  10.     /*
  11.      * Főátló alatti elemeket kinullázni
  12.     */
  13.     static double determinans(double[,] tomb)
  14.     {
  15.       double szorzat = 1;
  16.       for (int i = 0; i < tomb.GetLength(1); i++)
  17.       {
  18.         szorzat *= tomb[i, i];
  19.       }
  20.       return szorzat;
  21.     }
  22.     static void kinullazas(double[,] tomb)
  23.     {
  24.       double hanyados, tarolt;
  25.       for(int k = 0; k < tomb.GetLength(0)-1; k++)
  26.       {
  27.         for(int i = k+1; i < tomb.GetLength(0); i++)
  28.         {
  29.           tarolt = tomb[i, k];
  30.           for (int j = k; j < tomb.GetLength(1); j++)
  31.           {
  32.             hanyados = tarolt / tomb[k, k];
  33.             tomb[i, j] = tomb[i, j] + tomb[k, j] * -(hanyados);
  34.           }
  35.         }
  36.       }
  37.       /*for (int i = 1; i < tomb.GetLength(0); i++)
  38.       {
  39.         for (int j = 0; j < tomb.GetLength(1); j++)
  40.         {
  41.           tomb[i, j] = tomb[i, j] + tomb[0, j] * -((double)tomb[i, 0] / (double)tomb[0, 0]);
  42.         }
  43.       }
  44.       for (int i = 2; i < tomb.GetLength(0); i++)
  45.       {
  46.         for (int j = 1; j < tomb.GetLength(1); j++)
  47.         {
  48.           double hanyados = (double)tomb[i, 1] / (double)tomb[1, 1];
  49.           tomb[i, j] = tomb[i, j] + tomb[1, j] * -(hanyados);
  50.         }
  51.       }
  52.       for (int i = 3; i < tomb.GetLength(0); i++)
  53.       {
  54.         for (int j = 2; j < tomb.GetLength(1); j++)
  55.         {
  56.           double hanyados = (double)tomb[i, 2] / (double)tomb[2, 2];
  57.           tomb[i, j] = tomb[i, j] + tomb[2, j] * -(hanyados);
  58.         }
  59.       }*/
  60.     }
  61.     static List<int> lista = new List<int>();
  62.     static void abszelemkereses(double[,] tomb, double max, ref int sorindex, ref int oszlopindex)
  63.     {
  64.       for (int i = 0; i < tomb.GetLength(0); i++)
  65.       {
  66.         for (int j = 0; j < tomb.GetLength(1); j++)
  67.         {
  68.           if (max == Math.Abs(tomb[i, j]))
  69.           {
  70.             sorindex = i;
  71.             oszlopindex = j;
  72.             lista.Add(i + 1);
  73.             lista.Add(j + 1);
  74.           }
  75.         }
  76.       }
  77.     }
  78.     static void elemkereses(double[,] tomb, double max, ref int sorindex, ref int oszlopindex)
  79.     {
  80.       for(int i= 0; i < tomb.GetLength(0); i++)
  81.       {
  82.         for (int j = 0; j < tomb.GetLength(1); j++)
  83.         {
  84.           if(max == tomb[i, j])
  85.           {
  86.             sorindex = i;
  87.             oszlopindex = j;
  88.             lista.Add(i+1);
  89.             lista.Add(j+1);
  90.           }
  91.         }
  92.       }
  93.     }
  94.     static double maxabszkereses(double[,] tomb)
  95.     {
  96.       double max = 0;
  97.       for (int i = 0; i < tomb.GetLength(0); i++)
  98.       {
  99.         for (int j = 0; j < tomb.GetLength(1); j++)
  100.         {
  101.           if (max < Math.Abs(tomb[i, j]))
  102.           {
  103.             max = Math.Abs(tomb[i, j]);
  104.           }
  105.         }
  106.       }
  107.       return max;
  108.     }
  109.     static double maxkereses(double[,] tomb)
  110.     {
  111.       double max = 0;
  112.       for(int i = 0; i < tomb.GetLength(0); i++)
  113.       {
  114.         for (int j = 0; j < tomb.GetLength(1); j++)
  115.         {
  116.           if(max < tomb[i, j])
  117.           {
  118.             max = tomb[i, j];
  119.           }
  120.         }
  121.       }
  122.       return max;
  123.     }
  124.     static void feltolt(double[,] tomb, Random vel)
  125.     {
  126.       for(int i = 0; i < tomb.GetLength(0); i++)
  127.       {
  128.         for (int j = 0; j < tomb.GetLength(1); j++)
  129.         {
  130.           tomb[i, j] = vel.Next(-100, 100);
  131.         }
  132.       }
  133.     }
  134.     static void kiir(double[,] tomb)
  135.     {
  136.       for(int i = 0; i < tomb.GetLength(0); i++)
  137.       {
  138.         for(int j = 0; j < tomb.GetLength(1); j++)
  139.         {
  140.           Console.Write("{0,3:00.00} ", tomb[i, j]);
  141.         }
  142.         Console.WriteLine();
  143.       }
  144.     }
  145.     static void Main(string[] args)
  146.     {
  147.       Console.Write("Paraszt írd be a dimnezió számát: ");
  148.       int n = Convert.ToInt16(Console.ReadLine());
  149.       int sorindex=0, oszlopindex=0;
  150.       double[,] tomb = new double[n, n];
  151.       double[,] tomb2 = new double[n, n];
  152.       Random vel = new Random();
  153.       feltolt(tomb, vel);
  154.       kiir(tomb);
  155.       Console.WriteLine("Ez itt a legnagyobb elem: {0}", maxkereses(tomb));
  156.       Console.WriteLine("Ez itt a legnagyobb abszolútértékű elem: {0}", maxabszkereses(tomb));
  157.       elemkereses(tomb, maxkereses(tomb), ref sorindex, ref oszlopindex);
  158.       Console.WriteLine("A maximális elem sorindexe: {0}, oszlopindexe: {1}", sorindex+1, oszlopindex+1);
  159.       Console.WriteLine("A maximális elemek száma: {0}", lista.Count/2);
  160.       for (int i = 0; i < lista.Count; i++)
  161.       {
  162.         Console.Write("{0} ", lista[i]);
  163.         if (i % 2 == 0)
  164.         {
  165.           Console.WriteLine();
  166.         }
  167.       }
  168.       Console.WriteLine();
  169.       lista.Clear();
  170.       abszelemkereses(tomb, maxabszkereses(tomb), ref sorindex, ref oszlopindex);
  171.       Console.WriteLine("A maximális abszolútértékű elem sorindexe: {0}, oszlopindexe: {1}", sorindex + 1, oszlopindex + 1);
  172.       Console.WriteLine("A maximális abszolútértékű elemek száma: {0}", lista.Count / 2);
  173.       for (int i = 0; i < lista.Count; i++)
  174.       {
  175.         Console.Write("{0} ", lista[i]);
  176.         if (i % 2 == 0)
  177.         {
  178.           Console.WriteLine();
  179.         }
  180.       }
  181.       Console.WriteLine();
  182.       kinullazas(tomb);
  183.       kiir(tomb);
  184.       Console.WriteLine("A mátrix determinánsa: {0}", determinans(tomb));
  185.       Console.ReadKey();
  186.     }
  187.   }
  188. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement