ZADANIE_7

 avatar
unknown
plain_text
2 years ago
2.1 kB
3
Indexable
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Linq;

namespace zadanie7
{
    internal class Program
    {
        static void Dzielenie(ref double[] inTab, double dzielnik)
        {
            for(int i = 1; i < inTab.Length; i++)
            {
                inTab[i] = inTab[i] / dzielnik;
            }
        }

        static void Main(string[] args)
        {
            /* 
             * Napisz metodę Dzielenie zmieniającą elementy w tablicy typu double 
             * w taki sposób, że każdy element jest dzielony przez wartość pierwszego 
             * elementu. W przypadku, gdy wartość pierwszego elementu 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.
             */
            Console.WriteLine("Podaj rozmiar tablicy");
            int n = Convert.ToInt32(Console.ReadLine());
            Random r = new Random();
            double[] tab = new double[n];     // utworzenie tablicy o podanym rozmiarze

            // wypelnienie tablicy
            for (int i = 0; i < tab.Length; i++)
                tab[i] = r.Next(10);

            Console.WriteLine("TABLICA: ");
            foreach (double x in tab)
                Console.Write(x + " ");

            Console.WriteLine();
            Console.WriteLine();

            double dzielnik = tab[0];
            try
            {
                if (dzielnik == 0.0)
                    throw new DivideByZeroException("Dzielenie przez zero NIEEEE");
                Dzielenie(ref tab, dzielnik);

                Console.WriteLine("TABLICA PO PODZIELENIU ELEMENTOW: ");
                foreach (double x in tab)
                    Console.Write(x + " ");
            }
            catch (DivideByZeroException e)
            {
                Console.WriteLine("BLAD TO: " + e.Message);
            }

            Console.ReadKey();
        }
    }
}
Editor is loading...