mardi 18 juin 2019

How do I find the smallest value in an array of Complex numbers a + ci

import java.util.Scanner; import java.util.Random;

I'm trying to write a method which finds the smallest value in an array of Complex numbers a + ci, where a and c are both doubles. a is the real part and ci is the imaginary part. I'm trying to have it go through the array and compare each object a + ci and b + di. I was going to start by comparing the real parts and if the real parts were equal, then I would move on to the imaginary ones such that complexNumberOne < complexNumberTwo if a < b or if (a==b), c < d.

So far I have a method that can determine the smallest number in the array, but only by comparing the real parts:

private boolean arrayTest = false;

public double min(Complex[] arr) { double min = Double.MIN_VALUE;

        for(int i = 0; i < arr.length;i++) {
            if(arr[i].getReal() < min) {
                min = arr[i].getReal();
            }
        }
        return min;
    }

But my method to find the smallest element according the the aforementioned conditions seems to go nowhere:

public Complex smallest(Complex[] arr) {

        Complex min = new Complex();

        for(int i = 0; i < arr.length;i++) {
            while(!arrayTest) {
                if(arr[i].getReal()<arr[i+1].getReal())
                {
                    arrayTest=true;
                    min = arr[i];
                }else if(arr[i].getReal()==arr[i+1].getReal()) {
                    while(!arrayTest) {
                        if(arr[i].getImaginary()<arr[i+1].getImaginary()) {
                            arrayTest=true;
                            min = arr[i];
                        }
                    }
                }
            }
        }
        return min;
    }




Aucun commentaire:

Enregistrer un commentaire