Answer to Question #209000 in C# for Zee

Question #209000

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.

For this program implement the following methods before implementing the main method (which must make use of these methods):


static int getValidTemperature (int dayNum)

static int getWarmest(int temp1, int temp2)

static void readIntegers (int[] list)

static int getWarmestTemperature(int[] list)

static double getAvgTemperature(int[] list)

static int countWarmDays(int[] list)

static void displayList(int[] list)


1
Expert's answer
2021-06-20T18:14:04-0400
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;


namespace Q209000
{
    class Program
    {
        static void Main(string[] args)
        {


            int[] list = new int[10];
            readIntegers(list);


            int warmestTemperature = getWarmestTemperature(list);
            double averageTemperature = getAvgTemperature(list);
            int warmDaysCounter = countWarmDays(list);
            displayList(list);
            Console.WriteLine("The warmest temperature: {0}", warmestTemperature);
            Console.WriteLine("The average temperature: {0}", averageTemperature);
            Console.WriteLine("The number of days that the temperature was higher than 30: {0}", warmDaysCounter);
            
            Console.ReadLine();
        }


        static int getValidTemperature(int dayNum)
        {
            //Only temperatures higher than 0 and less than 45 are valid
            int validTemperature=-1;
            while (validTemperature < 0 || validTemperature > 45) {
                Console.Write("Enter temperature for day {0} [0-45]: ", (dayNum+1));
                if(int.TryParse(Console.ReadLine(),out validTemperature)==false){
                    validTemperature = -1;
                }       
            }
            return validTemperature;
        }
        static int getWarmest(int temp1, int temp2)
        {
            if (temp1 > temp2)
            {
                return temp1;
            }
            return temp2;
        }
        static void readIntegers(int[] list)
        {
            for (int i = 0; i < 10; i++)
            {
                list[i] = getValidTemperature(i);
            }
        }
        static int getWarmestTemperature(int[] list)
        {
            int warmestTemperature = list[0];
            for (int i = 1; i < 10; i++)
            {
                warmestTemperature=getWarmest(list[i], warmestTemperature);
            }
            return warmestTemperature;
        }
        static double getAvgTemperature(int[] list)
        {
            int temperatureSum = 0;
            for (int i = 0; i < 10; i++)
            {
                temperatureSum += list[i];
            }
            return temperatureSum / 10.0;
        }
        static int countWarmDays(int[] list)
        {
            //The number of days that the temperature was higher than 30.
            int counter = 0;
            for (int i = 0; i < 10; i++)
            {
                if (list[i] > 30) {
                    counter++;    
                }
            }
            return counter;
        }
        static void displayList(int[] list)
        {
            for (int i = 0; i < 10; i++)
            {
                Console.WriteLine("The temperature {0} is {1}",(i+1), list[i]);
            }
        }
    }
}




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