Create an array of 20 doubles Assign values to your array us

Create an array of 20 doubles. Assign values to your array using a for loop, where myDoubles is the name of the array created. Print out the values using another for loop. Now, using your array and another loop, calculate the average of the values in the myDoubles array and print out the average using the statement below. \"The average value of myDoubles is: Once you are done with your program, show it to your TA and submit it on Moodle through the Recitation 8 Submit link by Sunday 5pm.

Solution

Please follow the code and comments for description :

CODE :

import java.util.Scanner; // required imports for the code

public class MyArrayAvg { // class that run the code

public static void main(String[] args) { // driver method
Scanner sc = new Scanner(System.in); // scanner class to get the data from the user

double[] myDoubles = new double[20]; // array of doubles with the required size
System.out.println(\"Please Enter the Values to Add to the Array : \"); // prompt to enter the dat from the user
for (int i = 0; i < 20; i++) { // iterating over the size to get the elements
System.out.println(\"Enter the Element \" + (i + 1) + \" : \");
myDoubles[i] = sc.nextDouble(); // saving the data to array
}

System.out.println(\"\ The Entered Values Are : \");
for (int j = 0; j < 20; j++) { // iterating to print the array data
System.out.println(\"Element \" + (j + 1) + \" : \" + myDoubles[j]); // print to console the data in the array
}

System.out.println(\"\ Calculating the Average of the Array : \"); // calculating the average of the array
double sum = 0, avg = 0; // required initialisations
for (int m = 0; m < 20; m++) { // iterating to get the average
sum = sum + myDoubles[m]; // calculating the sum of the array data
}
avg = sum / 20; // calculating the average
System.out.println(\"The Average Values of myDoubles is : \" + avg); // print the result to console
}
}

OUTPUT :

Please Enter the Values to Add to the Array :
Enter the Element 1 :
1
Enter the Element 2 :
2
Enter the Element 3 :
3
Enter the Element 4 :
4
Enter the Element 5 :
5
Enter the Element 6 :
6
Enter the Element 7 :
7
Enter the Element 8 :
8
Enter the Element 9 :
9
Enter the Element 10 :
10
Enter the Element 11 :
11
Enter the Element 12 :
12
Enter the Element 13 :
13
Enter the Element 14 :
14
Enter the Element 15 :
15
Enter the Element 16 :
16
Enter the Element 17 :
17
Enter the Element 18 :
18
Enter the Element 19 :
19
Enter the Element 20 :
20
The Entered Values Are :
Element 1 : 1.0
Element 2 : 2.0
Element 3 : 3.0
Element 4 : 4.0
Element 5 : 5.0
Element 6 : 6.0
Element 7 : 7.0
Element 8 : 8.0
Element 9 : 9.0
Element 10 : 10.0
Element 11 : 11.0
Element 12 : 12.0
Element 13 : 13.0
Element 14 : 14.0
Element 15 : 15.0
Element 16 : 16.0
Element 17 : 17.0
Element 18 : 18.0
Element 19 : 19.0
Element 20 : 20.0
Calculating the Average of the Array :
The Average Values of myDoubles is : 10.5


Hope this is helpful.

 Create an array of 20 doubles. Assign values to your array using a for loop, where myDoubles is the name of the array created. Print out the values using anoth
 Create an array of 20 doubles. Assign values to your array using a for loop, where myDoubles is the name of the array created. Print out the values using anoth

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site