Given months and temperature (in ºC) data as shown in the code snippet below:
string[] month = { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul",
"Aug", "Sep", "Oct", "Nov", "Dec" };
double[] temperature = { 3.0, 5.6, 9.7, 13.8, 19.0, 24.5, 26.0, 25.0,
21.3, 14.6, 7.6, 2.8 };
a) Determine the temperature classification based on the table given.
Temperature Classification Temperature Range
Freezing < 0 ºC
Very Cold 0 ºC – 5.9 ºC
Cold 6 ºC – 9.9 ºC
Cool 10 ºC – 13.9 ºC
Mild 14 ºC – 17.9 ºC
Moderate 18 ºC – 22.9 ºC
Warm 23 ºC – 26.9 ºC
Very Warm 27 ºC – 29.9 ºC
Hot > 30 ºC
b) Determine the minimum and maximum value from the array of temperature
given. Subsequently, compute the average mark (rounded to 2 decimal
places).
using System;
class Program
{
static void Main(string[] args)
{
string[] month = { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };
double[] temperature = { 3.0, 5.6, 9.7, 13.8, 19.0, 24.5, 26.0, 25.0, 21.3, 14.6, 7.6, 2.8 };
int minindex = 0;
int maxindex = 0;
double total = 0;
double average;
for (int i=0;i<12;i++)
{
if (temperature[i] < 0) { Console.WriteLine(month[i] + " is Freezing"); }
else if (temperature[i] >= 0 && temperature[i] <= 5.9) { Console.WriteLine(month[i] + " is Very Cold"); }
else if (temperature[i] >= 6 && temperature[i] <= 9.9) { Console.WriteLine(month[i] + " is Cold"); }
else if (temperature[i] >= 10 && temperature[i] <= 13.9) { Console.WriteLine(month[i] + " is Cool"); }
else if (temperature[i] >= 14 && temperature[i] <= 17.9) { Console.WriteLine(month[i] + " is Mild"); }
else if (temperature[i] >= 18 && temperature[i] <= 22.9) { Console.WriteLine(month[i] + " is Moderate"); }
else if (temperature[i] >= 23 && temperature[i] <= 26.9) { Console.WriteLine(month[i] + " is Warm"); }
else if (temperature[i] >= 27 && temperature[i] <= 29.9) { Console.WriteLine(month[i] + " is Very Warm"); }
else if (temperature[i] >= 30 ) { Console.WriteLine(month[i] + " is Hot"); }
if (temperature[i] < temperature[minindex])
minindex = i;
if (temperature[i] > temperature[maxindex])
maxindex = i;
total = total + temperature[i];
}
average = Math.Round(total / 12,2);
Console.WriteLine("minimum value in " + month[minindex] + " temperature:" + temperature[minindex]);
Console.WriteLine("maximum value in " + month[maxindex] + " temperature:" + temperature[maxindex]);
Console.WriteLine("average value in " + average);
}
}
Comments
Leave a comment