Chris Schulz
Chris Schulz

Reputation: 11

how to set an array's elements with a specific algorithm?

generatePrizes creates an array of prize amounts based on the size of the lottery pick and the total prizeMoney. The array positions correspond to the count of correct numbers in a lottery pick (zero through size, inclusively).

The algorithm for generating prize money is as follows. If a player guesses 0, 1, or 2 numbers correctly the prize amount is 0. The remaining prize money is generated by awarding to each level (starting with correctly guessing all of the numbers) 3/4 of the available prize money, rounded to the nearest dollar, with the last count (3 correct) receiving whatever remains. For example, if the lottery were 6 numbers and the prizeMoney were 1000, 6 correct would get 750, 5 correct would get 3/4 of the remainder or 188, 4 correct would get 3/4 of the remainder or 47, and 3 correct would get the rest, 15. If there were only 3 numbers and 500 in prize money, 3 matching numbers would get all 500. Note that the return value (prizes) array length will be one more than the count of numbers in the lottery (size), since it must include the prize for zero and for size.

I can't seem to figure out how to make this method work the way I am supposed too. I have tried the code below but it just fills every element with the same number, how do I change that?

public static int[] generatePrizes(int size, int prizeMoney) {
 int[] prizes = new int[size + 1];
 int remainder = 0;
 for (int i = 0; i < 2; i++) {
   prizes[i] = 0;
 }
 for (int j = size; j > 2; j--) {
   prizes[j] = (int)Math.round(prizeMoney * .75);
 }
   
  
   return prizes;
 }

Upvotes: 0

Views: 65

Answers (1)

Swetha
Swetha

Reputation: 467

    public static int[] generatePrizes(int size, int prizeMoney) {
     int[] prizes = new int[size + 1];
     int remainder = prizemoney;
     int lottery = prizeMoney ;
     for (int j = size; j > 3; j--) {
       prizes[j] = lottery ;
       remainder=  remainder - prizes[j];
       lottery = (int)Math.round(remainder* .75);
     }
     for (int k = 3; k >= 0; k--) {
         prizes[k] = remainder;
         remainder = 0;
     }
   return prizes;
 }

This should solve.You were not updating the remaining prize money amount.

Upvotes: 1

Related Questions