Counting



 DEVELOP > c-Plus-Plus > Counting

LINK TO THIS PAGE  


rating :  0   |  0


  Page 1 of 1

1

 
Topic: DEVELOP > c-Plus-Plus
User: "shell"
Date: 09 Nov 2003 10:49:14 AM
Object: Counting
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;
}
}
-------------------------------------------------------------------------------
.

User: "Moonlit"

Title: Re: Counting 09 Nov 2003 12:40:54 PM
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" <eunsinn@houston.rr.com> wrote in message
news:e7e929df.0311090849.6af30a5f@posting.google.com...

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;
}
}


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

-----
.

User: "jbruno4000"

Title: Re: Counting 09 Nov 2003 09:05:49 PM
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!
.

User: "osmium"

Title: Re: Counting 09 Nov 2003 01:37:46 PM
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?
.

User: "Karl Heinz Buchegger"

Title: Re: Counting 10 Nov 2003 03:41:08 AM
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
kbuchegg@gascad.at
.


  Page 1 of 1

1

 


Related Articles
 

NEWER

pg.1232     pg.940     pg.716     pg.544     pg.412     pg.311     pg.234     pg.175     pg.130     pg.96     pg.70     pg.50     pg.35     pg.24     pg.16     pg.10     pg.6     pg.3     pg.1

OLDER