Ask Question
23 October, 00:46

Iven an array temps of double s, containing temperature data, compute the average temperature. Store the average in a variable called avgTemp. Besides temps and avgTemp, you may use only two other variables - - an int variable k and a double variable named total, which have been declared.

k = 0;

total = 0;

while (k < temps. length)

{

total = total + temps[k];

k++;

}

avgTemp = total / (temps. length);

+5
Answers (1)
  1. 23 October, 02:58
    0
    import java. util. Arrays;

    import java. util. Scanner;

    public class num1 {

    public static void main (String[] args) {

    Scanner in = new Scanner (System. in);

    System. out. println ("Enter length of the array:");

    int len = in. nextInt ();

    double [] temps = new double[len];

    double avgTem;

    int k = 0;

    double total = 0;

    for (k=0; k
    System. out. println ("Enter values for the array");

    temps[k]=in. nextDouble ();

    }

    System. out. println ("The Arrays contains the following values");

    System. out. println (Arrays. toString (temps));

    / / Computing the average of the values

    for (k=0; k
    total = total+temps[k];

    }

    avgTem = total / (temps. length);

    System. out. println ("The average Temperature is: "+avgTem);

    }

    }

    Explanation:

    Using Java programming language Import the Scanner class to receive user input Prompt User for the length of the Array, receive and store in a variable len; Declare a new double array of size len double [] temps = new double[len]; Using a for loop, continually prompt user to enter values into the array Display the values of the array using Java's Arrays. toString method Use another for loop to add up all the elements in the arraay and store in the variable called total Outside the second for loop calculate the average avgTem = total / (temps. length); Display the average temp.
Know the Answer?
Not Sure About the Answer?
Get an answer to your question ✅ “Iven an array temps of double s, containing temperature data, compute the average temperature. Store the average in a variable called ...” in 📙 Computers & Technology if there is no answer or all answers are wrong, use a search bar and try to find the answer among similar questions.
Search for Other Answers