Overview For this assignment, write a program that will process two sets of rand
ID: 3738545 • 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 100 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(0) 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.
Call the printArray function that is described below to display the first array of unsorted values with a title similar to "First Array -- 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 "First Array -- 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.
Call the printArray function to display the second array of unsorted values with a title similar to "Second Array -- Unsorted Random Numbers".
Call the sortArray function to put the values in the second array in descending order.
Finally, call the printArray function to display the sorted values in the second array with a title similar to "Second Array -- 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 30 and 100. Use the following formula to generate a value 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 8 values displayed per line, with the exception of the last line, which may have fewer than 8 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 and the number of values to be displayed. After that, execute a loop that executes numberOfValues number of times. Within the loop, determine if 8 values have been displayed. If 8 values have been displayed, cout a newline character. After that, display a number from the array.
The values should be displayed with exactly 3 digits after the decimal point and the decimal points should line up from one line to the next.
void sortArray( double array[], int numberOfValues, char sortType )
This function will use the selection sort algorithm that was presented in lecture to sort the array.
This function takes three arguments: the first argument is an array of doubles that holds the numbers to be sorted, the second argument is an integer that holds the number of values to be sorted, and the third argument is a character that holds the type of sort that should be performed. The function returns nothing.
For the sortType argument, a value of 'A' or 'a' indicates that an ascending order sort should be performed. A value of 'D' or 'd' indicates that a descending order sort should be performed. You may assume that the sortType argument will only contain a valid value.
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 150. (Note: make sure to use the value 150.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 30.
The fourth constant is an integer that represents the maximum number of values that the array of doubles can hold. The value should be 100.
The fifth constant is an integer that represents the maximum number of values to display per line of output. The value should be 8.
Programming Requirements:
All 6 of the symbolic constants must be used in the program.
Add #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(0);.
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(0))
Run 2 using srand(time(0))
Extra Credit 1
For up to 5 points of extra credit, display the smallest and largest values in each of the arrays. This should be displayed AFTER displaying the sorted numbers in the array.
Extra Credit 2
For up to 5 points of extra credit, write and use a function that will calculate the average of the numbers in an array of doubles. This function should be called for each of the arrays of doubles and the return value should be displayed in main() AFTER displaying the sorted numbers in the array.
Extra Credit 1 DOES NOT have to be attempted to receive the extra credit points for Extra Credit 2.
double average( double array[], int numberOfValues )
This function will calculate the average of the values in an array of doubles.
This function takes two arguments: the first argument is an array of doubles that holds the numbers to be used in the calculation, and the second argument is an integer that holds the number of values to use in the calculation. The function returns a double: the average of the values in the array of doubles.
Extra Credit 3
For up to 5 points of extra credit, write and use a function that will calculate the standard deviation of the numbers in an array of doubles. This function should be called for each of the arrays of doubles and the return value should be displayed in main() AFTER displaying the sorted numbers in the array.
Extra Credit 1 and 2 DO NOT have to be attempted to receive the extra credit points for Extra Credit 3.
double standardDeviation( double array[], int numberOfValues )
This function will calculate the standard deviation of the values in an array of doubles.
This function takes two arguments: the first argument is an array of doubles that holds the numbers to be used in the calculation, and the second argument is an integer that holds the number of values to use in the calculation. The function returns a double: the standard deviation of the values in the array of doubles.
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
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
The following is the extra output for the runs of the program from earlier in the assignment write-up.
Run 1 using srand(0)
Run 2 using srand(time(0))
(??) 2 ? x2) Standard deviation ? – 1Explanation / Answer
#include <iostream>
#include <cstdlib>
#include <cmath>
using namespace std;
#define lb 0.0
#define ub 150.0
#define min_value 30
#define max_value 100
double randDouble(){
return lb + (rand()*(RAND_MAX/(ub - lb)));
}
int buildArray(double* a){
int size = min_value + (rand() % (max_value - min_value + 1));
a = new double[size];
for(int i=0;i<size;i++){
a[i]=randDouble();
}
return size;
}
void printArray( double* a, int numberOfValues, string title ){
cout<<"title"<<numberOfValues<<endl;
for(int i=0;i<numberOfValues;i++){
cout<<a[i]<<" ";
if(i%8==0)
cout<<endl;
}
}
void sortArray( double* a, int numberOfValues, char sortType ){
switch(sortType){
case 'A':{
double max=0;
int index=0;
for(int i=0;i<numberOfValues;i++){
max=a[0];
for(int j=0;j<numberOfValues-i;j++){
if(max<a[j]){
max=a[j];
index=j;
}
}
a[index]=a[numberOfValues-i-1];
a[numberOfValues-i-1]=max;
}
break;
}
case 'D':{
double max=0;
int index=0;
for(int i=0;i<numberOfValues;i++){
max=a[i];
for(int j=i;j<numberOfValues;j++){
if(max<a[j]){
max=a[j];
index=j;
}
}
a[index]=a[i];
a[i]=max;
}
break;
}
}
}
double average( double* a, int numberOfValues ){
double avg=0;
for(int i=0;i<numberOfValues;i++){
avg+=a[i];
}
avg/=numberOfValues;
return avg;
}
double standardDeviation( double* a, int numberOfValues ){
double sumOfSquares=0;
double avg=average(a,numberOfValues);
for(int i=0;i<numberOfValues;i++){
sumOfSquares+=(a[i]*a[i]);
}
sumOfSquares/=numberOfValues;
sumOfSquares-=(avg*avg);
sumOfSquares = sqrt(sumOfSquares);
return sumOfSquares;
}
double smallest(double* a,int numberOfValues){
double min = (double)INT_MAX;
for(int i=0;i<numberOfValues;i++){
if(min>a[i])
min=a[i];
}
return min;
}
double largest(double* a,int numberOfValues){
double max = 0.0;
for(int i=0;i<numberOfValues;i++){
if(max<a[i])
max=a[i];
}
return max;
}
void printData(double* a,int numberOfValues,string title){
cout<<title<<endl;
cout<<"Smallest value: "<<smallest(a,numberOfValues)<<endl;
cout<<"Largest value: "<<largest(a,numberOfValues)<<endl;
cout<<"Average: "<<average(a,numberOfValues)<<endl;
cout<<"Standard deviation: "<<standardDeviation(a,numberOfValues)<<endl;
}
int main(){
double* a;
double* b;
int a_size = buildArray(a);
int b_size = buildArray(b);
printArray(a,a_size,"First Array -- Unsorted Random Numbers -- ");
sortArray(a,a_size,'A');
printArray(a,a_size,"First Array -- Sorted Random Numbers -- ");
printArray(a,a_size,"Second Array -- Unsorted Random Numbers -- ");
sortArray(b,b_size,'D');
printArray(a,a_size,"Second Array -- Sorted Random Numbers -- ");
printData(a,a_size,"First Array:");
printData(b,b_size,"Second Array:");
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.