Sıcaklık Hesaplama

sicaklikhesaplama

Sıcaklık değerlerini tutacak bir double array’i oluşturulur, for döngüsü ile değerler bu array’e eklenir. Sonrasında “AverageHeat”, “MaximumHeat”, “MinimumHeat” metotları aracılığıyla ortalama, en büyük ve en düşük sıcaklıklar bulunur. “Maxday” ve “Minday” string’leri hangi günde en yüksek ve en düşük sıcaklıkların olduğunu göstermek için yapılmıştır. Metotlarda dikkat edilecek bir nokta: Parametre ile “heats” adlı double array’in tüm elemanları metoda geçirilmektedir. Bu sayede array’in tüm elemanlarını kapsayacak hesaplamalar yapılmaktadır.

using System;

internal class Sicaklik_Hesaplama
{
    private static double[] heats;
    private static string Maxday;
    private static string Minday;

    public static double AverageHeat(double[] heaters)
    {
        double top = 0;
        for (int i = 0; i < heaters.Length; i++)
        {
            top += heaters[i];
        }
        top /= heaters.Length;

        return top;
    }

    public static double MaximumHeat(double[] heaters)
    {
        double max = 0;
        for (int i = 0; i < heaters.Length; i++)
        {
            if (heaters[i] > max) max = heaters[i];
        }
        for (int i = 0; i < heaters.Length; i++)
        {
            if (heaters[i] == max)
            {
                Maxday = " [ " + (i+1) + ".Day ]";
            }
        }
        return max;
    }

    public static double MinimumHeat(double[] heaters)
    {
        double min = heaters[0];

        for (int i = 0; i < heaters.Length; i++)
        {
            if (heaters[i] < min) min = heaters[i];
        }

        for (int i = 0; i < heaters.Length; i++)
        {
            if (heaters[i] == min)
            {
                Minday = " [ " + (i + 1) + ".Day ]";
            }

        }

        return min;
    }

    private static void Main()
    {
        heats = new double[7];
        for (int i = 0; i < heats.Length; i++)
        {
            Console.Write("{0}. Day's Temperature [°C]: ", i + 1);

            heats[i] = Convert.ToDouble(Console.ReadLine());
            Console.WriteLine();
        }

        Console.WriteLine("Average Heat : {0:#.##} °C", AverageHeat(heats));
        Console.WriteLine("Maximum Heat : {0:#.##} °C  Happened in {1}", MaximumHeat(heats), Maxday);
        Console.WriteLine("Minimum Heat : {0:#.##} °C  Happened in {1}", MinimumHeat(heats), Minday);
        Console.ReadKey();
    }
    }

Leave a comment