pls write the codes as simple as possible. Thank you. JAVA Given an array of int
ID: 3841497 • Letter: P
Question
pls write the codes as simple as possible.
Thank you.
JAVA
Given an array of integers, calculate which fraction of its elements are positive, which fraction of its elements are negative, and which fraction of its elements are zeroes, respectively. Print the decimal value of each fraction on a new line. Input Format The first line contains an integer, N, denoting the size of the array. The second line contains N space-separated integers describing an array of numbers (a_0, a_1, ..., a_n - 1). Output Format You must print the following 3 lines: 1. A decimal representing of the fraction of positive numbers in the array. 2. A decimal representing of the fraction of negative numbers in the array. 3. A decimal representing of the fraction of zeroes in the array. Sample Input 6 -4 3 -9 0 4 1 Sample Output 0.500000 0.333333 0.166667 Explanation There are 3 positive numbers, 2 negative numbers, and 1 zero in the array. The respective fractions of positive numbers, negative numbers and zeroes are 3/6 = 0.500000, 2/6 = 0.333333 and 1/6 = 0.166667, respectively.Explanation / Answer
import java.util.*;
class ArrayFractioning {
public static void main(String args[])
{
Scanner sc= new Scanner(System.in); //taking input from user as array size
System.out.println("Enter the sizze of your array:");
int n= sc.nextInt();
int a[]= new int[n];
System.out.println("Enter the elements of your array"); // taking input from user, the array
for (int i=0;i<n;i++);
{
a[i]= sc.nextInt();
}
cal_fraction(a);
}
static void cal_fraction(int arr[]) /*cal_fraction is the method that will count number of positive,negative and zero integers and then calculate there fraction in decimal and return the value.*/
{
int pos=0;
int neg=0;
int zero=0;
for(i=0;i<arr.length;i++)
{
if(arr[i]>0) //if condition checks the array from first till the last element
{ pos = pos++;
}
else if(arr[i]<0)
{ neg = neg++;
}
else
{ zero = zero++;
}
}
double frac_pos = pos/n;
double frac_neg = neg/n;
double frac_zero = zero/n;
System.out.println(+frac_pos);
System.out.println(+frac_neg);
System.out.println(+frac_zero);
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.