Need help editing code to make it look like expected output /** * Lab 5a * To pr
ID: 3600290 • Letter: N
Question
Need help editing code to make it look like expected output
/**
* Lab 5a
* To pracic various array skills such as initializing, setting individual values, summing, copying, and displaying
* 107-2
* 24-Oct-2017
* @author
*/
import java.util.*;
public class Lab5a {
public static void main(String[] args) {
// System.out.println("Program 5, , 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[2]); //The element 6 is at index 2 in f.
//--------------------------------------------------------------------------------
// 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; //Every element of g is initialized to 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; //Fills the elements of float array c with values 1 to 100.
for(int i = 0; i < 100; i++)
total += c[i]; //Sums the array c elements.
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]; //Creates a 34-element float array b.
for(int i = 0; i < 11; i++) //Copies first 11 elements from c into b.
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
float[] w = new float[99]; //Declares w, a float array of size 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] = (float)rand.nextDouble() * 10000 - 5000 ;
// TODO: Find min and max
for(int i = 0; i < w.length; i++)
{
if(w[i] < min)
min = w[i];
if(w[i] > max)
max = w[i];
}
System.out.println("Smallest value is " + min);
System.out.println("Largest value is " + max);
}
}
Explanation / Answer
Please find my implementation.
/**
* Lab 5a
* To pracic various array skills such as initializing, setting individual values, summing, copying, and displaying
* 107-2
* 24-Oct-2017
* @author
*/
import java.util.*;
public class Lab5a {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter size of first set of double values: ");
int n1 = sc.nextInt();
float[] set1 = new float[n1];
System.out.println("Enter "+n1+" float values: ");
for(int i=0; i<n1; i++)
set1[i] = sc.nextFloat();
System.out.print("Enter size of second set of double values: ");
int n2 = sc.nextInt();
float[] set2 = new float[n2];
System.out.println("Enter "+n2+" float values: ");
for(int i=0; i<n2; i++)
set1[i] = sc.nextFloat();
sc.close();
if(n1 != n2) {
System.out.println("set one and set two are not equal");
}else{
// creating boolean value
boolean[] flag = new boolean[n1];
for(int i=0; i<n1; i++) {
for(int j=0; j<n2; j++) {
if((Math.abs(set1[i] - set2[j]) <= 0.001) && flag[j] != true) {
flag[j] = true; // this element has matched with set 1
break;
}
}
}
// check all the flag values
int i = 0;
while(i<n1) {
if(flag[i] == false) {
break;
}
i++;
}
if(i == n1) {
System.out.println("set one and set two are equal");
}else{
System.out.println("set one and set two are not equal");
}
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.