Question
Write a C++ program that will implement the following functions: computeMean: Returns the mean (average) of an array (vector) of values computeMedian: Returns the median
Write a C++ program that will implement the following functions:
computeMean: Returns the mean (average) of an array (vector) of values
computeMedian: Returns the median of a vector of values
searchLinear: Searches a vector of values for a given value, returns the index of the value if found or -1 if not found
searchBinary: searches a vector of sorted values for a given value, returns the index of the value if found or -1 if not found
selectionSort: Sorts a vector of values from high to low or from low to high using the selection sort algorithm; sort order is determined by the boolean parameter ascending. If ascending is true, sort from low to high, otherwise sort from high to low
Your functions should adhere to the following signatures;
Mean
double computeMean (vector
Median
int computeMedian (vector
Binary Search
int searchBinary (int value, vector
Linear Search
int searchLinear (int value, vector
Selection Sort
vector
Vector Values
Use a random number generator to generate 101 values for your vector. As illustrated in class, use a call to the srand function with a seed value of 75025 to initialize the generator. You will need to add a new include direction
...
#include
....
// initialize the random number generator to the 25th Fibonacci number
const unsigned int seed = 75025;
const unsigned int moduloValue = 517;
srand(seed);
Generate 101 random values for your vector, like this
const int COUNT = 101;
vector
for (int i=0; i
numbers[i] = rand() % moduloValue;
}
Computations
Save the first three values generated by your random number generator as values to be used later for search. Identify them as searchValue1, searchValue2, and searchValue3.
Compute the mean of your vector values. Print the result
Sort your vector values from low to high. Print the result
Compute the median of your vector values. Print the result
Find (or not) the values; searchValue1, searchValue2, and searchValue3 in your vector. Print the index of those values or -1
Find (or not) the values -1, 10,000 in your vector using binary search. Print the index of those values or -1
Sort your vector values from high to low. Print the result.
Sample Output
The following is sample output that the program should generate. Note that when printing values resulting from function operations, the values are labeled so as to indicate what operation was performed to achieve these values.
Note: Do not use these search values to test your search routines.
Vector of values randomly generated
value[0]: 65
value[1]: 145
value[2]: 158
value[3]: 399
value[4]: 232
value[5]: 380
value[6]: 233
value[7]: 481
value[8]: 47
value[9]: 284
value[10]: 272
value[11]: 270
value[12]: 197
value[13]: 133
value[14]: 422
value[15]: 505
value[16]: 278
value[17]: 33
.
value[98]: 439
value[99]: 468
value[100]: 173
Vector of values sorted (descending) by selection sort
value[0]: 510
value[1]: 505
value[2]: 501
value[3]: 499
value[4]: 499
value[5]: 485
value[6]: 483
value[7]: 481
value[8]: 477
value[9]: 473
value[10]: 468
value[11]: 466
value[12]: 465
value[13]: 446
value[14]: 439
...
value[100]: 4
Vector of values sorted (ascending) by selection sort
value[0]: 4
value[1]: 8
value[2]: 17
value[3]: 21
value[4]: 28
value[5]: 33
value[6]: 33
value[7]: 37
value[8]: 47
value[9]: 56
value[10]: 59
value[11]: 64
value[12]: 65
value[13]: 96
value[14]: 108
...
value[100]: 510
****Linear Searches****
Searching for 4 using linear search
Found 4 at location 0
Searching for 510 using linear search
Found 510 at location 100
Searching for -1 using linear search
!!-1 not found
Searching for 1024 using linear search
!!1024 not found
****Binary Searches****
Searching for 4 using binary search
Found 4 at location 0
Searching for 510 using binary search
Found 510 at location 100
Searching for -1 using binary search
!!-1 not found
Searching for 1024 using binary search
!!1024 not found
Median is 272
Mean is 276.129
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started