CSCI240 Program 7 Arrays: building, printing and sorting solution

$25.00

Original Work ?
Category: You will Instantly receive a download link for .ZIP solution file upon Payment

Description

5/5 - (4 votes)

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:

  1. All 6 of the symbolic constants must be used in the program.
  2. Add #include <ctime> and #include <cstdlib> at the top of the program.
  3. 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);.
  4. 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.
  5. Hand in a copy of the source code using Blackboard.

Output

Run 1 (using srand(7))

There are 14 values in the first array.


Unsorted Random Numbers
------------------------------------------------------------
  53.2  46.4  21.5  47.4  19.9  92.0  36.0  84.6  62.1  14.7
  86.7  76.4  54.3  36.8

Sorted Random Numbers
------------------------------------------------------------
  14.7  19.9  21.5  36.0  36.8  46.4  47.4  53.2  54.3  62.1
  76.4  84.6  86.7  92.0



There are 43 values in the second array.


Unsorted Random Numbers
------------------------------------------------------------
   0.2  73.8  48.3  47.3   7.9  28.0  86.8  28.9  68.3  40.3
  43.3  58.8  43.2  34.0  71.6  53.3   0.2   6.1  30.4  36.5
  16.0  30.8  35.8  95.5  72.4  82.7  52.3  20.5  99.0  22.7
  99.9  95.1  64.8  34.2   6.1  52.1  72.9  52.5  81.5  61.4
  60.6  76.7  45.4

Sorted Random Numbers
------------------------------------------------------------
   0.2   0.2   6.1   6.1   7.9  16.0  20.5  22.7  28.0  28.9
  30.4  30.8  34.0  34.2  35.8  36.5  40.3  43.2  43.3  45.4
  47.3  48.3  52.1  52.3  52.5  53.3  58.8  60.6  61.4  64.8
  68.3  71.6  72.4  72.9  73.8  76.7  81.5  82.7  86.8  95.1
  95.5  99.0  99.9

Run 2 using srand(time(0))

There are 4 values in the first array.


Unsorted Random Numbers
------------------------------------------------------------
  21.6  57.4  97.1  72.2

Sorted Random Numbers
------------------------------------------------------------
  21.6  57.4  72.2  97.1



There are 40 values in the second array.


Unsorted Random Numbers
------------------------------------------------------------
  30.6  67.6   9.0  98.9  22.5  14.5  71.4  88.6  64.0  33.4
  59.0  69.2  62.3   9.6  44.5  87.7  29.5  96.4  50.0  81.8
  91.4  49.8  55.9  14.8  94.5  71.8  83.5  90.4  74.3  65.5
  42.3  74.1   3.7  55.5  81.4   0.3  83.4  76.0  36.9  93.0

Sorted Random Numbers
------------------------------------------------------------
   0.3   3.7   9.0   9.6  14.5  14.8  22.5  29.5  30.6  33.4
  36.9  42.3  44.5  49.8  50.0  55.5  55.9  59.0  62.3  64.0
  65.5  67.6  69.2  71.4  71.8  74.1  74.3  76.0  81.4  81.8
  83.4  83.5  87.7  88.6  90.4  91.4  93.0  94.5  96.4  98.9

Extra Credit 1

For up to 5 points of extra credit, write and use a fuction that will merge the two arrays of data into one larger array.

This will require the addition of a third array of doubles to main(). The new array should be 2 times the size of the original arrays. Another integer should also be added. The integer will hold the number of values that are placed into the new array and should be equal to the sum of the number of values that were placed in the first array and the number of values that were placed in the second array.

In main(), display something like “Extra Credit 1 output” to indicate to the TA grading the assignment that the first extra credit was attempted. Display the number of values that the larger array holds.

Call the mergeArrays function that is described below to merge the values from the two small arrays into the larger array. Call the printArray function to display the larger array with a title similar to “Merged Array”.

Extra Credit 1 Function

Write (and use) the following function to merge the two smaller arrays into one larger array:

void mergeArrays( double array1[], int numberOfValues1, double array2[], int numberOfValues2, double largerArray[] )

This function will merge two smaller arrays of double values into one large array of double values.

This function takes five arguments: the first argument is an array of doubles that holds one set of values to be merged, the second argument is an integer that holds the number of values from the first argument to be merged, the third argument is an array of doubles that holds the other set of values to be merged, the fourth argument is an integer that holds the number of values from the third argument to be merged, and the fifth argument is an array of doubles that will hold all of the values from the other two arrays. The function returns nothing.

Call the sortArray function to sort the values in the first array argument.

Call the sortArray function to sort the values in the second array argument.

To merge the values from the two arrays into the larger array (the third array argument), the two smaller arrays must be processed simultaneously. To do this, write a loop that will execute until the end of one or both of the smaller arrays has been reached. Inside of the loop, if the value from the first array argument is smaller than the value from the second array argument, copy the value from the first array argument into the larger array and increment a subscript for the first array argument and a subscript for the larger array. Otherwise, copy a value from the second array argument into the larger array and increment a subscript for the second array argument a subscript for the larger array.

