(1) Create a C++ project for your assignment 3, with the name format prescribed in the Information section. Add a source file and name it SwapCount. In this file, write the C++ program that will perform the tasks specified in question 2.

(2) Declare and implement a function called sortSwapCount that passes three parameters, the array, the size and the reference of proportion, and returns the count. The function declaration is int sortSwapCount (int data[], int size, double& proportion)

(I) The function sorts the integers in data[] in ascending order using the bubble sort method. The purpose is to count or determine the number of exchanges (swaps) made by the bubble sort algorithm.

(ii) The expected number of sorting swaps (expectedSwapCount) is size* (size -1) if the array was unsorted. Calculate the proportion of the array status by first converting (or casting) the integers, swapCount and expectedSwapCount into floating-point, before calculating the proportion. The syntaxes for these calculations are
double value = (double) x; // if x is an integer or
double value = (double) x * 1.00; // try this if the first fails

The proportion can be defined as the number of swaps counted over the expected swap counts, and multiply by 100 to present it in percentage.

Finally, return the swapCount.

(3) The proportion which is calculated in the function of (2) above will be effectively updated in the main function. Therefore, the function here will determine the status or condition of the data distribution in the array. The function passes the proportion and returns the status. The function signature is

string listStatus (double proportion)

The status must be stored in the variable status before returning it. If the proportion of count is above 80%, the array is unsorted, if the proportion is above 45% then the array is partially sorted, if the proportion is above 0%, the array is almost sorted, otherwise, the array is fully sorted. Return the status.


(4) In the main function, do the following:

a. Declare and store the following 10 integers in an array elem[]. 8, 4, 14, 9, 1, 12, 6, 2, 5, 7

b. Determine the swap-count of the array in (a) by declaring variable ssCount and store the value from the function call sortSwapCount() function in it. Furthermore, declare variable swProportion which will store the proportion of swaps made. Pass this also to the function. Display these values with labels.

c. Determine the array status by calling the listStatus function, store it in a variable before displaying it. Remember to pass the proportion in (b) as argument to the function.​



Answer :

Other Questions