Computer Applications

Declare a single dimensional array of size 28 to store daily temperatures for the month of February. Using this structure, write a program to find:

  1. The hottest day of the month
  2. The coldest day of the month
  3. The average temperature of the month

Java

Java Arrays

20 Likes

Answer

import java.util.Scanner;

public class KboatFebTemp
{
    public static void main(String args[]) {
        Scanner in = new Scanner(System.in);
        double febTemp[] = new double[28];
        int n = febTemp.length;
        
        System.out.println("Enter Feb daily temperatures:");
        for (int i = 0; i < n; i++) {
            febTemp[i] = in.nextDouble();
        }
        
        double sum = 0.0;
        int low = 0, high = 0;
        for (int i = 0; i < n; i++) {
            if (febTemp[i] < febTemp[low])
                low = i;
            
            if (febTemp[i] > febTemp[high])
                high = i;
                
            sum += febTemp[i];
        }
        
        double avg = sum / n;
        
        System.out.println("Hottest day = " + (high + 1));
        System.out.println("Coldest day = " + (low + 1));
        System.out.println("Average Temperature = " + avg);
    }
}

Output

BlueJ output of Declare a single dimensional array of size 28 to store daily temperatures for the month of February. Using this structure, write a program to find: (a) The hottest day of the month (b) The coldest day of the month (c) The average temperature of the monthBlueJ output of Declare a single dimensional array of size 28 to store daily temperatures for the month of February. Using this structure, write a program to find: (a) The hottest day of the month (b) The coldest day of the month (c) The average temperature of the month

Answered By

5 Likes


Related Questions