-4

I have a problem with rand() function I want to build a program that shows the number of each face of dice when you throw it 6000 times

I wrote this code

#include <iostream>
using namespace std;
#include <iomanip>
using std::setw;
#include <cstdlib>
#include <ctime>
int main()
{
int face ;
int frequency1 =0;
int frequency2 =0;
int frequency3 =0;
int frequency4 =0;
int frequency5 =0;
int frequency6 =0;
for(int counter =1;counter <=6000;counter++){

      face = 1+rand()%6;
      switch(face){
    case 1:
        ++frequency1;
        break;

     case 2:
        ++frequency1;
        break;

     case 3:
        ++frequency1;
        break;

     case 4:
        ++frequency1;
        break;

     case 5:
        ++frequency1;
        break;

     case 6:
        ++frequency1;
        break;
     default:
        cout<<"program should never get here!!! ";
        break;
  }}
  cout<<"the number of face 1 is : "<<frequency1<<endl;
 cout<<"the number of face 2 is : "<<frequency2<<endl;
 cout<<"the number of face 3 is : "<<frequency3<<endl;
 cout<<"the number of face 4 is : "<<frequency4<<endl;
 cout<<"the number of face 5 is : "  <<frequency5<<endl;
 cout<<"the number of face 6 is : "      <<frequency6<<endl;
 return 0;
 }

Every time that i run this code it shows the same thing

the number of face 1 is : 6000
the number of face 2 is : 0
the number of face 3 is : 0
the number of face 4 is : 0
the number of face 5 is : 0
the number of face 6 is : 0
moein
  • 43
  • 1
  • 6

1 Answers1

0

You just keep adding to frequency1 instead of other cases. I've tried to explain to you, but you still do not seem to understand. Here is the edited code:

#include <iostream>
using namespace std;
#include <iomanip>
using std::setw;
#include <cstdlib>
#include <ctime>

int main() {
    int face;
    int frequency1 = 0;
    int frequency2 = 0;
    int frequency3 = 0;
    int frequency4 = 0;
    int frequency5 = 0;
    int frequency6 = 0;
    for(int counter =1;counter <=6000;counter++){
        face = 1+rand()%6;
        switch(face){
            case 1:
                ++frequency1;
                break;

            case 2:
                ++frequency2;
                break;

            case 3:
                ++frequency3;
                break;

            case 4:
                ++frequency4;
                break;

            case 5:
                ++frequency5;
                break;

            case 6:
                ++frequency6;
                break;
            default:
                cout<<"program should never get here!!! ";
                break;
        } 
    }
    cout<<"the number of face 1 is : "<<frequency1<<endl;
    cout<<"the number of face 2 is : "<<frequency2<<endl;
    cout<<"the number of face 3 is : "<<frequency3<<endl;
    cout<<"the number of face 4 is : "<<frequency4<<endl;
    cout<<"the number of face 5 is : "<<frequency5<<endl;
    cout<<"the number of face 6 is : "<<frequency6<<endl;
    return 0;
}

As you see, your original code does ++frequency1 (which increments) in all case statements. This edited code should work. I've already explained this.

Andrew Li
  • 55,805
  • 14
  • 125
  • 143