Ask Question
2 August, 21:59

Given a variable temps that refers to a list, all of whose elements refer to values of type float, representing temperature data, compute the average temperature and assign it to a variable named avg_temp. Besides temps and avg_temp, you may use two other variables - - k and total.

+2
Answers (1)
  1. 2 August, 22:02
    0
    The solution code is written in Java. This is because Java is a type-strict language that enable us to assign a specific data type (e. g. float) to the variable that match the problem scope stated in the question.

    public class Main { public static void main (String[] args) { float temps[] = { 12.1f, 5.4f, 8.9f, 3.2f, 15.5f }; float avg_temp; float total = 0; for (int k = 0; k < temps. length; k++) { total + = temps[k]; } avg_temp = total / temps. length; System. out. println (avg_temp); } }

    Explanation:

    Firstly, declare a temps array with float type and assigns it with a random set of temperature values (Line 5). All the random temperature are float values (decimal numbers).

    Next, declare another two variables avg_temp (Line 7) and total (Line 8) with float type. The avg_temp will hold the average of temperatures from the array whereas the total will hold the total temperatures.

    Prior to calculating the average temperature, we need to get the total of temperature. This is done by creating a for-loop and iterate through each element from the array and add it to the variable total (Line 10 - 13).

    Once we get the total of temperature, divide the variable total with the length of the array temps (which is equivalent to the number of temperature values in the array) and assign it to variable avg_temp (Line 15).

    At last, we can print out the value of avg_temp to verify the output (Line 17).
Know the Answer?
Not Sure About the Answer?
Find an answer to your question 👍 “Given a variable temps that refers to a list, all of whose elements refer to values of type float, representing temperature data, compute ...” in 📗 Computers & Technology if the answers seem to be not correct or there’s no answer. Try a smart search to find answers to similar questions.
Search for Other Answers