-2

Trying to take my sumDanceScore method and divide each element in my danceScore array by the return value of sumDanceScore; however, when It just keeps comming back 0. I have placed println's to show that there are two legitamate integers there however it is always ==0 please help!

 package javasandbox;
 import java.util.*;


 public class JavaSandbox {


public static int sumDanceScore(int danceScore[], int first, int last)
 {


    if (first > last) 
     {
      return 0;
     }
   else
    {
      int total = sumDanceScore(danceScore,first+1,last) + danceScore[first];
      return total;
   }
}

   public static void main(String[] args)
 {


    Scanner kbd = new Scanner(System.in);
    System.out.println("Enter number of contestants : ");
    int numContest = kbd.nextInt();
    int danceScore[] = new int[numContest + 1];
    int first = 0;
    int last = danceScore.length - 1;
    System.out.println("Enter dance scores: ");

    int numContestIndex;
    for (numContestIndex = 1; numContestIndex <= numContest; numContestIndex++) 
    {
         danceScore[numContestIndex] = kbd.nextInt();
    }
    int danceScoreTotal = sumDanceScore(danceScore, first, last);
    System.out.println("SUM DANCE SORE METHOD: "+danceScoreTotal);

    for(int danceScoreIndex = 1; danceScoreIndex <= danceScore.length-1; danceScoreIndex++)
    {
        System.out.println("DANCE SCORE INDEX NUMBER: "+danceScore[danceScoreIndex]);
        int danceScoreShare = danceScore[danceScoreIndex] /  danceScoreTotal;
        System.out.println("DANCER SHARE PERCENT: "+danceScoreShare);
    }
} 

}

2 Answers2

1

It's obvious that on dividing each element in my danceScore array by the bigger return value of sumDanceScore, the return value will sure be 0 because of the int/int division.

The rule says that in numerator/denominator division, if numerator < denominator,then the result will be 0 due to truncation in int.

int(smaller)/int(bigger_sum) ~= 0.some_value = 0 (in terms of result as int)

Example :-

4/20 = 0.20(in double) = 0 (for int result).

The workaround is to use float/double as the data-type for the division variable.

Am_I_Helpful
  • 18,735
  • 7
  • 49
  • 73
1

You need to cast the ints to a floating point number (float or double) first . danceScoreShare is going to be a fraction, so it should also be a float or double.

double danceScoreShare = (double)danceScore[danceScoreIndex] /  (double)danceScoreTotal;

Your danceScoreTotal will always be bigger than danceScore[danceScoreIndex], so with java integer division you will always get a 0 result.

actually you can just cast one of the right hand arguments and binary numeric promotion does the other

The technical details from the JLS:

... the quotient produced for operands n and d that are integers after binary numeric promotion (§5.6.2) is an integer value q whose magnitude is as large as possible while satisfying |d · q| ≤ |n|.

So, for example, with the division 9 / 10, you can see that |10 · 1| = 10 which is greater than |9|. Therefore it returns 0.

Andy Brown
  • 18,961
  • 3
  • 52
  • 62