So I need a program that will say how many even, odd, and "0" numbers there are from a random number generator that generates a set of numbers. I have the generator working fine and i have done programs where a user inputs a set of numbers and it displays the odd and even numbers, but i can not figure out how to make one that actually counts the even and odd numbers and "0's" in a set of numbers from a random number generator. Here is my code for the generator. I feel like maybe there is something simple im missing or i should have done something different with my generator code to make this easier to do. Any help would be great. Thank you.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
#include <iostream>
#include <iomanip>
#include <cstdlib>
#include <ctime>
usingnamespace std;
int main()
{ printf ("The program will generate 5 numbers using a for loop");
srand((unsigned)time(0));
int random_integer;
for(int index=0; index<5; index++){
random_integer = (rand()%20)+2;
cout << random_integer << endl;
}
}
#include <iostream>
#include <cstdlib>
#include <ctime>
int main() {
constint N = 16'000'000 ;
std::cout << "The program will generate " << N << " random numbers using a for loop\n\n" ;
std::srand( (int)std::time(nullptr) ) ;
int n_even = 0 ;
int n_odd = 0 ;
int n_zero = 0 ;
constint ubound = 20 ;
for( int i=0; i<N; ++i ) {
constint random_integer = std::rand() % ubound ; // + 2 ;
// remove the +2 or we would never get any zeroes
if( random_integer == 0 ) ++n_zero ;
if( random_integer%2 == 0 ) ++n_even ; // note: zero is an even number
else ++n_odd ; // if it is not even, it must be odd
}
std::cout << "out of the " << N << " random integers, there were\n\t"
<< n_zero << " zeroes (mathematical expectation: " << N/ubound << ")\n\t"
<< n_even << " even numbers (mathematical expectation: " << N/2 << ")\n\t"
<< n_odd << " odd numbers (mathematical expectation: " << N/2 << ")\n" ;
}