Answer to Question #247441 in C++ for HelloWorld

Question #247441

Write a program that will prompt the user to input the following:

 

1. The number of dice to be rolled.

2. How many times the dice should be rolled?

Since the values above will vary, you must use pointer arrays and dynamically allocate memory.

The program will store the total of faces per roll and the values will be later used to plot a graph. The graph should look like the output shown below

3:

4:

5:

6:

7:

8: *

9:

10:

11:

12: *

13:

14:

15:

16:

17:

18:

 

The asterisk (*) tallies the number of times that total has occurred. 3 and 18 are the minimum and maximum totals, respectively, for rolling 3 dice.

The declaration and implementation sections of the class have been defined for you. Use it to complete your program.




1
Expert's answer
2021-10-06T06:17:51-0400
#include <stdio.h>
#include <stdlib.h>
void printChars(int c, int num)
{
	for(int i = 0; i < num; i++)
		printf("%c", c);
}
int main()
{
	int dice1, dice2;
	int arr[13];


	for(int i = 2; i <= 12; i = i + 1)
		arr[i] = 0;


	for(int i = 0; i < 100; i = i + 1)
		{
		dice1 = rand() % 6 + 1;
		dice2 = rand() % 6 + 1;
		arr[dice1 + dice2] = arr[dice1 + dice2] + 1;
		}


	for(int i = 2; i <= 12; i = i + 1)
		{
		printf("%d: %d\t", i, arr[i]);
		printChars('*', arr[i]);
		printf("\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
APPROVED BY CLIENTS