Untitled
unknown
plain_text
3 years ago
2.1 kB
9
Indexable
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace nwm
{
class Program
{
static void Unormowanie(ref double[] t1)
{
double suma = 0;
for (int i = 0; i < t1.Length; i++)
{
suma += t1[i];
}
Console.WriteLine(suma);
if(suma == 0.0)
{
throw new DivideByZeroException("Dzielenie przez zero");
} else
{
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 + " ");
try
{
Unormowanie(ref t1);
Console.WriteLine("TABLICA PO PODZIELENIU ELEMENTOW: ");
foreach (double x in t1)
Console.Write(x + " ");
}
catch (DivideByZeroException e)
{
Console.WriteLine("BLAD TO: " + e.Message);
}
Console.ReadKey();
}
}
}
Editor is loading...