Alright, let's get your code compiling and work on the logic needed to count the even and odd numbers generated by calling rand()
.
Error Preventing Your Code From Compiling
- unmatched brace following
for
loop declaration, e.g. for(int i=1; i<=6; i++){
(you have no closing '}'
.
Logic Issues Preventing Your Code From Working
- you have no variables to hold the number of even and number of odd values generated by
rand()
.
Issue You Should Address
- your loop iterates from
i=1; i<=6
in order to loop five-times. While this captures the correct count, it shows a fundamental misunderstanding of indexing in C/C++ and it one of the most common errors when using the loop variable with an array. In C/C++ all arrays (or allocated collections of objects) are zero-indexed. That means valid indexes for a five-element array are 0-4
inclusive. In order to loop five-times and not index outside of your array bounds a proper for
loop declaration would be for (int i = 0; i < 5; i++) { ... }
. Being consistent with your loop limits will help you avoid problems later,
- while fine for short example problems, see Why is “using namespace std;” considered bad practice?,
- see also C++: “std::endl” vs “\n” and be aware of the differences.
- avoid using MagicNumbers. If you need a constant,
#define
one or more, or since this is C++ use constexpr
(coming soon to C in C23 in a limited manner). You have 5
, 10
, 11
-- huh? What are those? Give those constants names. (and in doing so, you provide one convenient location to make changes to the values at the top of your code...)
Putting it altogether, to find the number of even and odd values generated by rand()
, you could do (adding the the comments above), something similar to:
#include <iostream>
#include <iomanip>
#include <cstdlib>
int main() {
int odd = 0, /* vars to track no. of odd / even numbers generated */
even = 0;
constexpr int nvals = 5, /* number of values to generate */
rngmin = 10, /* range minimum for distribution */
range = 11; /* number of values in range */
srand((unsigned)time(NULL)); /* seed random number generator */
for (int i = 0; i < nvals; i++) { /* loop 5 times, 0 <= i < 5 */
/* get random number within range */
int random = rngmin + (rand() % range);
std::cout << std::setw(2) << random; /* output value - 2-digts */
if (random & 1) { /* if odd */
odd += 1; /* increment odd */
std::cout << " : odd\n"; /* output odd and newline */
}
else { /* otherwise */
even += 1; /* increment even */
std::cout << " : even\n"; /* output even and newline */
}
}
/* output count of odd and even numbers generated */
std::cout << "\nodd : " << odd << "\neven : " << even << '\n';
}
(note: as discussed in the comments, random & 1
is the same as random % 2 != 0
-- use whichever you choose. The bitwise AND simply avoids a potential divide inherent in computing modulo. It's an academic difference)
Example Compile String
Always compile with full warnings enabled, and do not accept code until it compiles without warning.
Compiling with g++
you could do:
$ g++ -Wall -Wextra -pedantic -Wshadow -Werror -std=c++17 -Ofast -o rand-count-odd-even-c rand-count-odd-even-c.cpp
For the Microsoft compiler, using /W3
is sufficient to enable full warnings and /Wx
will cause any warnings to be treated as errors.
Example Use/Output
$ ./rand-count-odd-even-c
13 : odd
16 : even
13 : odd
13 : odd
12 : even
odd : 3
even : 2
C++ Provides Pseudo-random number generation
See Pseudo-random number generation.
To do the same thing using the C++ psuedo random number generation library, simply choose which random-number generation engine you want to use (the std::mt19937
Mersenne Twister engine is a common choice) and use that engine to generate numbers within a std::uniform_int_distribution. The rest is the same except that 10
values are generated instead of 5
, e.g.
#include <iostream>
#include <iomanip>
#include <random>
int main()
{
int odd = 0, /* variables to track no. of odd/even numbers */
even = 0;
constexpr int nvals = 10, /* number of values to generate */
rngmin = 10, /* range minimum for distribution */
rngmax = 20; /* range maximum for distribution */
/* create random device and mersenne twister engine */
std::random_device rd;
std::mt19937 mt (rd());
/* create a uniform_int_distribution between rngmin and rngmax */
std::uniform_int_distribution<int> uniform_dist(rngmin, rngmax);
for (int i = 0; i < nvals; i++) { /* loop nvals times */
int rand = uniform_dist(mt); /* get random in range */
std::cout << std::setw(2) << rand; /* output value 2-digts */
if (rand & 1) { /* if odd */
odd += 1; /* increment odd */
std::cout << " : odd\n"; /* output odd and newline */
}
else { /* otherwise */
even += 1; /* increment even */
std::cout << " : even\n"; /* output even and newline */
}
}
/* output count of odd and even numbers generated */
std::cout << "\nodd : " << odd << "\neven : " << even << '\n';
}
(compile the same)
Example Use/Output
$ ./rand-count-odd-even
20 : even
11 : odd
14 : even
14 : even
14 : even
16 : even
19 : odd
16 : even
13 : odd
19 : odd
odd : 4
even : 6
Look both examples over and hopefully they will get you started. Bookmark cppreference.com and use it as your reference. See: The Definitive C++ Book Guide and List to pick up a good C++ book that will be needed given the C approach to the language you are starting off with.
Let me know if you have questions.