By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,171 Members | 1,004 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,171 IT Pros & Developers. It's quick & easy.

Counting

P: n/a
A little background regarding what I am doing:
I am supposed to write a small program that simulates rolling dice. I
randomly generated the number like I am supposed to, but then I am
supposed to count the number of times each number occurs(this is my
problem). I am also supposed to print in column for the number, the
max count, the min count, and the average count!!!

Could some one help me!!!

Here is the Code I have so far (I cannot use arrays on this project):

#include <iostream>
#include <stdlib.h>
#include <time.h>

using namespace std;

void dice (int & x, int count);

int main ()
{
int count=0;
int x;
dice(x,count);
}
//-------------------------------
void dice (int & x,int count)
{
do
{
srand((unsigned)time(NULL));
x= (rand () %6+1);
cout<< x;
count++;
}
while (count<0);
{
cout<< x;
}
}
-------------------------------------------------------------------------------
Jul 19 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
shell writes:
A little background regarding what I am doing:
I am supposed to write a small program that simulates rolling dice. I
randomly generated the number like I am supposed to, but then I am
supposed to count the number of times each number occurs(this is my
problem). I am also supposed to print in column for the number, the
max count, the min count, and the average count!!!

Could some one help me!!!

Here is the Code I have so far (I cannot use arrays on this project):


I can't think of any sensible way to do this without arrays. (Assuming that
includes array-like things, such as vectors). Perhaps this is an exercise
to build up a lust in your soul for arrays?
Jul 19 '05 #2

P: n/a
Hi,

#include <map>
using namespace std;

// init stuff
map<int> Count;
for( int Cnt = 0; Cnt < 6; ++Cnt ) map[ Cnt ] = 0;

// And use it
++Count[ Dice ];

// And print when ready
for( map<int>::const_iterator Walker = Count.begin(); Walker != Count.end();
++Walker )
{
count << "Number " << Walker->first << " rolled " << Walker->second << "
Times " << endl;
}

// Todo max min etc.

Regards, Ron AF Greve.
"shell" <eu*****@houston.rr.com> wrote in message
news:e7**************************@posting.google.c om...
A little background regarding what I am doing:
I am supposed to write a small program that simulates rolling dice. I
randomly generated the number like I am supposed to, but then I am
supposed to count the number of times each number occurs(this is my
problem). I am also supposed to print in column for the number, the
max count, the min count, and the average count!!!

Could some one help me!!!

Here is the Code I have so far (I cannot use arrays on this project):

#include <iostream>
#include <stdlib.h>
#include <time.h>

using namespace std;

void dice (int & x, int count);

int main ()
{
int count=0;
int x;
dice(x,count);
}
//-------------------------------
void dice (int & x,int count)
{
do
{
srand((unsigned)time(NULL));
x= (rand () %6+1);
cout<< x;
count++;
}
while (count<0);
{
cout<< x;
}
}
--------------------------------------------------------------------------

-----
Jul 19 '05 #3

P: n/a
One solution might be to declare 12 integer variables and increment the
appropriate variable via switch statement for each occurance of 'x':

int fisrt=0;, second=0, third=0, fourth=0 ... twelfth=0;

switch (x)
{
case 1: first++;
break;
case 2: second++;
break;
case 3: third++;
. . .

. . .
case 12: twelfth++;
break;
};

After processing each occurance of 'x' you'll have values stored for the
occurances of each number, min values, max values. The rest is a synch!

Jul 19 '05 #4

P: n/a


shell wrote:

A little background regarding what I am doing:
I am supposed to write a small program that simulates rolling dice. I
randomly generated the number like I am supposed to, but then I am
supposed to count the number of times each number occurs(this is my
problem). I am also supposed to print in column for the number, the
max count, the min count, and the average count!!!

Could some one help me!!!

Here is the Code I have so far (I cannot use arrays on this project):
If you cannot use arrays (or any other container), I guess you need to do:

int NrOfOne = 0;
int NrOfTwo = 0;
int NrOfThree = 0;
...

if( x == 1 )
NrOfOne++;

else if( x == 2 )
NrOfTwo++;

...


#include <iostream>
#include <stdlib.h>
#include <time.h>

using namespace std;

void dice (int & x, int count);

int main ()
{
int count=0;
int x;
dice(x,count);
}
//-------------------------------
void dice (int & x,int count)
{
do
{
srand((unsigned)time(NULL));
call srand only *once* at the beginning of your program!

x= (rand () %6+1);
cout<< x;
count++;
}
while (count<0);
{
cout<< x;
}
}

-------------------------------------------------------------------------------


--
Karl Heinz Buchegger
kb******@gascad.at
Jul 19 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.