Show two ways to declare a one-dimensional array of 12 doubles.{1.0, 2.0, 3.0, 4
ID: 3692410 • Letter: S
Question
Show two ways to declare a one-dimensional array of 12 doubles.{1.0, 2.0, 3.0, 4.0, 5.0, 6.0,7.0, 8.0, 9.0, 10.0,11.0,12.0}; For the first example print the 7.0 and 10.0 in a println. Example - Your println should match 7.0 10.0 For the first example print the 2.0 and 12.0 in a println. Example - Your println should match 2.0 2.0 Attach a 2 different snipping photos. The Snipping photos should include the programmer source code and the outputs. 2. Write a program that uses an array to find the Average of 10 double values. Use Math. random() in combination with a for loop to fill the array with 10 random values. Then use a different for loop to iterate through the array when summing the total for all 10 double values. Attach Snipping photos of source code and output.Explanation / Answer
ArrayTwoWays.java
package org.students;
public class ArrayTwoWays {
public static void main(String[] args)
{
/* First way of creating a one-dimensional array */
/* declaration of an array with allocation of array of size 12 */
double array[]=new double[12];
/* Storing the values into an array */
array[0]=1.0;
array[1]=2.0;
array[2]=3.0;
array[3]=4.0;
array[4]=5.0;
array[5]=6.0;
array[6]=7.0;
array[7]=8.0;
array[8]=9.0;
array[9]=10.0;
array[10]=11.0;
array[11]=12.0;
/* Displaying the values */
System.out.println(array[6]+" "+array[9]);
System.out.println(" ");
/* Second way of creating a one-dimensional array */
/*Arrays can be initialized when it is declared itself */
double array2[]={1.0,2.0,3.0,4.0,5.0,6.0,7.0,8.0,9.0,10.0,11.0,12.0};
/* Displaying the values */
System.out.println(array[1]+" "+array[11]);
}
}
__________________________________________________________________________________________
output:
7.0
10.0
2.0
12.0
______________________________________________________________________________________
2)
Averageof10D.java
package org.students;
public class Averageof10D {
public static void main(String[] args) {
/* Declaring the variables */
double sum=0.0,average=0.0;
/* Declaring an array and assign size */
double array[]=new double[10];
/* Filling the array with 10 random values between 1 and 100 */
for(int i=0;i<10;i++)
{
/* Generating random values between 1 and 100 in double */
array[i]=Math.random()*100;
}
/* This loop will generate the sum of those array values */
for(int i=0;i<10;i++)
{
sum+=array[i];
}
/* Displaying The sum of the 10 random numbers in the array */
System.out.println("The sum of the 10 random numbers in the array is :"+sum);
/* Calculating the Average for the Values in the array */
average=sum/10;
/* Displaying The Average of the 10 random numbers in the array */
System.out.println("The Average of the 10 random numbers in the array is :"+average);
}
}
__________________________________________________________________________________________
output:
The sum of the 10 random numbers in the array is :546.9883532673852
The Average of the 10 random numbers in the array is :54.69883532673852
__________________________________________________________________________________________
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.