Question: C++ (do not use arrays) Program 4 Loops, Decision Statements, and Symbolic Constants (100 points) Due: Friday, February 15 by 11:59 PM Overview For this

C++ (do not use arrays)

Program 4 Loops, Decision Statements, and Symbolic Constants (100 points)

Due: Friday, February 15 by 11:59 PM

Overview

For this assignment, write a program that will calculate statistics for small groups of random numbers.

For the groups of numbers, calculate the following:

  • the smallest value in a group
  • the largest value in a group
  • the sum of all of the values in a group
  • the average of all of the values in a 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

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:

srand(time(0));

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

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() % 8;

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

num = rand() % 8 + 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));

Note for Mac users: the sequence of random numbers that is generated on a Mac will likely be different than those generated on a Windows machine. This means that the output produced by a program running on a Mac will likely be different than the output produced by a program running on Windows.

Basic Program Logic

Seed the random number generator with a value of 24. Note: other seed values may be used to produce different results. However, the version that is handed in for grading MUST use a seed value of 24.

Generate a random number between 2 and 10. 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 1 and 50, display the random number, and perform the calculations that are necessary to calculate the statistics.

After the for loop has finished executing, display the four statistics with appropriate labels. Any value with a decimal point should be displayed with exactly 2 digits after the decimal point. All of the statistical values should be displayed with the last digits lining up.

Now, generate a random number between 2 and 10. This will be the number of values in the second group. Display the number of values in the group with an appropriate label.

Using a while loop that executes exactly "number of values in the second group" number of times, generate a random number between 1 and 50, display the random number, and perform the calculations that are necessary to calculate the statistics.

After the while loop has finished executing, display the four statistics with appropriate labels. Any value with a decimal point should be displayed with exactly 2 digits after the decimal point. All of the statistical values should be displayed with the last digits lining up.

Finally, generate a random number between 2 and 10. This will be the number of values in the third group. Display the number of values in the group with an appropriate label.

Using a do while loop that executes exactly "number of values in the third group" number of times, generate a random number between 1 and 50, display the random number, and perform the calculations that are necessary to calculate the statistics.

After the do while loop has finished executing, display the four statistics with appropriate labels. Any value with a decimal point should be displayed with exactly 2 digits after the decimal point. All of the statistical values should be displayed with the last digits lining up.

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 10.

The other constant is for the maximum value for the numbers to be processed. The maximum value is 50.

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. Make sure to actually use the symbolic constants that are created.
  3. To use the random number generator, add #include at the beginning of the program
  4. Make sure that the copy of the program that is handed in uses srand(24); to set the seed value for the random number generator.
  5. As mentioned above, any value with a decimal point should be displayed with exactly 2 digits after the decimal point, including zeros.
  6. Hand in a copy of the source code (the CPP file) using Blackboard.

Extra Credit

For up to 5 points of extra credit, add code that will calculate and display a 5th statistic - standard deviation - for each of the groups.

Calculating Standard Deviation

The standard deviation is calculated as follows:

where

  • ( x2 ) is the sum of the numbers squared
  • ( x)2 is the square of the sum of the numbers
  • n is the number of values

For example, if there are 4 numbers:

7 3 4 5

then

  • ( x2 ) is equal to

(7 * 7) + (3 * 3) + (4 * 4) + (5 * 5)

  • ( x)2 is equal to

(7 + 3 + 4 + 5) * (7 + 3 + 4 + 5)

  • n is equal to 4
(X2) standard deviation n1

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!