Hi, I would really appreciate the help for this lab. I don't understand what I did wrong; for example, I don't understand the program errors displayed.
5.13 Lab 5b
Objectives
In this lab you will practice using for loops with arrays and comparing double values, using a threshold, to see if two sets of numbers are the same.
Background Reading
ZyBooks Chapter 5
Task
The first comment of your main method should be your identification comment:
//Lab5b, [Name], [mascID]
You will need to then:
Create and populate the two sets of double values, in which the number of elements and the elements themselves are specified by the user. (i.e. the user will state how many numbers are in the first set, give the values for that set and then move on to the second set of numbers).
Iterate through the sets and compare the values. For the purposes of this lab, we will say that the order matters in determining if the sets are the same. (HINT: This also means that the lengths of the arrays must be the same).
If the arrays are the same in both length and values, print set one and set two are equal. Otherwise, print set one and set two are not equal
Comparing double values using a threshold
Recall that floating-point numbers may differ in a very small value. For example, 1.0 might actually be stored as 0.9999999... or 1.0000000002.
When comparing these values, we often use a "tolerance" level that says, "if the difference is smaller than some amount, then it is negligible and the numbers can be thought of as the same." We can achieve this by comparing the absolute value of the difference of the elements to the tolerance. For example,
if ( Math.abs( array1[0] - array2[0] ) > 0.001 ) )
System.out.println("The values of array1[0] and array2[0] are not equal");
Code:
import java.lang.Math;
import java.util.Scanner;
public class SetComparison {
public static void main (String [] args) {
Scanner scnr = new Scanner(System.in);
int array1Elems = 0;
int array2Elems = 0;
int i = 0;
int j = 0;
array1Elems = scnr.nextInt();
array2Elems = scnr.nextInt();
double array1[] = new double[array1Elems];
double array2[] = new double[array2Elems];
for (i = 0; i < array1Elems; ++i) {
array1[i] = scnr.nextInt;
for (j = 0; j < array2Elems; ++j) {
array2[j] = scnr.nextInt;
}
if (Math.abs(array1[i] - array2[j]) > 0.001) {
System.out.println("set one and set two are not equal");
}
else if (Math.abs(array1[i] - array2[j]) < 0.001) {
System.out.println("set one and set two are equal");
}
}
}
}
Enter program input
6 45.24 54.67 42.55 44.61 65.29 49.75 6 45.24 54.67 42.55 44.61 65.29 49.75
Program errors displayed here SetComparison.java:23: error: cannot find symbol array1[i] = scnr.nextInt; ^ symbol: variable nextInt location: variable scnr of type Scanner SetComparison.java:25: error: cannot find symbol array2[j] = scnr.nextInt; ^ symbol: variable nextInt location: variable scnr of type Scanner 2 errors