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
|
//PREPROCESSING DIRECTIVES
#include <iostream> ///Standard Input / Output Streams Library (i.e cout, cin, etc).
#include <cstdlib> ///C Standard General Utilities Library
#include <ctime> ///Convert time_t value to string
using namespace std; ///Using the 'std' namespace for C++ Standard Library features.
int main() ///Initiates function declaration 'main'.
{
///VARIABLE DECLARATIONS
srand(time(0)); ///Generates a random number based on time(0)
char goagain; ///Defines a character variable 'goagain'.
do { ///Begins a do-while loop.
int nums[6] {}; ///Defines an integer array of 6 indexes, set to 0.
int num_rolls; ///Defines an integer for the number of rolls.
cout << "How many rolls: "; ///Prompts user to enter number of rolls.
cin >> num_rolls; ///Reads in a single integer, into num_rolls from user.
// Roll the dice and collect the counts.
// Begins a for loop. Syntax (for (statement 1; statement 2; statement 3)
// Statement 1 is executed once, before the code. Statement 2 is the condition that the code
// will execute. Statement 3 is executed after the code has been executed.
// Loop initiates 'i'. Code will run as long as i is less than 'num_rolls' with 'i' increasing
// by one (++i) after each loop.
for (int i = 0; i < num_rolls; ++i)
{
int roll = rand() % 6 + 1; //Defines roll to be the modulus of a random number and 6, +1.
cout << "You rolled a " << roll << '\n'; //Outputs roll to the user.
++nums[roll - 1]; //Increments a value in the roll array by 1, position incremented is the
//value of whatever was just rolled, minus 1.
} //End for loop.
// Print the results.
for (int i = 0; i < 6; ++i) //S1: initiate i to 0, S2: while i is less than 6, S3: increment i.
cout << i + 1 << " - " << nums[i] << '\n'; //Ouputs a number, -, whats in the first array position.
// Determine the highest count and whether it occurs more than once.
int high = 0; //Sets a integer high, to 0. If you are in going initiating variables, you should be consistent.
bool multiple = false; //Sets a boolean variable, of a match, to false.
for (int i = 1; i < 6; ++i) //S1: initiate i to 0; S2, while i is less than 6, S3: increment i.
{
if (nums[i] >= nums[high]) //Check if the value in array position i is great/equal to array position 'high'
{
if (nums[i] > nums[high]) //If value in array position is greater, high because array position.
{
high = i; //Assign high the value of the current loop iteration.
multiple = false; //Boolean is still false because its not equal.
} //End if check 2.
else
multiple = true; //Boolean is true because its equal, a match.
} //End if check 1.
} //End for loop.
if (multiple) //If the multiple boolean is true, from above.
cout << "I am not sure what you should vote for.\n"; //Output to user.
else //If the multiple boolean is false, from above.
cout << "You should vote for " << high + 1 //Output to user.
<< ", because it rolled " << nums[high] << " times.\n";
cout << "\nGo again? y/n "; //Output to user.
cin >> goagain; //Read in from user.
} //End do-while loop.
while (goagain == 'y'); //Repeat if the user selected y.
} //End main.
|