Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Counting

Reply
Thread Tools

Counting

 
 
shell
Guest
Posts: n/a
 
      11-09-2003
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;
}
}


-------------------------------------------------------------------------------
 
Reply With Quote
 
 
 
 
Moonlit
Guest
Posts: n/a
 
      11-09-2003
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" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) 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;
> }
> }
>
>
> --------------------------------------------------------------------------

-----


 
Reply With Quote
 
 
 
 
osmium
Guest
Posts: n/a
 
      11-09-2003
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?


 
Reply With Quote
 
jbruno4000
Guest
Posts: n/a
 
      11-10-2003
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!





 
Reply With Quote
 
Karl Heinz Buchegger
Guest
Posts: n/a
 
      11-10-2003


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
http://www.velocityreviews.com/forums/(E-Mail Removed)
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
counting up instead of counting down edwardfredriks Javascript 6 09-07-2005 03:30 PM
Counting bits Steve VHDL 7 05-12-2004 05:42 AM
Re: tired of counting.... UDP socket bug Feroze [MSFT] ASP .Net 0 01-19-2004 02:42 AM
Re: tired of counting.... UDP socket bug Willy Denoyette [MVP] ASP .Net 0 01-13-2004 08:57 PM
Re: tired of counting.... UDP socket bug Ray Cassick \(Home\) ASP .Net 3 01-13-2004 06:53 PM



Advertisments