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
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());
}
}
}
}
Comments
Leave a comment