Cofee1sY0rFrend
February 25th, 2003, 12:48 PM
I need help coding a simple program that generates 10,000 integers in the range of 0 to 9 and display a list of counts for each number at the end. Since there is a 1 in 10 chance for each number, there should be about 1000 of each when done.
I would like help in making this program with as very little code as possible.
Federal102
February 25th, 2003, 02:48 PM
#include<iostream>
#include<vector>
#include <algorithm>
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
srand((unsigned)time( NULL ));
vector<int> v;
int counter = 1;
for(int i = 0; i < 10000; ++i)
v.push_back(rand() % 10);
sort(v.begin(), v.end());
int p = 0;
vector< pair < int, int > > vPair;
while(p != v.size())
{
if(v[p] == v[p+1] && (p + 1) < v.size())
{
counter++;
++p;
continue;
}
vPair.push_back(make_pair<int, int>(v[p], counter));
counter = 1;
++p;
}
for(int q = 0; q < vPair.size(); ++q)
cout << "Count for " << vPair[q].first << " is " << vPair[q].second << endl;
return 0;
}