Need help creating code/ editing for this assignment if you look at the picture
ID: 3816035 • Letter: N
Question
Need help creating code/ editing for this assignment if you look at the picture and within the code will have tasks and will show what to do. Here is the code that I created.
//lab5b ,name, cssc
import java.util.*;
import java.lang.*;
import java.io.*;
public class SetComparison
{
public static void main(String[] args)
{
double[] arr1 =new double[10];
double[] arr2 =new double[10];
int a,b;
Scanner con = new Scanner(System.in);
System.out.println("Enter the size of Array 1");
do{
a = con.nextInt();
}while(con.hasNextInt());
System.out.println("Enter the size of Array 2");
do{
b = con.nextInt();
}while(con.hasNextInt());
if (a == b)
{
System.out.println("Enter the values of Array 1");
for (int i=0;i<a;i++)
arr1[i]=con.nextDouble();
System.out.println("Enter the values of Array 2");
for (int i=0;i<b;i++)
arr2[i]=con.nextDouble();
for (int i=0;i<a;i++)
{
if((Math.abs(arr1[i]-arr2[i])>0.001))
System.out.println("the values are not same ");
else
System.out.println("the values are same ");
}
}
else
System.out.println("The Array sizes are not same ");
}
}
Explanation / Answer
//lab5b ,name, cssc
import java.util.*;
import java.lang.*;
import java.io.*;
public class SetComparison
{
public static void main(String[] args)
{
int count = 0; //variable to manipulate while comparing arrays
Scanner con = new Scanner(System.in);
int array1size = 0;
int array2size = 0;
System.out.println("Enter the size of Array 1");
array1size = con.nextInt();
double[] arr1 =new double[array1size];
System.out.println("Enter the size of Array 2");
array2size = con.nextInt();
double[] arr2 =new double[array2size];
if (array1size == array2size)
{
System.out.println("Enter the values of Array 1");
for (int i=0;i<array1size;i++)
arr1[i]=con.nextDouble();
System.out.println("Enter the values of Array 2");
for (int i=0;i<array2size;i++)
arr2[i]=con.nextDouble();
for (int i=0;i<array2size;i++)
{
if((Math.abs(arr1[i]-arr2[i])>0.001)){
count++;
System.out.println("The values of arr1[" + i + "] and arr2[" + i + "] are not equal");
break;
}
}
if(count == 0){
System.out.println("set one and two are equal");
}else{
System.out.println("set one and two are not equal");
}
}
else
System.out.println("The Array sizes are not same ");
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.