From 317ce9d5b1eaa064f793fecf3428ad8ff874045d Mon Sep 17 00:00:00 2001 From: Sami Date: Mon, 21 Oct 2024 12:41:23 -0400 Subject: [PATCH] RatioSomething --- RatioArray.class | Bin 0 -> 1734 bytes RatioArray.java | 60 +++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 60 insertions(+) create mode 100644 RatioArray.class create mode 100644 RatioArray.java diff --git a/RatioArray.class b/RatioArray.class new file mode 100644 index 0000000000000000000000000000000000000000..83e009a30aeb55a9da3f84db508a45d5ce91c734 GIT binary patch literal 1734 zcmaJ>-*XyO6#gzOEG*klsf3cWDOzZOq(Nr*-6b<| z#Ky?n z;(-DWyfP#eKKL0ybJfOU-gFrJOS+*uw-~(X%({XAf-)2pA*c+>L(S36 zJC>#GGx*#M&FPJDW<7`rgk_vo@fso{z_oqb;cdb*y96JtxFK~j_o1a5PR+5n)-FUS zyVq4jG06~XYrDK?8cof4q&qvbvK`HEZ1PQ)GkteAWAfFF?Sg`{h|8E#F^zK!XU5<& z$Q==_WeAXy5SjG|7-mEQ7lcJ=Oqd8UHxq#uGa}$c5zQY!iXpqU!xNSuCSh(R9NKhD zTX*z5o-n%Y2DcKry_iVN=eHCEmywo{QIW;$2uaIkx6$I1=bqN;^6HkzVtF`=zJXip z6&3Tyky36=WV$q_q>Z5}`_dK$aQ~v7K`NS?6gphd4gSE*cujMYk=mM0j7_G8!3L~n z))`LK9Id&#s&!m@8F>cJMwzZj&ForDUeZNH;p00qFQlQe@0zA#JC@d2<<5?|X$MeX zm=?^nxUuc*yy8)_`rNSqZZgaXo(GH)(f^Q4y4nfdaG4+Sc<+RV!M8~zlI1@glQ(!xz;9)Ud|0wk{oURfKoqrBtEM}g_*ct@!L4FTvu$q6 zc%R5PQPa3oWmKqj{@;}DYQHYLK@Wa<6E>lPcx(0m;Dj!2U;+^O{4fN z#vtCoRay(vn~+?H^MRj~x>u^!CHj2cYTfUt)@4t%GW!hjF~kH#0%oXZFW?*!P{dQ# zhhjEl+5x|1kCvX|)N~JLfJ4MWE1^)c|ISA2EK)o zR2wS?n2H9zfs#@ir2|Yv179N;lhnq7^h9O{FP{@=vPZ=UP+F&Yl)zvelX}F4*d(Vv zH9o)LOSzLAliWE-=7)Hb#z<278J@cLtnV2v)ebTDX!uYbcNlziC}{AwOg4xB;3AQc zL>MW0r7uyjmobSna>(EsJxK-3;x^`R4_8p3H|8Pz`-r?ZiTw`o{|Q+=!F7CwJifrY z_zDa79yjm{7V(0-uTg{qd@qH{GOja^$0H^N{E0XV{Q>27lH5z4BC7@R1g1#MyClb= XJ1)87EqA<)`}AL-e`S1tRmA@W!$G0r literal 0 HcmV?d00001 diff --git a/RatioArray.java b/RatioArray.java new file mode 100644 index 0000000..b6dc666 --- /dev/null +++ b/RatioArray.java @@ -0,0 +1,60 @@ +import java.text.DecimalFormat; + +/** + * RatioArray + */ +public class RatioArray { + + // Given an array of integers, calculate the ratios of its elements that are + // positive, negative, and zero. Print the decimal value of each fraction on a + // new line with + // + // places after the decimal. + // + // Note: This challenge introduces precision problems. The test cases are scaled + // to six decimal places, though answers with absolute error of up to + // + // are acceptable. + // + // Example + // There are elements, two positive, two negative and one zero. Their ratios are + // , and + // + // . Results are printed as: + // + // 0.400000 + // 0.400000 + // 0.200000 + // + public static void main(String[] args) { + int[] ar = { 1, 1, 0, -1, -1 }; + Ratio(ar); + } + + private static void Ratio(int[] arr) { + int n = arr.length; + System.out.println("the length of the arr is: " + n); + float pos = 0; + float neg = 0; + float zero = 0; + for (int x : arr) { + if (x > 0) { + pos++; + } else if (x < 0) { + neg++; + } else if (x == 0) { + zero++; + } + } + System.out.println("The number of positive ints in the arr is: " + pos); + System.out.println("The number of negative ints in the arr is: " + neg); + System.out.println("The number of zeros int the arr is: " + zero); + double posRatio = pos / n; + double negRatio = neg / n; + double zeroRatio = zero / n; + System.out.println(); + System.out.printf("The ratio of the positive number is: " + "%.6f\n", posRatio); + System.out.printf("The ratio of the positive number is: " + "%.6f\n", negRatio); + System.out.printf("The ratio of the positive number is: " + "%.6f\n", zeroRatio); + } +}