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.
Display the temperatures in the array e.g All the temperatures in the array are: 22 26 17 19 20 39 44 37 30 16
using System;
using System.Linq;
namespace PortElizabeth
{
public class Program
{
public static void PrintLine()
{
Console.WriteLine(new string('-', 30));
}
public static void Main()
{
var temperatures = new int[10];
var counter = 0;
var temp30count=0;
Console.WriteLine("Please, enter 10 midday temperatures for Port Elizabeth:");
while (counter < 10)
{
int input = Convert.ToInt32(Console.ReadLine());
if(input>0 && input<45)
{
Console.WriteLine($"Number accepted!");
PrintLine();
temperatures[counter]=input;
counter++;
if(input>30)
temp30count++;
continue;
}
Console.WriteLine("Error! Please, enter a valid temprature(higher than 0 and less than 45)");
}
Console.WriteLine("All temperatures recieved.");
PrintLine();
Console.WriteLine($"The warmest temperature: {temperatures.Max()}");
Console.WriteLine($"The average temperature: {temperatures.Average()}");
Console.WriteLine($"The number of days that the temperature was higher than 30: {temp30count}");
PrintLine();
Console.Write("All the temperatures in the array are:");
foreach(var temp in temperatures)
Console.Write($" {temp} ");
}
}
}
Comments
Leave a comment