Explain each line of code

A user here answered a different question of mine using the code below, but since I am 3 days into learning C++, I don't quite understand some of the concepts in the program. If you can add a comment next to each line inside of int main() explaining what its doing, I would really appreciate it. Thanks in advance!

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
  #include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;

int main()
{
    srand(time(0));
    char goagain;
    do {
        int nums[6] {}; // the empty braces init the array to zeros.

        int num_rolls;
        cout << "How many rolls: ";
        cin >> num_rolls;

        // Roll the dice and collect the counts.
        for (int i = 0; i < num_rolls; ++i)
        {
            int roll = rand() % 6 + 1;
            cout << "You rolled a " << roll << '\n';
            ++nums[roll - 1];
        }

        // Print the results.
        for (int i = 0; i < 6; ++i)
            cout << i + 1 << " - " << nums[i] << '\n';

        // Determine the highest count and whether it occurs more than once.
        int high = 0;
        bool multiple = false;
        for (int i = 1; i < 6; ++i)
        {
            if (nums[i] >= nums[high])
            {
                if (nums[i] > nums[high])
                {
                    high = i;
                    multiple = false;
                }
                else
                    multiple = true;
            }
        }

        if (multiple)
            cout << "I am not sure what you should vote for.\n";
        else
            cout << "You should vote for " << high + 1
                 << ", because it rolled " << nums[high] << " times.\n";

        cout << "\nGo again? y/n ";
        cin >> goagain;
    }
    while (goagain == 'y');
}
I would focus on
for (int i = 0; i < num_rolls; ++i) //loop
as that loop does something interesting, the rest of the code is more or less
"my second c++ program" stuff -- cin, cout, loop stuff.

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
#include <iostream> //indent off.  
#include <cstdlib>
#include <ctime>
using namespace std;  //bad practice, learn to using std::cout etc for only what you use or 
//put std:: on everything.

int main()
{
    srand(time(0)); //use C code to initialize a random number generator. this should be using<random> instead. 
    char goagain;  //create a character that looks like, from its name, should be a bool. 
//its used correctly, just a very minor nitpick on the name. 
    do {  //start a  do - while loop.  these loop types execute one time no matter what. 
        int nums[6] {}; // the empty braces init the array to zeros.  

        int num_rolls;   //make a variable
        cout << "How many rolls: ";  //tell user to put data in
        cin >> num_rolls;   //get that data from user

        // Roll the dice and collect the counts.  
        for (int i = 0; i < num_rolls; ++i)  //loop for the value that the user put in.  
//this is in the other loop.   
        {
            int roll = rand() % 6 + 1; //create a random number from 1 to 6 (%6 is 0 to 5, +1)
               //using C code.  ^^
            cout << "You rolled a " << roll << '\n'; //show the number you got from random. 
            ++nums[roll - 1]; //cryptic way to do a counting sort  or bucket sort algorithm 
//the idea is that you have a counter for each possible value
// (here, 0-5 represents 1-6, should have just made the array support 0-6 instead) and
// increment as you get them, so if you have 
//5 rolls of the value 3, then bucket[2] (3-1) has the value of 5, telling you what you needed to know. 
        }

        // Print the results.
        for (int i = 0; i < 6; ++i)    //loop to show the number of rolls for each value, as explained above. 
            cout << i + 1 << " - " << nums[i] << '\n'; 

        // Determine the highest count and whether it occurs more than once.
        int high = 0;     
        bool multiple = false;
        for (int i = 1; i < 6; ++i)  //find the index for the highest count of rolls of the same value. 
        {                   //this is overly complex for what it does and could have been rolled into the
//loop above.   the explicit overly complex logic detects if you have more than one high count
// eg 5 3s and 5 2s. 
           if (nums[i] >= nums[high])
            {
                if (nums[i] > nums[high])
                {
                    high = i;
                    multiple = false;
                }
                else
                    multiple = true;
            }
        }

//output to the user..
        if (multiple)
            cout << "I am not sure what you should vote for.\n";
        else
            cout << "You should vote for " << high + 1
                 << ", because it rolled " << nums[high] << " times.\n";

//ask if want to do this again
        cout << "\nGo again? y/n ";
        cin >> goagain;
    }
    while (goagain == 'y'); // as long as the user wants to keep going. 
}
Last edited on
A user here answered a different question of mine using the code below, but since I am 3 days into learning C++, I don't quite understand some of the concepts in the program.

Shouldn't you ask in that thread http://www.cplusplus.com/forum/beginner/268579/ from that user what the code means?
Comments added.
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. 

Last edited on
Topic archived. No new replies allowed.