Counting

Discussion in 'C++' started by shell, Nov 9, 2003.

  1. shell

    shell Guest

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


    -------------------------------------------------------------------------------
     
    shell, Nov 9, 2003
    #1
    1. Advertising

  2. shell

    Moonlit Guest

    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" <> wrote in message
    news:...
    > 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;
    > }
    > }
    >
    >
    > --------------------------------------------------------------------------

    -----
     
    Moonlit, Nov 9, 2003
    #2
    1. Advertising

  3. shell

    osmium Guest

    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?
     
    osmium, Nov 9, 2003
    #3
  4. shell

    jbruno4000 Guest

    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!
     
    jbruno4000, Nov 10, 2003
    #4
  5. 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
     
    Karl Heinz Buchegger, Nov 10, 2003
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Ray Cassick \(Home\)

    Re: tired of counting.... UDP socket bug

    Ray Cassick \(Home\), Jan 13, 2004, in forum: ASP .Net
    Replies:
    3
    Views:
    583
    mikeb
    Jan 13, 2004
  2. Willy Denoyette [MVP]

    Re: tired of counting.... UDP socket bug

    Willy Denoyette [MVP], Jan 13, 2004, in forum: ASP .Net
    Replies:
    0
    Views:
    496
    Willy Denoyette [MVP]
    Jan 13, 2004
  3. Feroze [MSFT]

    Re: tired of counting.... UDP socket bug

    Feroze [MSFT], Jan 19, 2004, in forum: ASP .Net
    Replies:
    0
    Views:
    367
    Feroze [MSFT]
    Jan 19, 2004
  4. Steve

    Counting bits

    Steve, May 6, 2004, in forum: VHDL
    Replies:
    7
    Views:
    6,061
    Charles Bailey
    May 12, 2004
  5. edwardfredriks

    counting up instead of counting down

    edwardfredriks, Sep 6, 2005, in forum: Javascript
    Replies:
    6
    Views:
    207
    Dr John Stockton
    Sep 7, 2005
Loading...

Share This Page