CSCI240 Program 4 Loops, Decision Statements, and Symbolic Constants solution

$25.00

Original Work ?
Category:

Description

5/5 - (4 votes)

For this assignment, write a program that will count specific types of values from three small groups of random numbers.

For each group of numbers, count the following:

  • the number of even values in the group
  • the number of odd values in the group
  • the number of zeroes in the group

Random Number Generation

In the first three programs, the user has been asked for input. This program will be different. Rather than asking the user how many values are in a group of numbers or even what the values are, the random number generator will be used to determine the size of a group and the actual values in the group.

To use the random number generator, first add a #include statement for the cstdlib library to the top of the program:

#include <cstdlib>

Next, initialize the random number generator. This is done by calling the srand function and passing in an integer value (known as a seed value). This should only be done ONE time and it MUST be done before actually getting a random number. A value of 1 (or any integer literal) will generate the same sequence of “random” numbers every time the program is executed. This can be useful for debugging:

srand(1);

To get a different series of random numbers each time the program is run, the actual time that the program is run can be passed as the seed value for the random number generator. This is done as follows:

srand(time(0));

If the time function is used, make sure to include the ctime library as well.

Note: pick one of the srand instructions for the program. Do not use both!

Finally, generate a random number by calling the rand function:

num = rand();

The above line of C++ code will generate a “random” integer between 0 and RAND_MAX and saves the value in an integer variable named num. RAND_MAX is a pre-defined constant that is equal to the maximum possible random number. It is implementation dependent but is guaranteed to be at least 32,767.

Modulus division can be used to restrict the “random” integer to a smaller range:

num = rand() % 7;

will produce a value between 0 and 6. To change the range to 1 through 7, simply add 1:

num = rand() % 7 + 1;

To get random values that are within a specified range that starts at a value other than 0 or 1:

num = minimum_value + (rand() % (maximum_value - minimum_value + 1));

So, to get values within the range 3 – 17:

num = 3 + (rand() % (17 - 3 + 1));

Basic Program Logic

Generate a random number between 2 and 20. This will be the number of values in the first group. Display the number of values in the group with an appropriate label.

In a for loop that executes exactly “number of values in the first group” number of times, generate a random number between 0 and 99, display the random number, and determine whether the number is even, odd, or zero, and increment the appropriate counter.

After the for loop has finished executing, display the three counters for the first group with appropriate labels.

Generate another random number between 2 and 20. This will be the number of values in the second group. Display the number of values in the group with an appropriate label.

In a while loop that executes exactly “number of values in the second group” number of times, generate a random number between 0 and 99, display the random number, and determine whether the number is even, odd, or zero, and increment the appropriate counter.

After the while loop has finished executing, display the three counters for the second group with appropriate labels.

Generate another random number between 2 and 20. This will be the number of values in the third group. Display the number of values in the group with an appropriate label.

In a do while loop that executes exactly “number of values in the third group” number of times, generate a random number between 0 and 99, display the random number, and determine whether the number is even, odd, or zero, and increment the appropriate counter.

After the do while loop has finished executing, display the three counters for the third group with appropriate labels.

Symbolic Constants

This program MUST use at least 3 symbolic constants.

Two of the constants should be for the minimum and maximum values for the size of the groups of numbers. The minimum group size is 2. The maximum group size is 20.

The other constant is for the maximum value for the range of numbers to be processed. The maximum value is 99. However, this constant should have the value 100 because of the formula that is used to generate the random numbers between 0 and 99.

More symbolic constants may be added to the code if necessary.

Program Requirements

  1. Include line documentation. There is no need to document every single line, but logical “chunks” of code should be preceded by a line or two that describes what the “chunk” of code does. This will also be a part of every program that is submitted for the remainder of the semester.
  2. To use the random number generator, add two additional #includes: #include <ctime> and #include <cstdlib> at the beginning of the program
  3. The program MUST use the 3 symbolic constants that were described above.
  4. Make sure that the copy of the program that is handed in uses srand(15) to set the seed value for the random number generator.
  5. Hand in a copy of your source code (the CPP file) using Blackboard.

Output

Run 1 (using srand(1))

The program will generate 5 numbers using a for loop.
  67  34   0  69  24

There were 2 even numbers, 2 odd numbers, and 1 zeroes in the group of numbers



The program will now generate 4 numbers using a while loop.
  58  62  64   5

There were 3 even numbers, 1 odd numbers, and 0 zeroes in the group of numbers



The program will now generate 8 numbers using a do while loop.
  81  27  61  91  95  42  27  36

There were 2 even numbers, 6 odd numbers, and 0 zeroes in the group of numbers

Run 2 (using srand(15))

The program will generate 13 numbers using a for loop.
   5  56  38   1  62  84  44  48  27  35  61  45   0

There were 6 even numbers, 6 odd numbers, and 1 zeroes in the group of numbers



The program will now generate 16 numbers using a while loop.
  48  98  51  57   2  68  61   2  53  26  65  29   0  47  74  92

There were 8 even numbers, 7 odd numbers, and 1 zeroes in the group of numbers



The program will now generate 7 numbers using a do while loop.
  98  57  14  70  80  28   5

There were 5 even numbers, 2 odd numbers, and 0 zeroes in the group of numbers

Run 3

The program will generate 20 numbers using a for loop.
  16  89  26   3  59  19   8  34   1   3  56   0  32  96   3  67  42  19  85  75


There were 8 even numbers, 11 odd numbers, and 1 zeroes in the group of numbers



The program will now generate 18 numbers using a while loop.
  46  98  73  21  52  94  91   2  99   6  10  93   2  64  71  72  21   1

There were 10 even numbers, 8 odd numbers, and 0 zeroes in the group of numbers



The program will now generate 16 numbers using a do while loop.
  81  21  24  28  14  64  42  26  75   4  28  42  84  43  52  34

There were 12 even numbers, 4 odd numbers, and 0 zeroes in the group of numbers

Extra Credit

For up to 5 points of extra credit, add code that will calculate the total number of values that were processed from all three groups, total number of even values, total number of odd values, total number of zeroes; and display all of those calculated values along with a bar graph for each value.

The bar graph for each counter should be made up of asterisks, and the number of asterisks should correspond to the value of the counter. So if there were 7 even numbers, there should be 7 astericks.

Note about extra credit: the points will ONLY be awarded if the required portions of the assignment work correctly. In other words, don’t take short cuts in the rest of the program because it is assumed that 5 extra points will be awarded.

Extra Credit Output (using srand(15);)

The program will generate 13 numbers using a for loop.
   5  56  38   1  62  84  44  48  27  35  61  45   0

There were 6 even numbers, 6 odd numbers, and 1 zeroes in the group of numbers



The program will now generate 16 numbers using a while loop.
  48  98  51  57   2  68  61   2  53  26  65  29   0  47  74  92

There were 8 even numbers, 7 odd numbers, and 1 zeroes in the group of numbers



The program will now generate 7 numbers using a do while loop.
  98  57  14  70  80  28   5

There were 5 even numbers, 2 odd numbers, and 0 zeroes in the group of numbers



Overall, there were 36 numbers.
   19 were even  *******************
   15 were odd   ***************
    2 were zero  **