jeudi 24 octobre 2019

Question on adding value in an array to another array for counting c++

Here's the situation:

User can choose up to 4 dices on the table with a range[1-12] faces they want (Yes, 1 face dice is a THING here). Then the program will calculate all the outcome possibilities.

For example: 2 dices, 1st with 6 faces, 2nd with 2 faces.

Output:

Sum of 2 = 1

Sum of 3 = 2

Sum of 4 = 2

Sum of 5 = 2

Sum of 6 = 2

Sum of 7 = 2

Sum of 8 = 1

I have found out the pattern to calculate with all of the possibilities with different of no. of dices and faces they have, here's the illustration:

4 dices with faces [6, 2, 3, 4] respectively

Click here to check the pattern

Blue area is a dice with 6 faces Green area is 6 faces with 2 times Yellow area is green area loop with 3 times Read area is yellow area loop with 4 times

The numbers aside are the count of the appearance of each sum and it's correct all the time no matter what inputs are.

However, I've attempted many times and still cannot find the correct way to implement this pattern into c++ program.

My codes are like this:

// Calculate the first iteration of dices

for (int k = 0; k < faces[2]; k++) {
        for (int j = num + k; j < (num + faces[1] + k); j++) {
            tempCount[j]++;
        }
    }

    // Copy results into counter1[]

    for (int i = num; i <= faceCounter; i++) {
        counter1[i] += tempCount[i];
    }

    // Find out the remaining dices

    for (int i = 2; i < num; i++) {
        for (int k = 0; k < faces[i+1]; k++) {                          // Calculate the count range
            for (int j = num + k + 1; j < (num + faces[i] + k); j++) {  // Add the previous counter's value into temp counter
                tempCount[j] += counter1[i];
            }
            if (k == faces[i + 1] - 1)                                  // Make sure finished the for loop first then to final addition, won't duplicate data
                finished = 1;
        }

        //Load results back to counter, which will using it back in loop for further counting

        for (int i = num; (i <= faceCounter) && (finished = 1); i++) {
            counter1[i] += tempCount[i];
        }

        finished = 0;
    }

This is not working though.

How can I change it?

Aucun commentaire:

Enregistrer un commentaire