0

Hi I need help with writing code! So I have to write java code that 1. tells if two arrays are equal or not (same values in the same index position) 2. If they are not the same then we want to know how many values are differing and the largest difference between two values.

Here are the two arrays:

double[] Array1 = {10.2, 2.7, 6.4};  
double[] Array2 = {5.7, 4.0, 2.7}; 

This is what I've done and tried so far it might be wrong so please tell me the right solution and how to continue solving the problem:

for (i=0; i<Array1.length; i++) {
  for( i=0; j<Array2.length; j++) {

    if (Array1 [i] == Array2 [j]) {
      

      System.out.println ("Arrays are equal");

    }
    else {
      if (Array1 [i] != Array2 [j] ) {
      System.out.println ("Arrays are not equal" );
DYZ
  • 55,249
  • 10
  • 64
  • 93
Si Hu
  • 9
  • 3
  • Generally speaking, we only fix bugs here. If you code has no problems we don't "fix" it. "Please tell me if this is working" doesn't fly. Run and test your own code. If you see a problem, then post about the problem you are seeing. If you aren't seeing a problem, we can't "fix" it for you. – markspace Nov 02 '20 at 21:34
  • 2
    Comparing floating-point numbers using `==` is generally a bad idea. Consider using `Double.compare(...)` instead. – nkrivenko Nov 02 '20 at 21:38

4 Answers4

1

Considering largest difference is calculated on same position value.

void compare(double[] Array1, double[] Array2) {
        int countDifferent = 0;
        double largestDiff = 0;
        int i= 0;
        for(; i < Array2.length && i < Array1.length; i++) {
            if(Array2[i] != Array1[i]) {
                countDifferent++; 
                if(Math.abs(Array1[i] - Array2[i]) > largestDiff) {
                    largestDiff = Math.abs(Array1[i] - Array2[i]);
                }
            }
        }
        if(i < Array1.length)
            countDifferent += Array1.length - i;
        if(i < Array2.length)
            countDifferent += Array2.length - i;
        if(countDifferent > 0) {
            System.out.println("Differnt Count: " + countDifferent);
            System.out.println("Largest Diff: " + largestDiff);
        }else {
            System.out.println("Arrays are equal..");
        }
    }
Pandey Amit
  • 657
  • 6
  • 19
-1

you did ok, no need for 2 loops, and no need for additional comparison
Can you do it in function like that? (pseudo code)

@Test   
public void compare(){

double[] array1 = {10.2, 2.7, 6.4};  
double[] array2 = {5.7, 4.0, 2.7};

//double[] array1 = {10.2, 2.7, 6.4};
//double[] array2 = {10.2, 2.7, 6.4};


int howManyDiffarent =0;
double largestDiffarenc = 0;

  if(array1.length != array2.length){
      System.out.println("Arrays are not equal");
      return;
   }

for (int i=0; i<array1.length; i++) { //run over the length    
    if (Double.compare(array1 [i],array2 [i]) > 0) { //compare content        
        howManyDiffarent++;                           //count differences   
        
        double compare = Math.abs(array1 [i]) - Math.abs(array2 [i]);
        if(compare > largestDiffarenc) { //store the largest
            largestDiffarenc = compare;
        }
    }
 }
if(howManyDiffarent==0) {                       
    System.out.println("Arrays are equal");//no differences found 
}else {                               
    System.out.println("Arrays are not equal, there are:" +howManyDiffarent + " differences, largest diffrence is:" +largestDiffarenc);
    
}

}

JavaSheriff
  • 7,074
  • 20
  • 89
  • 159
-1

First, check the length, then if it's the same length compare values at the same indexes and count the number of not equalled values if there are any

here is my suggested code for you and you can change it depending on what you do need exactly.

int cmp = 0;
if(Array1.length != Array2.length){
   System.out.println("Arrays are not equal - not the same length -");
   return;
}else
       for (i=0; i<Array1.length; i++)
           if (Array1[i] != Array2[i])
                cmp++;    
if(cmp == 0) System.out.println("the two arrays are equalled");
else System.out.println("the two arrays are not equalled with " + cmp + " different values.");
  • 1
    Doesn't answer #2. – Andreas Nov 02 '20 at 21:46
  • I answered #2 - how many values are differing -, I didn't answer it's last part - the largest difference - cause I considered his problem about the methodology not about writing full code. He can just add two or three simple lines to find the largest difference that's why I said " you can change it depending on what you do need exactly" – Karim Hemina Nov 02 '20 at 21:52
-1

I think you can do something like this if your arrays are of same length.

I have not run the code didn't have java setup so there might be bugs.

double[] Array1 = {10.2, 2.7, 6.4};  
double[] Array2 = {5.7, 4.0, 2.7}; 

boolean is_same_length=false;
boolean is_same = true;
int total_different_value_count = 0;
double difference = 0;

if(Array1.length == Array2.length)
{
    is_same_length = true;
}
else
{
    is_same_length = false;
    is_same = false;
}

if(is_same_length)
{
    for (int i=0; i<Array1.length; i++) {
        if(Array1[i] != Array2[i])
        {
            is_same = false;
            total_different_value_count++;
            if(Array1[i] - Array2[i] > difference)
            {
                difference = Array1[i] - Array2[i];
            }

            if(Array2[i] - Array1[i] > difference)       //Edit missed this
            {
                difference = Array2[i] - Array1[i];
            }
        }
    }
}

if(is_same == true)
{
    System.out.println ("Arrays are equal");
}
else
{
    System.out.println ("Arrays are not equal");
    System.out.println ("Count of values that are different :" + total_different_value_count);
    System.out.println ("Largest difference between two values are :" + difference);
}
Haider Ali
  • 22
  • 6