This problem is divided into seven parts. Your main program should make a call t
ID: 3590267 • Letter: T
Question
This problem is divided into seven parts. Your main program should make a call to these seven functions, passing the necessary parameters and getting the necessary return values. You must provide functions with the given names and parameters. The names of any other supporting functions are named at your discretion. The names of the supporting functions that you implement should be reflective of the function they perform. You will not take any user inputs in the functions in your Assignment5.cpp file. The problem statement is designed in a way that all the inputs to the functions are being passed from your main function. Secondly, you will not be printing any values in these functions. You need to evaluate the correctness of your implementation by writing code in your main program and validating the return values from these function. If you add debugging outputs, please remove that code before submitting. In this assignment you will make use of arrays as your data structure to store values and looping should be preferably done using for loops. Treat every function as an independent function, where each function can take a different array as an input. Each of these functions will return a value or modify an array parameter. Your main should be used to setup the parameters and print out the returned value(s) of the function.
There are seven parts to the problem. The following is the step by step description of each. Part 1 – Sum all the values of the array In this part, you will sum all the values in an array. The sumArray function will take two arguments, a reference to an array and the size of the array. The function will return the sum of all the values in the array as a float. Part 2 - Search a value in an array In this part, you will search a target value in an array, and return the index of that target value. The Search function takes in three arguments: an integer array, the size of that array and the target value we are searching for. Your function should return the index of that target value in the input array. If the target is not found, return -1. Part 3 – Calculate sum of squared differences between two arrays In this part, you will find the sum of squared differences between two arrays of the same size. The calculateDifference function takes in three arguments: a reference to array a, reference to array b, and the common size of the arrays. The function will use these values to calculate the sum of squared differences between each value of the two arrays and return that value as a float. For example, the squared differences of two numbers a and b is (a-b) 2 . Part 4 – Sort the array In this part, you will arrange the items in your array in ascending order, with the lowest item being at the start of the array and the highest item at the end. You can use the Bubble sort algorithm or the selection sort algorithm to sort the array. The sortArray function will take two arguments, i.e. a reference to the array and the size of the array. The function does not return any value, but will modify the elements of the array given as a parameter. Part 5 – Copy the array In this part, you will copy the values from one array to another array. The copyArray function will take three arguments, a reference to the source array to copy the values from, the size of this array, and a destination array. Part 6 - Convert and fill another array In this part, you will fill in the values of array b, corresponding to each value in array a. Map the values as follows: For example, when a is [0, 3, -5], b should be [“Not-read”, “Good”, “Terrible”] after the function is executed. Use "INVALID" if the value does not match any valid rating. Part 7 – Find the median score In this part, you will find the median item in the array. The median value of a list arranged in numerical order is the middle element of that list. However, you are not allowed to change the order of the elements or change the values in the array that you receive as a parameter. The findMedian function should take two arguments, i.e. a reference to the array and the size of the array. The function should return the median of numbers in the array. Challenge Problem For this challenge problem, you are going to use something called a two-dimensional array, which is an array of arrays. You will write a function that will multiply every value inside the two-dimensional array with an integer, and return the resulting two-dimensional array. In a one-dimensional array, it works like this: [2 4 6 8] × 3 = [6 12 18 24] And two-dimensional arrays work exactly the same. The Simple2DMultiplication function will take two parameters, an integer and a two-dimensional array. The function should modify the values in the two-dimensional array. Every value in the modified array should be the corresponding original value in matrix multiplied by multiplier.
Explanation / Answer
// part 1
float sumArray(float *arr, int size) {
float sum = 0;
for(int i = 0; i < size; i++) {
sum+= arr[i];
}
return sum;
}
// part 2
int search(float *arr, int size, float target) {
for(int i = 0; i < size; i++) {
if (arr[i] == target) {
return i;
}
}
return -1;
}
// part 3
float calculateDifference(float *a, float *b, int size) {
float squaredDiffSum = 0;
for(int i = 0; i < size; i++) {
squaredDiffSum += (a[i] - b[i])*(a[i] - b[i]);
}
return squaredDiffSum;
}
// part 4
void swap(float *a, float *b)
{
int temp = *a;
*a = *b;
*b = temp;
}
void sortArray(float *arr, int size) {
int i, j, min_index;
for (i = 0; i < size - 1; i++)
{
min_index = i;
for (j = i + 1; j < size; j++)
if (arr[j] < arr[min_index])
min_index = j;
swap(&arr[min_index], &arr[i]);
}
}
// Part 5
void copyArray(int *sourceArr, int size, int *destinationArr) {
for(int i = 0; i < size; i++) {
destinationArr[i] = sourceArr[i];
}
}
As per policy I answered more than 4 parts
For part 6
you need to indicate how to map
6 - Convert and fill another array In this part, you will fill in the values of array b, corresponding to each value in array a. Map the values as follows: For example, when a is [0, 3, -5], b should be [“Not-read”, “Good”, “Terrible”] after the function is executed. Use "INVALID" if the value does not match any valid rating.
7.
// A comparator function used by qsort
int compare(const void * a, const void * b)
{
return ( *(float*)a - *(float*)b );
}
float findMedian(float array[], int size) {
float *temp = new float[size];
qsort(temp, size, sizeof(float), compare);
float median = array[(size-1)/2];
if (size % 2 == 0) {
median = (median + array[size/2])/2;
}
return median;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.