1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
|
#include <iostream>
using namespace std;
const int NUM_COUNT = 10; // how many numbers do we want to generate.
// function declarations
void generateData(int data1[], int cnt);
void countValue(int data1[], int data2[], int cnt);
int main()
{
// our arrays
int genNumbers[NUM_COUNT]; // set array to hold numbers
int numCount[6] = {0,0,0,0,0,0}; // serves as a storage for the 6 possible numbers.
int dice;
// build the array with numbers between 1 and 6
generateData(genNumbers, NUM_COUNT);
// Display numbers generated - for debugging if you want to check :)
for (int i = 0; i < NUM_COUNT; i++)
cout << "Number " << genNumbers[i] << endl;
cout << endl << endl;
// now lets count those numbers generated...
countValue(genNumbers, numCount, NUM_COUNT);
// at this point the numCount array has 6 counters stored
// for the 6 possible numbers. Array indexes start at 0
// therefore... numCount[0]..numCount[1]..numCount[2] etc..
// Because we have numbers between 1 to 6 we subtracted 1
// from the index (generated by the rand) so that we would
// correctly land in index 0.. with that it mind, 1 - 6
// is actually 0 - 5 in the array.
// lets check this...
for (int i = 0; i < 6; i++)
cout << "Number " << i + 1 << " came out " << numCount[i] << " times." << endl;
return 0;
}
//
// $function: Write a batch of numbers betwwen 1 and 6 to an array
//
void generateData(int data1[], int cnt)
{
// data1 is the array of numbers passed to the function.
int dice;
for (int count = 0; count < cnt; count++)
{
dice = rand() % 6 + 1;
data1[count] = dice;
}
}
//
// $function: Check the number of times a given value has generated
//
void countValue(int data1[], int data2[], int cnt)
{
// data1 is the array of numbers passed to the function.
// data2 is the array holding the total number for each number
int numValue;
for (int count = 0; count < cnt; count++)
{
// grab the value from the numbers array and use that
// as an index to our total number array.
numValue = data1[count]; // get the number generated at that position
data2[numValue - 1]++; // array indexes start at 0, our number is between 1 and 6
// so we subtract one so we are indexed correctly.
}
}
|