Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- namespace ConsoleApplication1
- {
- class Program
- {
- static bool isprime(int szam)
- {
- bool prim;
- bool kilep = false;
- if (szam == 0 || szam == 1)
- {
- prim = false;
- }
- else if (szam == 2)
- {
- prim = true;
- }
- else
- {
- prim = true;
- for (int i = 2; i <= Math.Sqrt(szam) && !kilep; i++)
- {
- if (szam % i == 0)
- {
- prim = false;
- kilep = true;
- }
- }
- }
- return prim;
- }
- static uint primekszama(ref int tombhatar1, ref int tombhatar2)
- {
- uint count = 0;
- for (int i = tombhatar1; i <= tombhatar2; i++)
- {
- if (isprime(i))
- {
- count++;
- }
- }
- return count;
- }
- static int nedikprim(int kezdoertek, int db)
- {
- uint szamlal = 0;
- int i = kezdoertek;
- do
- {
- if(isprime(i))
- {
- szamlal++;
- }
- i++;
- }while(szamlal < db);
- return i - 1;
- }
- static void tombfeltolt(ref int[,] tomb, Random vel, ref int tombhatar1, ref int tombhatar2)
- {
- int generalt = 0;
- bool talal = false;
- int i;
- if(primekszama(ref tombhatar1, ref tombhatar2) < tomb.Length)
- {
- Console.WriteLine("Az intervallumon generálható prímek száma kevesebb, mint a mátrix mérete.");
- Console.WriteLine("Legalább {0} db prímnek kell lennie és a tömbhatár {1} és {2} közt kell legyen!", tomb.Length, tombhatar1, nedikprim(tombhatar1, tomb.Length));
- }
- else
- {
- do
- {
- generalt = vel.Next(tombhatar1, tombhatar2+1);
- } while (!isprime(generalt));
- tomb[0, 0] = generalt;
- for (int j = 0; j < tomb.GetLength(0); j++)
- {
- if (j == 0)
- {
- i = 1;
- }
- else
- {
- i = 0;
- }
- for (; i < tomb.GetLength(1); i++)
- {
- do
- {
- talal = false;
- do
- {
- generalt = vel.Next(tombhatar1, tombhatar2 + 1);
- }while (!isprime(generalt));
- for (int k = 0; k < tomb.GetLength(0); k++)
- {
- for (int l = 0; l < tomb.GetLength(1); l++)
- {
- if (generalt == tomb[k, l])
- {
- talal = true;
- break;
- }
- }
- }
- } while (talal);
- tomb[j, i] = generalt;
- }
- }
- }
- }
- static void kiirat(ref int[,] tomb)
- {
- for (int j = 0; j < tomb.GetLength(0); j++)
- {
- for (int i = 0; i < tomb.GetLength(1); i++)
- {
- Console.Write("{0, 4} ", tomb[j, i]);
- }
- Console.WriteLine();
- }
- }
- static void primekdb(ref int[,] tomb, ref uint primdb)
- {
- primdb = 0;
- for (int j = 0; j < tomb.GetLength(0); j++)
- {
- for (int i = 0; i < tomb.GetLength(1); i++)
- {
- if (isprime(tomb[j, i]) == true)
- {
- primdb++;
- }
- }
- }
- }
- static void paritas(ref int[,] tomb, ref uint paros, ref uint paratlan)
- {
- paros = 0;
- paratlan = 0;
- for (int j = 0; j < tomb.GetLength(0); j++)
- {
- for (int i = 0; i < tomb.GetLength(1); i++)
- {
- if (tomb[j, i] % 2 == 0)
- {
- paros++;
- }
- else
- {
- paratlan++;
- }
- }
- }
- }
- static void kereses(ref int[,] tomb, ref int adott, ref uint sor, ref uint oszlop)
- {
- sor = 0;
- oszlop = 0;
- for (int j = 0; j < tomb.GetLength(0); j++)
- {
- for (int i = 0; i < tomb.GetLength(1); i++)
- {
- if (tomb[j, i] == adott)
- {
- sor = Convert.ToUInt32(j + 1);
- oszlop = Convert.ToUInt32(i + 1);
- }
- }
- }
- if (sor == 0 && oszlop == 0)
- {
- Console.WriteLine("Az elem nem található a tömbben.");
- }
- else
- {
- Console.WriteLine("A begépelt elem sorindexe: {0}, oszlopindexe: {1}", sor, oszlop);
- }
- }
- static void attoltes(ref int[,] tomb, ref int[] tomb2, ref bool beki)
- {
- uint k = 0;
- for(int j = 0; j < tomb.GetLength(0); j++)
- {
- for (int i = 0; i < tomb.GetLength(1); i++ )
- {
- if (beki == true)
- {
- tomb2[k++] = tomb[j, i];
- }
- else
- {
- tomb[j, i] = tomb2[k++];
- }
- }
- }
- }
- static void rendezes(ref int[,] tomb, ref int[] tomb2)
- {
- bool beki = true;
- attoltes(ref tomb, ref tomb2, ref beki);
- int tarolo;
- uint minindex = 0;
- for (uint i = 0; i < tomb2.Length - 1; i++)
- {
- minindex = i;
- for (uint j = i + 1; j < tomb2.Length; j++)
- {
- if (tomb2[minindex] > tomb2[j])
- {
- minindex = j;
- }
- }
- if (minindex != i)
- {
- tarolo = tomb2[minindex];
- tomb2[minindex] = tomb2[i];
- tomb2[i] = tarolo;
- }
- }
- beki = false;
- attoltes(ref tomb, ref tomb2, ref beki);
- }
- static void Main(string[] args)
- {
- Console.Write("Írja be a tömb sorainak számát: ");
- uint sorszam = Convert.ToUInt32(Console.ReadLine());
- Console.Write("Írja be a tömb oszlopainak számát: ");
- uint oszlopszam = Convert.ToUInt32(Console.ReadLine());
- uint paros = 0, paratlan = 0, primdb = 0, sor = 0, oszlop = 0;
- int[,] tomb = new int[sorszam, oszlopszam];
- int[] tomb2 = new int[tomb.Length];
- Random vel = new Random();
- Console.Write("Írja be a tömb alsó határát: ");
- int tombhatar1 = Convert.ToInt32(Console.ReadLine());
- Console.Write("Írja be a tömb felső határát: ");
- int tombhatar2 = Convert.ToInt32(Console.ReadLine());
- tombfeltolt(ref tomb, vel, ref tombhatar1, ref tombhatar2);
- kiirat(ref tomb);
- Console.WriteLine();
- paritas(ref tomb, ref paros, ref paratlan);
- primekdb(ref tomb, ref primdb);
- Console.WriteLine("Párosak darabszáma: {0}\nPáratlanok darabszáma: {1}", paros, paratlan);
- Console.WriteLine("Prímek darabszáma: {0}", primdb);
- Console.Write("Adja meg a keresendő számot: ");
- int adott = Convert.ToInt32(Console.ReadLine());
- kereses(ref tomb, ref adott, ref sor, ref oszlop);
- rendezes(ref tomb, ref tomb2);
- kiirat(ref tomb);
- Console.ReadKey();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement