Answer to Question #300882 in C++ for Kelly

Question #300882

How to execute this using vector




#include <cstdlib>


#include <ctime>


#include <iostream>



int main() {



int dice[6] = {0};



srand(time(0));



for (int ctr = 0; ctr < 10000; ctr++) {



switch ((rand() % 6) + 1) {


case 1:


dice[0] += 1;


break;



case 2:


dice[1] += 1;


break;



case 3:


dice[2] += 1;


break;



case 4:


dice[3] += 1;


break;



case 5:


dice[4] += 1;


break;



case 6:


dice[5] += 1;


break;


}


}



for (int index = 0; index < 6; index++)


std::cout << index + 1 << " appears " << dice[index] << " time/s\n";



return 0;


}

1
Expert's answer
2022-02-22T06:43:47-0500
#include <cstdlib>
#include <ctime>
#include <iostream>

int main() {
  vector<int> dice(6, 0);
  srand(time(0));
  for (int ctr = 0; ctr < 10000; ctr++) {
    switch ((rand() % 6) + 1) {
    case 1:
      dice[0] += 1;
      break;
    case 2:
      dice[1] += 1;
      break;
    case 3:
      dice[2] += 1;
      break;
    case 4:
      dice[3] += 1;
      break;
    case 5:
      dice[4] += 1;
      break;
    case 6:
      dice[5] += 1;
      break;
    }
  }
  for (int index = 0; index < 6; index++) {
    std::cout << index + 1 << " appears " << dice[index] << " time/s\n";
  }
  return 0;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog