Answer:
The solution code is written in Java.
- import java.util.Scanner;
-
- public class Main {
-
- public static void main(String[] args) {
- double myArray [] = new double[5];
-
- Scanner reader = new Scanner(System.in);
-
- for(int i=0; i < myArray.length; i++){
- System.out.print("Enter temperature (Fahrenheit): ");
- myArray[i] = reader.nextDouble();
- }
-
- convert2Cels(myArray, 5);
-
- for(int j = 0; j < myArray.length; j++){
- System.out.format("%.2f \\", myArray[j]);
- }
-
- }
-
- public static void convert2Cels(double [] tempArray, int n){
- for(int i = 0; i < n; i++){
- tempArray[i] = (tempArray[i] - 32) * 5 / 9;
-
- }
- }
- }
Step-by-step explanation:
Firstly, let's create a double-type array with 5 elements, myArray (Line 6).
Next, we create a Java Scanner object to read user input for the temperature (8).
Using a for-loop that repeats iteration for 5 times, prompt user to input temperature in Fahrenheit unit and assign it as the value of current element of the array. (Line 11 - 12). Please note the nextDouble() method is used to read user input as the data type of input temperature is expect in decimal format.
At this stage, we are ready to create the required function convert2Cels() that takes two input arguments, the temperature array, tempArray and number of array elements, n (Line 23). Using a for-loop to traverse each element of the tempArray and apply the formula to convert the fahrenheit to celcius.
Next, we call the function convert2Cels() in the main program (Line 15) and print out the temperature (in celsius) in one single column (Line 17-19). The %.2f in the print statement is to display the temperature value with 2 decimal places.