Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Overview in C++ For this assignment, write a program that will calculate statistics for small groups of random numbers. For the groups of numbers, calculate

Overview in C++

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)); 

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. Make sure to actually use the symbolic constants that are created.

  2. To use the random number generator, add #include at the beginning of the program

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

  4. As mentioned above, any value with a decimal point should be displayed with exactly 2 digits after the decimal point, including zeros.

image text in transcribed

Output Run using srand(24); on Windows PC The for loop will generate 10 numbers The numbers are 38 45 1 30 35 29 18 13 31 4 Smallest: Largest: Sum: Average: 45 244 24.40 The while loop will generate 9 numbers The numbers are 38 28 12 2 35 39 17 17 37 Smallest: Largest: Sum: Average: 39 225 25.00

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Inference Control In Statistical Databases From Theory To Practice Lncs 2316

Authors: Josep Domingo-Ferrer

2002nd Edition

3540436146, 978-3540436140

More Books

Students also viewed these Databases questions

Question

7-16 Compare Web 2.0 and Web 3.0.

Answered: 1 week ago