Home > Blockchain >  Which number showed up most often in a array
Which number showed up most often in a array

Time:07-15

This code is for a coin flipping program. Where a coin is flipped 1000 times and the program shows you how many times each number from 1-10 appeared those 1000 times, which side of the coin came up more often and which number showed up more often.

Currently, the code shows how many times each number from 1-10 appeared in those 1000 flips and which side of the coin came up more often, but I am struggling with how to get the program to show which number showed up more often.

I need the program to show which number from 1-10 showed up the most during those 1000 flips.

package so;

import java.util.Random;

public class CoinFlipping {

    public static void main(String[] args) {
        Random randomNumbers = new Random(); // random number generator

        int frequency1 = 0, frequency2 = 0, frequency3 = 0, frequency4 =
        0, frequency5 = 0, frequency6 = 0, frequency7 = 0, frequency8 = 0,
        frequency9 = 0, frequency10 = 0;// stores individual frequencies

        int number; // stores most recently generated value

        // summarize results of 1000
        for (int col = 1; col <= 1000; col  ) {
            number = 1   randomNumbers.nextInt(10); // numbers from 1 to 10


            // determine values 1-10 and increment appropriate counter
            switch (number) {
                case 1:
                      frequency1; // increment the 1s counter
                    break;
                case 2:
                      frequency2; // increment the 2s counter
                    break;
                case 3:
                      frequency3; // increment the 3s counter
                    break;
                case 4:
                      frequency4; // increment the 4s counter
                    break;
                case 5:
                      frequency5; // increment the 5s counter
                    break;
                case 6:
                      frequency6; // increment the 6s counter
                    break;
                case 7:
                      frequency7; // increment the 7s counter
                    break;
                case 8:
                      frequency8; // increment the 8s counter
                    break;
                case 9:
                      frequency9; // increment the 9s counter
                    break;
                case 10:
                      frequency10; // increment the 10s counter
                    break;


            }
        }

        System.out.println("Number \t Times Landed");
        System.out.printf("1\t%d\n2\t%d\n3\t%d\n4\t%d\n5\t%d\n6\t%d\n7\t%d\n8\t%d\n9\t%d\n10\t%d\n", frequency1, frequency2, frequency3, frequency4, frequency5, frequency6, frequency7, frequency8, frequency9, frequency10);
        int even = 0, odd = 0;//stores even or odd totals
        even = frequency2   frequency4   frequency6   frequency8   frequency10;

        odd = frequency1   frequency3   frequency5   frequency7   frequency9;

        System.out.println("\n Heads: "   even   "\n Tails: "   odd);

    }

}

CodePudding user response:

Wow, let's refactor your code to make it easier to read and develop.

First, let's turn your variables into an array, where the the value at each index is the amount of times it has appeared.

int[] freqTable= new int[11];
...
// Then, when we generate a new number we simply do the following
freqTable[number]  ;

Then, we can also loop through the indices and printf more readably the elements and their count.

for (int i= 1; i <= 10;   i) {
    System.out.printf("%d\t%d\n", i, freqTable[i]);
}

We can use another loop to add to our even variable at the even indices, and add to our odd variable at the odd indices.

for (int i= 1; i <= 10;   i) {
    if (i % 2 == 0) {
        even = freqTable[i];
    } else {
        odd = freqTable[i];
    }
}

Now, let's develop an algorithm for finding the element that appears the most. We set the initial max value to however many times 1 appears, and then arbitrarily set 1 as the element that appears most. If it is, great, the loop won't change anything. If it isn't, the loop will change the current maximum number of appearances we've found and store that in the curMax variable, and set the number which this occurs with in the numberShowedUpMost variable.

int numberShowedUpMost= 1;
int curMax= freqTable[1];
for (int i= 2; i <= 10;   i) {
    if (freqTable[i] > curMax) {
        curMax= freqTable[i];
        numberShowedUpMost= i;
     }
}
System.out.printf("The number that showed up the most is %d\n", numberShowedUpMost);

CodePudding user response:

Try my code

 public void coinFlipping()
    {
        Map<Integer,Integer> result = new HashMap<>();
        result.put(1,0);
        result.put(2,0);
        result.put(3,0);
        result.put(4,0);
        result.put(5,0);
        result.put(6,0);
        result.put(7,0);
        result.put(8,0);
        result.put(9,0);
        result.put(10,0);
        Random randomNumbers = new Random();
        for (int i = 0; i <1000 ; i  ) {
            int val = randomNumbers.nextInt(10)  1;
            result.put(val,result.get(val) 1);
        }
        System.out.println(result);
    }

Result : {1=99, 2=110, 3=97, 4=91, 5=105, 6=94, 7=90, 8=89, 9=110, 10=115}

  • Related