Given an array temps of double s, containing temperature data, compute the avera
ID: 3622539 • Letter: G
Question
Given 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.Suppose the the temps array had just four elements: 5.4, 8.0, 2.0, and 4.6.
Then the value of avgTemp that you would compute would be 5.0, because that is the average of the above four numbers.
please help!!
Explanation / Answer
I'll go through a pseudo-code explanation first: because you want an average of the temperatures, you need to add up all the temperatures in the array, and then divide by the number of temperatures. Now how will you be able to store this information? You have the variables int k and double total for this. Now, in order to get the information out of the array, you need to loop through each cell in the array, incrementing the number of temperatures, and adding the read temperature to the total. The full pseudo-code would look like this: Initialize k to 0, total to 0. For each cell i in the array: Increment k. Add the value temps[i] to total. Get the average-- avgTemp = total/k. END. In Java, the algorithm looks more like this (this code gives you a function): double getAvgTemp(double temps[]) { int k = 0; double total = 0; double avgTemp = 0; for (int i = 0; iRelated Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.