At this point, the end of one or both of the smaller arrays has been reached. While the end of the first array has not been reached, copy the remaining values from the first array argument into the larger array. While the end of the second array has not been reached, copy the remaining values from the second array argument into the larger array.

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 1 Output

The following is the extra output for the runs of the program from earlier in the assignment write-up.

Run 1 using srand(7)

Extra Credit 1 output

There are 57 values in the merged array.


Merged Array
------------------------------------------------------------
   0.2   0.2   6.1   6.1   7.9  14.7  16.0  19.9  20.5  21.5
  22.7  28.0  28.9  30.4  30.8  34.0  34.2  35.8  36.0  36.5
  36.8  40.3  43.2  43.3  45.4  46.4  47.3  47.4  48.3  52.1
  52.3  52.5  53.2  53.3  54.3  58.8  60.6  61.4  62.1  64.8
  68.3  71.6  72.4  72.9  73.8  76.4  76.7  81.5  82.7  84.6
  86.7  86.8  92.0  95.1  95.5  99.0  99.9

Run 2 using srand(time(0))

Extra Credit 1 output

There are 44 values in the merged array.


Merged Array
------------------------------------------------------------
   0.3   3.7   9.0   9.6  14.5  14.8  21.6  22.5  29.5  30.6
  33.4  36.9  42.3  44.5  49.8  50.0  55.5  55.9  57.4  59.0
  62.3  64.0  65.5  67.6  69.2  71.4  71.8  72.2  74.1  74.3
  76.0  81.4  81.8  83.4  83.5  87.7  88.6  90.4  91.4  93.0
  94.5  96.4  97.1  98.9

Extra Credit 2

For up to 5 points of extra credit, write and use a fuction that will count and display the number of values in an array of double values that fall within specific ranges.

Extra Credit 1 DOES NOT have to be attempted to receive the extra credit points for Extra Credit 2.

In main(), display something like “Extra Credit 2 output” to indicate to the TA grading the assignment that the second extra credit was attempted.

Display a simple line such as “Array 1” and then call the countValues function that is described below to count and display the number of values in the first array that fall within specific ranges.

Display a simple line such as “Array 2” and then call the countValues function (again) to count and display the number of values in the second array that fall within specific ranges.

If Extra Credit 1 was attempted, display a simple line such as “Merged Array” and then call the countValues function to count and display the number of values in the merged array that fall within specific ranges.

Extra Credit 2 Function

Write (and use) the following function to count and display the number of values that fall within specific ranges in an array of double values:

void countValues( double array[], int numberOfValues )

This function will count and display the number of values in an array of double values that fall within specific ranges.

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

The function should count and display the number of values within each of the following ranges:

0 – 9.9
10 – 19.9
20 – 29.9
30 – 39.9
40 – 49.9
50 – 59.9
60 – 69.9
70 – 79.9
80 – 89.9
90 – 99.9

The counts should be neatly displayed in a tabular format with labels that indicate the various ranges.

Note about extra credit: the points will ONLY be awarded if the required portions of the assignment work correctly.

Extra Credit 2 Output

The following is the extra output for the runs of the program from earlier in the assignment write-up.

Run 1 using srand(7)

Extra Credit 2 output

Array 1
----------------------------

There were 14 total values

   0 - 10:    0
  10 - 20:    2
  20 - 30:    1
  30 - 40:    2
  40 - 50:    2
  50 - 60:    2
  60 - 70:    1
  70 - 80:    1
  80 - 90:    2
  90 - 100:   1


Array 2
----------------------------

There were 43 total values

   0 - 10:    5
  10 - 20:    1
  20 - 30:    4
  30 - 40:    6
  40 - 50:    6
  50 - 60:    5
  60 - 70:    4
  70 - 80:    5
  80 - 90:    3
  90 - 100:   4


Merged Array
----------------------------

There were 57 total values

   0 - 10:    5
  10 - 20:    3
  20 - 30:    5
  30 - 40:    8
  40 - 50:    8
  50 - 60:    7
  60 - 70:    5
  70 - 80:    6
  80 - 90:    5
  90 - 100:   5

Run 2 using srand(time(0))

Extra Credit 2 output

Array 1
----------------------------

There were 4 total values

   0 - 10:    0
  10 - 20:    0
  20 - 30:    1
  30 - 40:    0
  40 - 50:    0
  50 - 60:    1
  60 - 70:    0
  70 - 80:    1
  80 - 90:    0
  90 - 100:   1


Array 2
----------------------------

There were 40 total values

   0 - 10:    4
  10 - 20:    2
  20 - 30:    2
  30 - 40:    3
  40 - 50:    4
  50 - 60:    3
  60 - 70:    5
  70 - 80:    5
  80 - 90:    6
  90 - 100:   6


Merged Array
----------------------------

There were 44 total values

   0 - 10:    4
  10 - 20:    2
  20 - 30:    3
  30 - 40:    3
  40 - 50:    4
  50 - 60:    4
  60 - 70:    5
  70 - 80:    6
  80 - 90:    6
  90 - 100:   7