-2

I need my program to display the expected and actual percentages of how often the sums of two dice appear. I can't get the expected percentages to display the array, it always comes out as 0. I am not sure how to calculate the actual percentages using the random values.

I expect the output to show as decimals or as fractions, but I only get zeros.

Sherry
  • 1
  • 1
  • 1
    https://stackoverflow.com/questions/4685450/int-division-why-is-the-result-of-1-3-0 and https://stackoverflow.com/questions/3144610/integer-division-how-do-you-produce-a-double – Marvin Apr 24 '19 at 20:51
  • 1
    @Marvin OP would also need to change the type of `expected` and `actual` to `float` or `double`, and the `printf` format string to `"%8d %10d %10f %20f\n"`. – Jonny Henly Apr 24 '19 at 20:55
  • 1
    Yes. And none of the links helps with the index out of bounds. But I was too lazy for more information. And this should be a good starting point at least. – Marvin Apr 24 '19 at 20:56
  • Java arrays are zero based. So your for(int i=1; i – Evan Apr 24 '19 at 20:56
  • You're not setting actual to anything but zero that I see. I hate to be critical, but this code just needs some review and completion on your end. You should probably read through some basic tutorials on data types and formatting strings. – Evan Apr 24 '19 at 20:59
  • @Marvin I'm not seeing the index out of bounds, but I do notice the lack of the `actual` calculation. – Jonny Henly Apr 24 '19 at 21:00
  • There's definitely an array out of bounds error when she tries to loop over a 1 based i initializing values to 0. – Evan Apr 24 '19 at 21:01
  • @Evan OP made their arrays size 13 to account for the sum of 2 dice which is at most 12, that's why they start with index 1. – Jonny Henly Apr 24 '19 at 21:01
  • 1
    `expected` only has 11 elements, not 13. – Marvin Apr 24 '19 at 21:02
  • @Marvin I just noticed that, I assumed it had 13. – Jonny Henly Apr 24 '19 at 21:04

2 Answers2

1

If you use the operator / on two integers, it will behave like whole number division. That means the array expected will contain only zeros. Also the array is declared as int[] so it can't contain decimal values. You need to declare your number as float and use float literals in the array definition.

float [] expected={1f/36,2f/36,3f/36,4f/36,5f/36,6f/36,5f/36,4f/36,3f/36,2f/36,1f/36};

Or you can use double:

double [] expected={1.0/36,2.0/36,3.0/36,4.0/36,5.0/36,6.0/36,5.0/36,4.0/36,3.0/36,2.0/36,1.0/36};
  • It tells me I can't convert from a double to float when I make the numerators decimals. – Sherry Apr 24 '19 at 21:12
  • I edited my code. Yes, you are right 1.0 will create double literal, I needed to change it to 1f. – Michal Horvath Apr 24 '19 at 21:22
  • Thanks, that worked! The only problem is that is that the values start displaying from the 3.0/36 mark and ignore the first two for some reason. I realize it's because the loop starts from i=2, which is needed for the sum. Would I have to create a new loop? – Sherry Apr 24 '19 at 21:39
0

If you divide an integer with another integer (1/36 for example, both are integers), the result will always be an integer. You need to cast them into doubles or floats, if you don't want to get 0. You can find a lot of questions and answers on this if you make a quick search.

Can Bayar
  • 379
  • 2
  • 15