Created
June 3, 2016 12:45
-
-
Save slavaceornea/93d2fd67a13eccb94585e3f8958aa8e7 to your computer and use it in GitHub Desktop.
Java class that prints the fractions of positives, negatives and zeroes in an array.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.*; | |
import java.util.*; | |
import java.text.*; | |
import java.math.*; | |
import java.util.regex.*; | |
public class FractionsOfPositivesNegativesAndZeroesInArray { | |
public static void main(String[] args) { | |
Scanner in = new Scanner(System.in); | |
int n = in.nextInt(); | |
int arr[] = new int[n]; | |
for(int arr_i=0; arr_i < n; arr_i++){ | |
arr[arr_i] = in.nextInt(); | |
} | |
printFractionsOfPositivesNegativesAndZeroes(arr); | |
} | |
private static void printFractionsOfPositivesNegativesAndZeroes(int[] array) | |
{ | |
double positivesFraction = 0, negativesFraction = 0, zeroesFraction = 0; | |
for(int i = 0; i < array.length; i++) | |
{ | |
if(array[i] < 0) | |
negativesFraction += 1.0 / array.length; | |
if(array[i] == 0) | |
zeroesFraction += 1.0 / array.length; | |
if(array[i] > 0) | |
positivesFraction += 1.0 / array.length; | |
} | |
System.out.println(String.format("%.6f", positivesFraction)); | |
System.out.println(String.format("%.6f", negativesFraction)); | |
System.out.println(String.format("%.6f", zeroesFraction)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment