Answer to Question #340805 in C# for Siyanda

Question #340805

Write a program that reads in 10 midday temperatures for Port Elizabeth, for 10 consecutive days.


Only temperatures higher than 0 and less than 45 are valid (working with integer values for


temperatures). It must calculate and display the following:


• The warmest temperature


• The average temperature


• The number of days that the temperature was higher than 30


• The list of temperatures entered


1
Expert's answer
2022-05-14T16:39:38-0400
internal class Program
    {
        class PortElizabeth
        {
            public List<int> temperature { get; set; }


            public PortElizabeth()
            {
                temperature = new List<int>();
            }
            int NumberOfDaysWithHighTemperature()
            {
                int n = 0;
                for (int i = 0; i < temperature.Count; i++)
                {
                    if (temperature[i] > 30)
                        n++;
                }
                return n;
            }
            public override string ToString()
            {
                string t = "";
                for (int i = 0; i < temperature.Count; i++)
                {
                    t += $"Day: {i + 1}, Temperature: {temperature[i]} \n";
                }
                return $"The warmest temperature: {temperature.Max()}\n" +
                    $"The average temperature: {temperature.Sum()/temperature.Count}\n" +
                    $"The number of days that the temperature was higher than 30: {NumberOfDaysWithHighTemperature()}\n" +
                    $"{t}";
            }
        }


        static void Main(string[] args)
        {
            PortElizabeth portElizabeth = new PortElizabeth();
            EnterTemperature(portElizabeth);
            Console.WriteLine();
            Console.WriteLine(portElizabeth);
            Console.ReadKey();
        }
        static void EnterTemperature(PortElizabeth portElizabeth)
        {


            for (int i = 0; i < 10; i++)
            {
                Console.Write($"Enter the temperature of {i + 1} day: ");
                int temperature = int.Parse(Console.ReadLine());


                while (true)
                {
                    if (temperature > 0 && temperature < 45)
                    {
                        portElizabeth.temperature.Add(temperature);
                        break;
                    }
                    Console.Write($"Enter the temperature of {i + 1} day: ");
                    temperature = int.Parse(Console.ReadLine());
                }
            }
        }
    }

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS