Trouble with array
Jun 17, 2008 at 9:24am UTC
Hello!
I have filled an array with 100 random numbers beetven 1 and 6. Now i want to count how many of each i have and print the result such as:
You have 23 of the number 1.
You have 12 of the number 2.
and so on.
But i can't get it to work.
Does anyone have an idea?
//ingemar
Jun 17, 2008 at 9:59am UTC
Post your code so people can see what you have done, then they can make suggestions as where to go from there.
Jun 17, 2008 at 2:30pm UTC
hello!
The problem are that i haven't got any idea how to solve this part. I have search the internet and in books after a way to count through an array after specifik values but i can't find anything.
this is how i fill the array:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
#include "stdafx.h"
#include "yatizw.cpp"
#include "Die.h"
int _tmain(int argc, _TCHAR* argv[])
{
int result;
int diceNr[100];
int insert;
for (int count = 0; count <=99; count++)
{
Die mydie1;
mydie1.rollDie();
result = mydie1.getDieValue();
diceNr[count] = result;
}
Jun 17, 2008 at 7:19pm UTC
Your best approach would be to use a map to achieve this.
e.g.
1 2 3 4 5 6 7 8 9 10 11
map<int , int > mCombinations;
for (int i = 0; i < 100; ++i) {
map[diceNr[i]]++;
}
map<int , int >::iterator mPtr = mCombinations.begin();
while (mPtr != mCombinations.end()) {
cout << "You have " << (*mPtr).second << " of the number " << (*mPtr).first << endl;
mPtr++;
}
That should be roughly in the playing field of what you require.
Topic archived. No new replies allowed.