Untitled
unknown
plain_text
3 years ago
2.0 kB
9
Indexable
class Program
{
static void Unormowanie(double [] t1, double suma)
{
suma = 0;
for (int i = 0; i < t1.Length; i++)
{
suma += t1[i];
}
Console.WriteLine(suma);
for (int i = 0; i < t1.Length; i++)
{
t1[i] = t1[i] / suma;
}
}
static void Main(string[] args)
{ /* Napisz metodę typu void Unormowanie zmieniającą elementy w tablicy typu double w taki sposób,
* że każdy element jest dzielony przez sumę wszystkich elementów. W przypadku,
* gdy suma jest równa 0 należy zgłosić wyjątek. W metodzie Main należy zastosować blok
* try catch w celu obsługi wyjątku. */
//utworzenie tablicy o n rozmiarze
Console.WriteLine("Podaj rozmiar tablicy");
int n = Convert.ToInt32(Console.ReadLine());
Random r = new Random();
double[] t1 = new double[n];
//wypelnienie tablicy
for (int i = 0; i < t1.Length; i++)
t1[i] = r.Next(10);
//wyświetlenie tablicy
Console.WriteLine("TABLICA: ");
foreach (double x in t1)
Console.Write(x + " ");
double suma = 0;
for (int i = 0; i < t1.Length; i++)
{
suma += t1[i];
}
try
{
if (suma == 0)
throw new DivideByZeroException("Dzielenie przez zero");
Unormowanie (t1, suma);
Console.WriteLine("TABLICA PO PODZIELENIU ELEMENTOW: ");
foreach (double x in t1)
Console.Write(x + " ");
}
catch (DivideByZeroException e)
{
Console.WriteLine("BLAD TO: " + e.Message);
}
}
}
}
Editor is loading...