Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Overview For this assignment, write a program that will process two sets of rand

ID: 3607635 • Letter: O

Question

Overview

For this assignment, write a program that will process two sets of random numeric information. The information will be needed for later processing, so it will be stored in arrays that will be displayed, sorted, and displayed (again).

Basic Program Logic

The main function for this program is pretty basic. It should start by creating two arrays. Each array should hold a maximum of 50 double or float elements. There should also be at least two integers. Each integer will hold the number of values that are placed into the arrays (Note: this is needed because it's possible that an entire array is not filled with data and only the part that is used should be processed).

Use srand to set the seed value for the random number generator. The program that is handed in will use srand(7) but any value can be used as the program is being developed.

Call the buildArray function that is described below to fill one of the arrays with a random number of random values. The value that is returned from the function should be saved in (assigned to) one of the integer variables.

Display the number of values that were placed in the array with an appropriate label.

Call the printArray function that is described below to display the first array of unsorted values with a title similar to "Unsorted Random Numbers".

Call the sortArray function that is described below to put the values in the first array in ascending order.

Call the printArray function a second time to display the sorted values in the first array with a title similar to "Sorted Random Numbers".

Call the buildArray function a second time to fill the other array with a random number of random values. The value that is returned from the function should be saved in (assigned to) the other integer variable.

Display the number of values that were placed in the second array with an appropriate label.

Call the printArray function to display the second array of unsorted values with a title similar to "Unsorted Random Numbers".

Call the sortArray function to put the values in the second array in ascending order.

Finally, call the printArray function to display the sorted values in the second array with a title similar to "Sorted Random Numbers".

The Functions

The following functions are required for the assignment:

double randDouble()

This function will generate a random double value that is within a specific range. It takes no arguments and returns a double: the random number.

The rand function that has been used in previous assignments generates a random integer value. This function will still use that value but it will be used in a formula that changes the value to a double. The formula is:

lower bound + ( random integer / (maximum possible random number / (upper bound - lower bound)))

See the Symbolic Constants section for the "lower bound," "upper bound," and "maximum possible random number" values.

int buildArray( double array[] )

This function will fill an array of doubles with a random number of random values.

It takes as its argument an array of doubles: the array that will hold the numeric information and returns an integer: the number of values that were placed in the array.

The function should start by generating a random integer value that represents the number of values to place into the array. This value should be between 2 and 50. (Note: the program 4 write-up shows how to get a random number within a specific range.)

Once the number of values has been determined, write a loop that will execute that number of times. Inside of the loop, call the randDouble function that was previously described to get a random double value and then put the value that is returned from the function into the array argument.

After the values have been placed into the array, return the number of values that were placed in the array (the random integer from earlier).

void printArray( double array[], int numberOfValues, string title )

This function will display the numeric information in an array of doubles. There should be 10 values displayed per line, with the exception of the last line, which may have fewer than 10 values.

This function takes three arguments: the first argument is an array of doubles that holds the numbers to be displayed, the second argument is an integer that holds the number of values to be displayed, and the third argument is a string that holds a title that should be displayed before displaying the numeric information. The function returns nothing.

The function should first execute a cout statement that will display whatever the string argument is holding. After that, execute a loop that executes numberOfValues number of times. Within the loop, determine if 10 values have been displayed. If 10 values have been displayed, cout a newline character. After that, display a number from the array.

The values should be displayed with exactly 1 digit after the decimal point and the decimal points should line up from one line to the next.

void sortArray( double array[], int numberOfValues )

This function will use the selection sort algorithm that was presented in lecture to sort the array in ASCENDING order.

This function takes two arguments: the first argument is an array of doubles that holds the numbers to be sorted, and the second argument is an integer that holds the number of values to be sorted. The function returns nothing.

Symbolic Constants

This program requires the use of 6 symbolic constants. One of the constants is predefined and the remaining 5 must be defined by you.

Pre-defined constant

RAND_MAX is a symbolic constant that holds the maximum possible random number that is available on the system that is executing the program. At a minimum, the value will be 32767. Make sure to add #include <cstdlib> to the top of the program. DO NOT declare this constant in the program.

Constants that need to be created/defined

The first constant is a double that represents the lower bound of the double values that will be placed into the array of doubles. The value should be 0. (Note: make sure to use the value 0.0 if using #define)

The second constant is a double that represents the upper bound of the double values that will be placed into the array of doubles. The value should be 100. (Note: make sure to use the value 100.0 if using #define)

The third constant is an integer that represents the minimum number of values that the array of doubles can hold. The value should be 2.

The fourth constant is an integer that represents the maximum number of values that the array of doubles can hold. The value should be 50.

The fifth constant is an integer that represents the maximum number of values to display per line of output. The value should be 10.

Programming Requirements:

All 6 of the symbolic constants must be used in the program.

Add #include <ctime> and #include <cstdlib> at the top of the program.

Make sure to seed the random number generator by calling the srand function. This MUST be done in main before calling the buildArray function. The file that is handed in for grading must use srand(7);.

As with program 6, each of the functions should have a documentation box that describes the function. This is the last reminder about function documentation that will appear in the assignment write-ups.

Hand in a copy of the source code using Blackboard.

Output

Run 1 (using srand(7))

Run 2 using srand(time(0))

Explanation / Answer


Given below is the code with output. Please don't forget to rate the answer if it helped.
Thank you.


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


#define LOWER 0.0
#define UPPER 100.0
#define MIN_SIZE 2
#define MAX_SIZE 50
#define VALS_PER_LINE 10

using namespace std;

double randDouble();
int buildArray(double arr[]);


double randDouble()
{
double d = LOWER + ( rand() / (RAND_MAX / (UPPER - LOWER)));

return d;
}
int buildArray(double arr[])
{
int n = MIN_SIZE + rand() % (MAX_SIZE - MIN_SIZE + 1);
for(int i = 0; i < n; i++)
{
arr[i] = randDouble();
}
return n;

}

void printArray( double arr[], int n, string title )
{
cout << title << endl;
cout << "------------------------------------------------------------" << endl;
for(int i = 0; i < n; i++)
{
if(i % VALS_PER_LINE == 0)
cout << endl;
cout << setw(6) << arr[i];
}
cout << endl;
}


void sortArray( double a[], int n )
{
int minIdx;
for(int i = 0; i < n; i++)
{
minIdx = i;
for( int j = i + 1; j < n; j++)
{

if(a[j] < a[minIdx])
minIdx = j;
}
if(i != minIdx) //swap if needed
{
double temp = a[i];
a[i] = a[minIdx];
a[minIdx] = temp;
}
}
}

int main()
{
double arr1[MAX_SIZE];
double arr2[MAX_SIZE];
int n1, n2;

srand(7);
cout << fixed << showpoint << setprecision(1);

n1 = buildArray(arr1);
cout << "There are " << n1 << " values in first array" << endl;

printArray(arr1, n1, "Unsorted Random numbers" );
sortArray(arr1, n1);
printArray(arr1, n1,"Sorted Random numbers" );



n2 = buildArray(arr2);
cout << "There are " << n2 << " values in second array" << endl;
printArray(arr2, n2, "Unsorted Random numbers" );
sortArray(arr2, n2);
printArray(arr2, n2, "Sorted Random numbers" );

return 0;
}


===============
output


There are 2 values in first array
Unsorted Random numbers
------------------------------------------------------------

92.1 28.9
Sorted Random numbers
------------------------------------------------------------

28.9 92.1
There are 13 values in second array
Unsorted Random numbers
------------------------------------------------------------

72.9 53.3 32.9 75.2 75.5 54.3 68.5 63.6 81.7 24.2
37.4 70.8 69.8
Sorted Random numbers
------------------------------------------------------------

24.2 32.9 37.4 53.3 54.3 63.6 68.5 69.8 70.8 72.9
75.2 75.5 81.7