ProgaOra/20230918/ConsoleApp1/Program.cs

201 lines
5.3 KiB
C#
Raw Normal View History

2023-12-04 09:51:29 +00:00
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApp1
{
class Program
{
public static class GlobalData
{
public static int[] szamok = new int[100];
public static int[] kisebb250 = { };
}
static void Feladat1()
{
Random random = new Random();
for (int i = 0; i < GlobalData.szamok.Length; i++)
{
GlobalData.szamok[i] = random.Next(100, 500);
}
}
static void Feladat2()
{
int sum = 0;
foreach (int n in GlobalData.szamok)
{
sum += n;
}
Console.WriteLine(sum);
}
static void Feladat3()
{
bool van100 = false;
foreach (int n in GlobalData.szamok)
{
if (n == 100)
{
van100 = true;
break;
}
}
if (van100)
{
Console.WriteLine("Van 100 a listában.");
} else
{
Console.WriteLine("Nincs 100 a listában.");
}
}
static void Feladat4()
{
int max = GlobalData.szamok[0];
foreach (int n in GlobalData.szamok)
{
if (n > max)
{
max = n;
}
}
Console.WriteLine($"A legnagyobb szám: {max}");
}
static void Feladat5()
{
int min = GlobalData.szamok[0];
foreach (int n in GlobalData.szamok)
{
if (n < min)
{
min = n;
}
}
Console.WriteLine($"A legkisebb szám: {min}");
}
static void Feladat6()
{
foreach (int n in GlobalData.szamok)
{
if (n < 250)
{
GlobalData.kisebb250 = GlobalData.kisebb250.Append(n).ToArray();
}
}
}
static void Feladat7(int keresendo)
{
int index = 0;
for (int i = 0; i < GlobalData.kisebb250.Length; i++)
{
if(GlobalData.kisebb250[i] == keresendo)
{
index = i;
break;
}
index = -1;
}
if (index == -1)
{
Console.WriteLine("Nincs ilyen elem.");
} else
{
Console.WriteLine($"Az elem indexe: {index}");
}
}
static void Feladat8()
{
for (int j = 0; j < GlobalData.kisebb250.Length; j++)
{
for (int g = 0; g < GlobalData.kisebb250.Length; g++)
{
if (GlobalData.kisebb250[j] < GlobalData.kisebb250[g])
{
int seged = GlobalData.kisebb250[j];
GlobalData.kisebb250[j] = GlobalData.kisebb250[g];
GlobalData.kisebb250[g] = seged;
}
}
}
foreach (int n in GlobalData.kisebb250)
{
Console.WriteLine(n);
}
}
static void Feladat9()
{
int lngt = GlobalData.szamok.Length;
for (int j = 0; j < lngt; j++)
{
for (int g = 0; g < (lngt - 1); g++)
{
if (GlobalData.szamok[g] > GlobalData.szamok[g + 1])
{
int seged = GlobalData.szamok[g + 1];
GlobalData.szamok[g + 1] = GlobalData.szamok[g];
GlobalData.szamok[g] = seged;
}
}
}
foreach (int n in GlobalData.szamok)
{
Console.WriteLine(n);
}
}
static void Feladat10(int keresendo)
{
Console.WriteLine(Array.BinarySearch(GlobalData.szamok, keresendo));
}
static void Feladat11(int keresendo)
{
Console.WriteLine(Array.BinarySearch(GlobalData.szamok, keresendo));
int i = GlobalData.szamok.Length / 2;
int megold = -1;
int ciklus = 2;
while (megold == -1)
{
if (GlobalData.szamok[i] > keresendo)
{
int num = Convert.ToInt32(Math.Pow(2, ciklus));
i += GlobalData.szamok.Length / (num);
} else
{
int num = Convert.ToInt32(Math.Pow(2, ciklus));
i -= GlobalData.szamok.Length / (num);
}
}
}
static void Main(string[] args)
{
Feladat1();
Feladat2();
Feladat3();
Feladat4();
Feladat5();
Feladat6();
Feladat7(150);
Feladat8();
Feladat9();
Feladat10(150);
Feladat11(150);
Console.ReadLine();
}
}
}