I have a fully functional code that uses the random number generator to generate a set of random numbers. I'm trying to display "e" next to all the even numbers in the set, and then have a cout statement that tells the user how many even numbers there are in the set. Here's my code:

#include <iostream>
#include <iomanip>
#include <cstdlib>

using namespace std;

int main()
{

//I'm declaring my variables
int numValues1, cnt = 0;

//I'm declaring my symbolic constants.
#define MAX1 500
#define MAX_VAL1 5

//I'm getting the random number generator ready and using 30 as my seed value.
srand(30);

//I'm getting a "random" number between 1 and 500.
numValues1 = rand () % MAX1 + 1;
cout << "The for loop will generate " << numValues1 << " random values." << endl;   

//I'm using the for loop to execute my code numValues1 times to get that many values.
for (cnt = 1; cnt <= numValues1; cnt++) //cnt increases by 1. The loop will end once the value of cnt exceeds the value of numValues1.
{
    cout << setw (12) << right << rand ();

//I'm limiting the number of values per line to 5.
    if (cnt % MAX_VAL1 == 0)
    {
        cout << endl;
    }
}

return 0;
}

Thanks in advance!

Here's a quick rundown of a solution for you.

  • Assign the random number to a variable.
  • Check if the number is even or odd
  • If it is even increment a counter and print the e and the number
  • If not print the number
  • After the loop is finished print the counter.
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.