473,396 Members | 2,098 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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;
}
}
-------------------------------------------------------------------------------
Jul 19 '05 #1
4 2291
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
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
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


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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Elbert Lev | last post by:
Please correct me if I'm wrong. Python (as I understand) uses reference counting to determine when to delete the object. As soon as the object goes out of the scope it is deleted. Python does...
1
by: ash | last post by:
hi does anyone has any experience with flyweight pattern with refernce counting i want to share objects between multiple clients and want to delete the object from shared pool when the last...
1
by: Tony Johansson | last post by:
Hello Experts! I reading a book called programming with design pattern revealed by Tomasz Muldner and here I read something that I don't understand completely. It says "A garbarage...
1
by: Tony Johansson | last post by:
Hello Experts! I reading a book called programming with design pattern revealed by Tomasz Muldner and here I read something that I don't understand completely. It says "A garbarage...
18
by: ChadDiesel | last post by:
I appreciate the help on this group. I know I've posted a lot here the last couple of weeks, but I was thrown into a database project at my work with very little Access experience. No other...
7
by: zets | last post by:
I need a macro for counting the bits in the odd positions of a given input (of any type, char, pointer, int, struct, whatever). Is there any clever way I could not think of, to do it efficiently? ...
1
by: j | last post by:
Hi, I've been trying to do line/character counts on documents that are being uploaded. As well as the "counting" I also have to remove certain sections from the file. So, firstly I was working...
4
by: aaronfude | last post by:
Hi, Please consider the following class (it's not really my class, but it's a good example for my question): class Vector { int myN; double *myX; Vector(int n) : myN(n), myX(new double) { }...
1
by: oec.deepak | last post by:
Hi Cn any one telll me what is Reference counting in C++.
3
by: nitric | last post by:
hey guys, i'm really stuck on this program. It's basically a survey and I have to ask people what drinks they like. 1-4, coffee tea oj and lemonade. i'm having trouble counting the TOTAL NUMBER...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.