Need help solving this question the task and within the code has instructions! p
ID: 3814962 • Letter: N
Question
Need help solving this question the task and within the code has instructions!
public class Lab5a {
public static void main(String[] args) {
// System.out.println("Program 5, Your Name, mascxxxx"); // Replace with your name and mascID
//--------------------------------------------------------------------------------
//1. Display the value of element 6 of array f
//--------------------------------------------------------------------------------
int[] f = {2, 4, 6, 8, 10, 12, 14, 16, 18, 20};
//TODO: Print element 6 of array f
System.out.println();
//--------------------------------------------------------------------------------
// 2. Initialize each element of g to 8
//--------------------------------------------------------------------------------
int[] g = new int[7];
for (int i=0; i < g.length; ++i)
// TODO: Fill array g
for (int i=0; i < g.length; ++i)
System.out.println("g[" + i + "] is " + g[i]);
System.out.println();
//--------------------------------------------------------------------------------
// 3. Total 100 elements of floating point array c filled with values 1 to 100
//--------------------------------------------------------------------------------
float[] c = new float[100];
double total = 0.0;
//TODO: Fill and sum array c
System.out.println("Total is " + total);
System.out.println("Average is " + (total / c.length));
System.out.println();
//--------------------------------------------------------------------------------
// 4. Copy 11-element array c into the first 11 positions of 34-element array b.
//--------------------------------------------------------------------------------
// TODO: create array b and copy c's elements into it
for (int i=0; i < b.length; ++i)
System.out.print(b[i] + " ");
System.out.println();
System.out.println();
//--------------------------------------------------------------------------------
// 5. Determine and print the largest and smallest values contained in 99-element
// floating-point array w
//--------------------------------------------------------------------------------
Random rand = new Random(1025L);
//TODO: Declare array w with 99 elements
double min = 5001;
double max = -5001;
// Filling array w with random numbers between -5000 and +5000
for (int i=0; i < w.length; ++i)
w[i] = rand.nextDouble() * 10000 - 5000 ;
// TODO: Find min and max
System.out.println("Smallest value is " + min);
System.out.println("Largest value is " + max);
}
}
Explanation / Answer
Please let me know in case of any issue.
import java.util.Random;
import java.util.Random;
public class Lab5a {
public static void main(String[] args) {
// System.out.println("Program 5, Your Name, mascxxxx"); // Replace with your name and mascID
//--------------------------------------------------------------------------------
//1. Display the value of element 6 of array f
//--------------------------------------------------------------------------------
int[] f = {2, 4, 6, 8, 10, 12, 14, 16, 18, 20};
//TODO: Print element 6 of array f
System.out.println(f[5]);
System.out.println();
//--------------------------------------------------------------------------------
// 2. Initialize each element of g to 8
//--------------------------------------------------------------------------------
int[] g = new int[7];
for (int i=0; i < g.length; ++i)
// TODO: Fill array g
g[i] = 8;
for (int i=0; i < g.length; ++i)
System.out.println("g[" + i + "] is " + g[i]);
System.out.println();
//--------------------------------------------------------------------------------
// 3. Total 100 elements of floating point array c filled with values 1 to 100
//--------------------------------------------------------------------------------
float[] c = new float[100];
double total = 0.0;
//TODO: Fill and sum array c
for(int i=0; i<100; i++){
c[i] = (i+1);
total = total + c[i];
}
System.out.println("Total is " + total);
System.out.println("Average is " + (total / c.length));
System.out.println();
//--------------------------------------------------------------------------------
// 4. Copy 11-element array c into the first 11 positions of 34-element array b.
//--------------------------------------------------------------------------------
// TODO: create array b and copy c's elements into it
float[] b = new float[34];
for(int i=0; i<11; i++)
b[i] = c[i];
for (int i=0; i < b.length; ++i)
System.out.print(b[i] + " ");
System.out.println();
System.out.println();
//--------------------------------------------------------------------------------
// 5. Determine and print the largest and smallest values contained in 99-element
// floating-point array w
//--------------------------------------------------------------------------------
Random rand = new Random(1025L);
//TODO: Declare array w with 99 elements
double []w = new double[99];
double min = 5001;
double max = -5001;
// Filling array w with random numbers between -5000 and +5000
for (int i=0; i < w.length; ++i)
w[i] = rand.nextDouble() * 10000 - 5000 ;
// TODO: Find min and max
for(int i=0; i<w.length; i++){
if(min > w[i])
min = w[i];
if(max < w[i])
max = w[i];
}
System.out.println("Smallest value is " + min);
System.out.println("Largest value is " + max);
}
}
/*
Sample run:
12
g[0] is 8
g[1] is 8
g[2] is 8
g[3] is 8
g[4] is 8
g[5] is 8
g[6] is 8
Total is 5050.0
Average is 50.5
1.0 2.0 3.0 4.0 5.0 6.0 7.0 8.0 9.0 10.0 11.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0
Smallest value is -4986.383134600046
Largest value is 4996.1201953975815
*/