hello i have a function that should get the amount of ways you can throw a specific number (sum off all dice) with 9 four sided dice and 6 six sided dice. the problem is i have in total about 15 for loops all nested and they are really annoying me. so could someone tell me how i could reduce the loops (by some formula to get it immediately or if side=6 then it only does 6 loops and if side=4 then it does 9 loops.)
#include <iostream>
#include <cmath>
int amount_of_possibilities(int total,int sides) {
int amount=0;
if(sides==4) {
for (int a1=1;a1<5;a1++) {
for (int a2=1;a2<5;a2++) {
for (int a3=1;a3<5;a3++) {
for (int a4=1;a4<5;a4++) {
for (int a5=1;a5<5;a5++) {
for (int a6=1;a6<5;a6++) {
for (int a7=1;a7<5;a7++) {
for (int a8=1;a8<5;a8++) {
for (int a9=1;a9<5;a9++) {
if (a1+a2+a3+a4+a5+a6+a7+a8+a9==total) {
amount++;
}
}
}
}
}
}
}
}
}
}
}
if(sides==6) {
for (int a1=1;a1<7;a1++) {
for (int a2=1;a2<7;a2++) {
for (int a3=1;a3<7;a3++) {
for (int a4=1;a4<7;a4++) {
for (int a5=1;a5<7;a5++) {
for (int a6=1;a6<7;a6++) {
if (a1+a2+a3+a4+a5+a6==total) {
amount++;
}
}
}
}
}
}
}
}
return amount;
}
int main() {
int pyramid_sides=4;
constint pyramid_dices=9;
int pyramid_chance[36-pyramid_dices];//shows how many combinations can be made with index+9 as total
int cube_sides=6;
constint cubic_dices=6;
int cubic_chance[36-cubic_dices];//shows how many combinations can be made with index+6 as total
for(int i=0;i<28;i++) {
pyramid_chance[i]=amount_of_possibilities(i+pyramid_dices,pyramid_sides);
}
for(int i=0;i<31;i++) {
cubic_chance[i]=amount_of_possibilities(i+cubic_dices,cube_sides);
}
for(int i=0;i<31;i++) {
std::cout<<cubic_chance[i]<<std::endl;
}
system("pause");
return 0;
}
example: if i want to know in how many ways i can throw an 8 in total (sum) when you have 3 four-sided dice.
i could make a table and i should find that there are 12 ways to get an
8 in total. i listed all the 8 ways below. note that the sum of 3 numbers below each other is always 8.