Answer to Question #287489 in C++ for allycat

Question #287489

Write a program using vectors that simulates the rolling of a single die a hundred times. The program should store 100 rolls of the die. After the program rolls the die, the program then goes through the 100 elements in the vector and tallies up the number of 1 rolls, the number of 2 rolls, the number of 3 rolls, the number of 4 rolls, the number of 5 rolls, and the number of 6 rolls. The program then displays the number of the respective rolls to the user.



1
Expert's answer
2022-01-14T04:36:12-0500
#include<iostream>
#include<vector>
#include<cstdlib>
#include<ctime>

using namespace std;

int main()
{
	enum dies{ONE=1,TWO,THREE,FOUR,FIVE,SIX};
	int counts[6]={0,0,0,0,0,0};
	vector<int>rol;
	srand(static_cast<unsigned int>(time(0)));
	for(int i=0;i<100;++i)
	{
		rol.push_back(rand()%6+1);
	}
	vector<int>::iterator it;
	for(it=rol.begin();it!=rol.end();++it)
	{
		if(*it==ONE)
			counts[0]++;
		else if(*it==TWO)
			counts[1]++;
		else if(*it==THREE)
			counts[2]++;
		else if(*it==FOUR)
			counts[3]++;
		else if(*it==FIVE)
			counts[4]++;
		else if(*it==SIX)
			counts[5]++;
	}
	for(int i=0;i<6;i++)
	{
		cout<<"\nNumber of the roll "<<i+1<<" is "<<counts[i];
	}
}

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