main.cpp stat_functions.h stat_functions.cpp Above is the Programming Assignment
ID: 3709573 • Letter: M
Question
main.cpp
stat_functions.h
stat_functions.cpp
Above is the Programming Assignment 3
Taking the program you wrote for Programming Assignment 3, add the following to the statistics library:
- A selection sort function
- A linear search function that returns the index of the first instance of the search value found
- A binary search function that also returns the index of the first instance of the search value found
- A function that returns the number of instances of the search value, i.e., a count of how many times the search value was found. Use a tolerance of +/- 5%.
In the main program, sort your array and recalculate the median. Output the original, incorrect median value as well. Output the contents of the first or last 500 values in the sorted array. Using both search functions, output the index of the median value. Because your array has an odd size, the median will be an exact match. Finally, ask the user to input a search value and return the number of instances of the value in your array...
Explanation / Answer
void selectionSort(double array[]){
for (int i = 0; i < 5001; i++)
{
int min_index = i;
for (j = i+1; j < 5001; j++)
if (array[j] < array[min_index])
min_index = j;
// Swap the found minimum element with the first element
double temp = array[min_index] ;
array[min_index] = array[i];
array[i] = temp;
}
}
int linearSearch(double array[], double num){
for(int i=0;i<5001;i++){
if(array[i] == num)
return i;
}
// inidicates element not present
return -1;
}
int binarySearch(double array[], int start, int end, double num){
if(end>start){
int mid = start + (end - start)/2;
if (array[mid] == num)
return mid;
if (array[mid] > num)
return binarySearch(array, start, mid-1, num);
return binarySearch(array, mid+1, end, num);
}
// inidicates element not present
return -1;
}
int countOccurences(double array[], double num){
int count=0;
for(int i=0;i<5001;i++){
if(array[i] == num)
count++;
}
return count;
}
Modified Main():
int main() {
srand(time(NULL));
//array of 5001 real numbers
double array[SIZE] = {0};
for ( int i = 0; i < 5001 ; i++ )
array[i] = 20 + (rand()/(float)RAND_MAX ) * 30 ;
cout << "Max value is : " << maxval( array, SIZE ) << endl;
cout << "Min value is : " << minval( array, SIZE ) << endl;
cout << "Mean value is : " << mean( array, SIZE ) << endl;
cout << "Median value is : " << median( array, SIZE ) << endl;
cout << "Variance value is : " << variance( array, SIZE ) << endl;
cout << "Std deviation value is : " << std_dev( array, SIZE ) << endl;
cout << endl;
cout << "Values within range 20 to 30 is : " << count( array, SIZE, 20, 30) << endl;
cout << "Values within range 30 to 40 is : " << count( array, SIZE, 30, 40) << endl;
cout << "Values within range 40 to 50 is : " << count( array, SIZE, 40, 50) << endl;
// sort the array
selectionSort(array);
// recalculate the median
cout << "Median value after sorting is : " << median( array, SIZE ) << endl;
// output first 500 elements
for ( int i = 0; i < 500 ; i++ )
cout<<array[i]<<" ";
// Using both search functions, output the index of the median value
double med = median( array, SIZE );
int indLin, indBin;
indLin = linearSearch(array, med);
indBin = binarySearch(array,0,);
// if median element has only one occurence, both linear and binary search will output same index
cout<<indLin<<" "<<indBin;
// find number of instances
int n;
cout<<"Enter a number ";
cin>>n;
cout<<"Number of occurences of "<<n<<" are: "<<countOccurences(array,n);
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.