### Dice Roll problem using arrays?

So I managed to get this simple code to work. Rolling a dice 6000 times and then recording and displaying how many times each number was rolled.

Now I'm trying to get this to work using an array instead.

I know I could start out with declaring an array like

int frequency[6] // to store the frequency of how many times a number was rolled

But I'm not to sure how to implement this in a loop and then output the results at the end.

Any help would be nice.

 ``12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849`` ``````#include #include #include int main () { int random_num; std::srand(std::time(0)); int roll1 = 0; int roll2 = 0; int roll3 = 0; int roll4 = 0; int roll5 = 0; int roll6 = 0; int i; for (int i=0; i < 6000; i++){ int random_num = std::rand() % 6 + 1; if (random_num == 1) roll1 = roll1 + 1; else if (random_num == 2) roll2 = roll2 + 1; else if (random_num == 3) roll3 = roll3 + 1; else if (random_num == 4) roll4 = roll4 +1; else if (random_num == 5) roll5 = roll5 + 1; else if (random_num == 6) roll6 = roll6 + 1; } std::cout << "1 was rolled: " << roll1 << " times " << std::endl; std::cout << "2 was rolled: " << roll2 << " times " << std::endl; std::cout << "3 was rolled: " << roll3 << " times " << std::endl; std::cout << "4 was rolled: " << roll4 << " times " << std::endl; std::cout << "5 was rolled: " << roll5 << " times " << std::endl; std::cout << "6 was rolled: " << roll6 << " times " << std::endl; return 0; } ``````
@Chedderlord

Here is one way, though I'm sure there are more elegant ways of doing it. I commented out some ( most ) of your code, to better compare your code to the corrected code.

Hope this helps you out.

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657`` ``````// Dice Rolls.cpp : main project file. #include #include #include int main () { int random_num, roll_ck=0; srand((unsigned) time(0)); int roll[6] = {0};// Declare and initialize to 0 /* int roll1 = 0; int roll2 = 0; int roll3 = 0; int roll4 = 0; int roll5 = 0; int roll6 = 0; int i; */ for (int i=0; i < 6000; i++) { random_num = 1+ rand()% 6; roll[random_num-1]++; // Since numbers are 1 to 6, reduce by one to increase correct 0 to 5 array } for (int i=0;i<6;i++) { std::cout << "\tA " << i+1 << " was rolled " << roll[i] << " times." << std::endl; roll_ck+=roll[i]; } std::cout << "Rolls = " << roll_ck << std::endl; // To verify amount of dice rolls /* if (random_num == 1) roll1 = roll1 + 1; else if (random_num == 2) roll2 = roll2 + 1; else if (random_num == 3) roll3 = roll3 + 1; else if (random_num == 4) roll4 = roll4 +1; else if (random_num == 5) roll5 = roll5 + 1; else if (random_num == 6) roll6 = roll6 + 1; } std::cout << "1 was rolled: " << roll1 << " times " << std::endl; std::cout << "2 was rolled: " << roll2 << " times " << std::endl; std::cout << "3 was rolled: " << roll3 << " times " << std::endl; std::cout << "4 was rolled: " << roll4 << " times " << std::endl; std::cout << "5 was rolled: " << roll5 << " times " << std::endl; std::cout << "6 was rolled: " << roll6 << " times " << std::endl; */ return 0; }``````
Last edited on
 ``123456789101112131415`` ``````#include #include #include using namespace std; int main() { srand(time(0)); int frequency[6]={0}; for(int i=0;i<6000;i++) frequency[rand()%6]++; for(int i=0;i<6;i++) cout<
Topic archived. No new replies allowed.