Graphical representation of numbers using asterisks

First of all, I'm completely new to C++, having had no prior coding experience using the language. I'm slightly stumped and would appreciate a steer in the right direction.

We've been given the problem of writing a program to simulate the outcome of two random dice throws, which runs for 10000 throws. I've nailed that part, tested it and it functions as intended. The twist is that the values need to be displayed graphically, using asterisks. So instead of;
The dice rolls equalled a total of 2: 200 times
The dice rolls equalled a total of 3: 300 times
The dice rolls equalled a total of 4: 550 times
The dice rolls equalled a total of 5: 1000 times
etc,

It must be displayed as;
The dice rolls equalled a total of 2: ** times
The dice rolls equalled a total of 3: *** times
The dice rolls equalled a total of 4: ****** times

Where each '*' denotes, for example, 100 throws, and the total number of asterisks corresponds to the nearest multiple of 100.

I've tried using strings and for loops, to no avail. I've done a little research and it seems that using an array to store the value for each possible dice outcome would work best, but I'm uncertain of how to implement this.

Here is what I have so far. Thanks in advance for any advice that can be offered.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
  #include <iostream>
#include <cstdlib>
#include <ctime>
#include <string>

using namespace std;

int main()
{
	int count[13];
	for (int i = 1; i < 13; i++)
		count[i] = 0;
	srand(static_cast<unsigned>(time(0)));
	for (int i = 1; i <= 10000; i++)
	{
		int j = rand() % 6 + 1;
		int k = rand() % 6 + 1;
		count[j + k] = count[j + k] + 1;
	}
	for (int i = 1; i < 13; i++)
		cout << "The dice rolls equalled a total of " << i << ": " << count[i]
		<< " times" << endl;

   return 0;
}
Last edited on
You could write a function that prints a character a certain number of times.
1
2
3
4
void printChar(char c, unsigned numTimes = 1)
{
    for(int i = 1; i <= numTimes; ++i) std::cout << c;
}
Then, call that function after doing some math on your count number in order to get your target number.
1
2
3
cout << "The dice rolls equalled a total of " << i << ": ";
printChar('*', (count[i] + 50) / 100);
cout << " times" << endl;
Thanks for the reply. The only thing I don't recognise from your suggestion is the 'void' function, which I don't believe we've encountered yet and thus would not be allowed to use in a solution to the problem. Other than that however, I'll see if I can make it work.
Still having trouble with this unfortunately. I tried to find a way around using the void function, instead using a nestled 'for' loop I used in another program that printed values as asterisks, but I couldn't seem to get my head around using it in this instance. If anyone has any further suggestions they would be most welcome.
std::string has a constructor allowing you to specify a character and a number, like this:
1
2
3
4
5
std::cout << "Four asterisks: " << std::string(4, '*') << std::endl;
std::cout << "Nine asterisks: " << std::string(9, '*') << std::endl;
//
std::cout << "The dice rolls equalled a total of " << i << " "
          << std::string((count[i] + 50) / 100, '*') << " times\n";
Thanks! That was exactly what I was looking for. Can't believe it was so seemingly simple.
Topic archived. No new replies allowed.