Question
Asked Nov 22, 2019
60 views

using an array write a program that will ask the user to enter a temperature for each day of the week and will find and print the average temperature, the low temperature and high temperature In Java 

check_circle

Expert Answer

Step 1

The approach and program to find the highest, lowest and the average temperature of a week by using arrays is given below:

Approach:

  • Importing the essential package and declaring the main function within the main class.
  • Creating the object of scanner class.
  • Declaring and allocating the memory to the t[7] array of the double data type.
  • Declaring the variables s, avg_temp of double datatype to store the sum and average of the temperatures of each day.
  • Using for loop to check the condition repeatedly and storing the temperature of each day entered by the user and calculating the sum and average.
  • Now, declaring the variables low_temp, high_temp to store the lowest and highest temperature stored in the array t[].
  • Inside another for loop, if statements are used to store and check the highest, lowest values of the temperature and storing these values to the above variables.
  • Displaying the highest, lowest and the average value of the temperatures.      
Step 2

Code:

//importing the package

import java.util.*;

//declaring the main class

public class Main

{

    //declaring the main function

    public static void main(String[] args)

    {

            //creating the object of Scanner class

            Scanner input = new Scanner( System.in );

            //declaring the array

            double t [];

            //allocating memory to the array

            t = new double[7];

            //declaring the variables as per the requirement

//initializing the variables to zero      

            double s = 0;

            double avg_temp = 0;

            System.out.println("Please enter the temperature for each day: ");

        //for loop is used to check the condition repeatedely   

        for( int i = 0; i < 7; i++ )

        {

            //prompt user to take input

            System.out.print("Temperature of day " + ( i + 1 ) + ":" );

            t[i] = input.nextDouble();

            //sum of temperature

            s = s + t[i];

            //calculating the average

            avg_temp = s / t.length;

        }

            //calculating the average

            avg_temp = s / t.length;

      &...

Want to see the full answer?

See Solution

Check out a sample Q&A here.

Want to see this answer and more?

Solutions are written by subject experts who are available 24/7. Questions are typically answered within 1 hour.*

See Solution
*Response times may vary by subject and question.
Tagged in

Engineering

Computer Science

Java

Related Computer Science Q&A

Find answers to questions asked by student like you
Show more Q&A
add
question_answer

Q: a. Explain what the CPU should do when an interrupt occurs. Include in your answer the method the CP...

A: Method used by Central Processing Unit (CPU) while an interrupt occursThe CPU first checks and then ...

question_answer

Q: (check all that apply) Find all the employees located in DallasA) select ename from dept where loc =...

A: Checking all the given SQL query to find all the employees located in dallas,Creating table from the...

question_answer

Q: In MS Access, which kind of values will be accepted by the databasethrough input mask&gt;L0L0L0;0;#A...

A: An input mask in MS-Access contain 3 fields.These fields are separated by semi-colonFirst field spec...

question_answer

Q: How do I code this on python heres the problem  Use the Design Recipe to define a function count_val...

A: Programming instructions: Define the function count_value which takes the list and a value as an arg...

question_answer

Q: in c++ Given the following, use a range-based for loop to load the array with user input (include a ...

A: The C++ program to enter and display the content of an integer array by using range-based for loop, ...

question_answer

Q: Find the location that has average salary more than 4000 dollars. Usually each location mayhave seve...

A: SubqueryA subquery is a query within another query. A subquery is a query that is embedded in WHERE ...

question_answer

Q: Explain why web application security is vital. What could happen to an insecure application? Why is ...

A: Web application securityWeb application security testing ensures that the information system is capa...

question_answer

Q: Given the follwing business rules, create an ERD.  The ERD must include all primary keys, foreign ke...

A: The entities, primary keys and foreign keys and attributes of ERD are:

question_answer

Q: List and describe four vulnerability intelligence sources. Which seems the most effective? Why?

A: Sources of Vulnerability IntelligenceSecurity VendorsSoftware VendorsGovernment and public organizat...