Home > Software design >  How to print the nearest integer to zero given an array of doubles?
How to print the nearest integer to zero given an array of doubles?

Time:03-01

Good evening, currently I am stuck on this homework problem:

Print out the double that is closest to 0, positive or negative.

My current code is:

import java.util.Arrays;
import java.util.Scanner;

public class Code {

    public static void main(String[] args){
        Scanner scanner = new Scanner(System.in);
        // Read input for the test cases
        int N = scanner.nextInt();
        double[] doubles = new double[N];
        double closestToZero = N;
        for(int i = 0; i < N; i  ) {
            doubles[i] = scanner.nextDouble();
        }

        // Your code here
        for(int a = 0; a < N; a  ) {
            if (Math.abs(a) < 0) {
                closestToZero = a;
            }

                else if(Math.abs(a) == 0.0) {
                    closestToZero = a;
                }
                else {

                }
            }         
        System.out.println("The double closest to 0 is "   closestToZero);

    }
}

Issue: The code keeps printing out 0.0 even though it is the incorrect answer, What would be the cause for the error?

CodePudding user response:

Closest to 0 is the smallest number in absolute value. Then you only have to find the minimum. Update closestToZero if a smaller number is found.

import java.util.Arrays;
import java.util.Scanner;

public class Code {
    public static void main(String[] args){
        Scanner scanner = new Scanner(System.in);
        // Read input for the test cases
        int N = scanner.nextInt();
        double[] doubles = new double[N];
        double closestToZero = Double.MAX_VALUE;
        for(int i = 0; i < N; i  ) {
            doubles[i] = scanner.nextDouble();
        }

        // Your code here
        for(int a = 0; a < N; a  ) {
            if (Math.abs(doubles[a]) < Math.abs(closestToZero)) {
                closestToZero = doubles[a];
            }
        }   
        
        System.out.println("The double closest to 0 is "   closestToZero );
    }
}

CodePudding user response:

here is a faster and more efficient way of finding the closent value to zero from a Double[] array. Please let me know if you have any question, and don't forget to accept the answer!

import java.util.Arrays;
import java.util.Scanner;

public class StackOverflow_Tester {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        System.out.println("Enter number of integers: ");
        int N = in.nextInt();
        double[] doubles = new double[N];
        for(int i = 0; i < N; i  ) {
            doubles[i] = in.nextDouble();
        }
        double curr = 0;
        double near = doubles[0];
        // find the element nearest to zero
        for ( int i=0; i < doubles.length; i   ){
            curr = doubles[i] * doubles[i];
            if ( curr <= (near * near) )  {
                near = doubles[i];
            }
        }
        System.out.println( near );
    }
}
  • Related