Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Question How Would i get this to run if i were to use only vector statements to

ID: 3761993 • Letter: Q

Question

Question

How Would i get this to run if i were to use only vector statements to keep track of the number of rolls? not as a function but as a vector just to keep track of the number of rolls

#include <iostream>

#include <cstdlib>

#include <ctime>

using namespace std;

int main(){

   int i = 0; // Loop counter iterates numRolls times

   int numRolls = 0; // User defined number of rolls

   int numTwos = 0;

   int numThrees = 0;

   int numFours = 0;

   int numFives = 0;

   int numSixes = 0; // Tracks number of 6s found

   int numSevens = 0;

   int numEights = 0;

   int numNines = 0;

   int numTens = 0;

   int numElevens = 0;

   int numTwelve = 0;//

   int die1 = 0; // Dice values

   int die2 = 0; // Dice values

   int rollTotal = 0; // Sum of dice values

   int idK = 0;

   cout << "Enter number of rolls: " << endl;

   cin >> numRolls;

   srand(time(0));

   while (numRolls >= 0){

       if (numRolls >= 1) {

           // Roll dice numRoll times

           for (i = 0; i < numRolls; ++i) {

               die1 = rand() % 6 + 1;

               die2 = rand() % 6 + 1;

               rollTotal = die1 + die2;

               // Count number of sixs and sevens

               if (rollTotal == 2){

                   numTwos = numTwos + 1;

               }

               else if (rollTotal == 3) {

                   numThrees = numThrees++;

               }

               else if (rollTotal == 4){

                   numFours = numFours++;

               }

               else if (rollTotal == 5){

                   numFives = numFives++;

               }

               else if (rollTotal == 6) {

                   numSixes = numSixes + 1;

               }

               else if (rollTotal == 7) {

                   numSevens = numSevens + 1;

               }

               else if (rollTotal == 8){

                   numEights = numEights++;

               }

               else if (rollTotal == 9){

                   numNines = numNines++;

               }

               else if (rollTotal == 10){

                   numTens = numTens++;

               }

               else if (rollTotal == 11){

                   numElevens = numElevens++;

               }

               else if (rollTotal == 12){

                   numTwelve = numTwelve++;

               }

               cout << endl << "Roll " << (i + 1) << " is "

                   << rollTotal << " (" << die1

                   << "+" << die2 << ")";

           }

           // Print statistics on dice rolls

           cout << endl << endl;

           cout << "Dice roll statistics:" << endl;

           cout << "2s: " << numTwos << endl;

           cout << "3s: " << numThrees << endl;

           cout << "4s: " << numFours << endl;

           cout << "5s: " << numFives << endl;

           cout << "6s: " << numSixes << endl;

           cout << "7s: " << numSevens << endl;

           cout << "8s: " << numEights << endl;

           cout << "9s: " << numNines << endl;

           cout << "10s: " << numTens << endl;

           cout << "11s: " << numElevens << endl;

           cout << "12s: " << numTwelve << endl;

else {

           cout << "Invalid rolls. Try again." << endl;

       }

       return 0;

   }

Explanation / Answer

Hi,

My comments in last

#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
int main(){
int i = 0; // Loop counter iterates numRolls times
int numRolls = 0; // User defined number of rolls
int numTwos = 0;
int numThrees = 0;
int numFours = 0;
int numFives = 0;
int numSixes = 0; // Tracks number of 6s found
int numSevens = 0;
int numEights = 0;
int numNines = 0;
int numTens = 0;
int numElevens = 0;
int numTwelve = 0;//
int die1 = 0; // Dice values
int die2 = 0; // Dice values
int rollTotal = 0; // Sum of dice values
int idK = 0;
cout << "Enter number of rolls: " << endl;
cin >> numRolls;
srand(time(0));
while (numRolls >= 0){
if (numRolls >= 1) {
// Roll dice numRoll times
for (i = 0; i < numRolls; ++i) {
die1 = rand() % 6 + 1;
die2 = rand() % 6 + 1;
rollTotal = die1 + die2;
// Count number of sixs and sevens
if (rollTotal == 2){
numTwos = numTwos + 1;
}
else if (rollTotal == 3) {
numThrees = numThrees++;
}
else if (rollTotal == 4){
numFours = numFours++;
}
else if (rollTotal == 5){
numFives = numFives++;
}
else if (rollTotal == 6) {
numSixes = numSixes + 1;
}
else if (rollTotal == 7) {
numSevens = numSevens + 1;
}
else if (rollTotal == 8){
numEights = numEights++;
}
else if (rollTotal == 9){
numNines = numNines++;
}
else if (rollTotal == 10){
numTens = numTens++;
}
else if (rollTotal == 11){
numElevens = numElevens++;
}
else if (rollTotal == 12){
numTwelve = numTwelve++;
}
cout << endl << "Roll " << (i + 1) << " is "
<< rollTotal << " (" << die1
<< "+" << die2 << ")";
}
// Print statistics on dice rolls
cout << endl << endl;
cout << "Dice roll statistics:" << endl;
cout << "2s: " << numTwos << endl;
cout << "3s: " << numThrees << endl;
cout << "4s: " << numFours << endl;
cout << "5s: " << numFives << endl;
cout << "6s: " << numSixes << endl;
cout << "7s: " << numSevens << endl;
cout << "8s: " << numEights << endl;
cout << "9s: " << numNines << endl;
cout << "10s: " << numTens << endl;
cout << "11s: " << numElevens << endl;
cout << "12s: " << numTwelve << endl;
else {
cout << "Invalid rolls. Try again." << endl;
}
return 0;
}

Comments:

rand[10] does not exist - you made the array percentage too small. When you write to rand[10], you're stomping over something else.

That aside, the value 12 does come up. maybe you're not rolling the dice enough times.

counter[m]/j will not do what you want; when you divide an int by another int, the answer is an int. No decimal places. Just an int. You need to make the values float, or double; something that can handle decimal places.


// user input # of dice rolls
cin >> numberOfRolls;

// variable to hold number of rolls (for output)
j= numberOfRolls;

This is insane. You take a nice, easy to understand variable with a sensible name (numberOfRolls) and then you stop using it and use the value j instead. Might I suggest that you keep numberOfRolls and for the value you have that you deduct from in the loop, change that to something sensible like numberOfRollsRemaining
Edit & Run
. Sensible names for variables makes life so much easier.

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote