Question
C++ Please Sorting Arrays In this assignment you will get to experiment with arrays in C++. You will implement a sorting algorithm and use functions
C++ Please
Sorting Arrays
In this assignment you will get to experiment with arrays in C++. You will implement a sorting algorithm and use functions defined with appropriate kind of parameters.
You will need to create 4 versions of your program, in a incremental fashion.
You should call your files:
1. a9
2. a9
3. a9
4. a9
Your program needs to accomplish the following:
A. Generating Random Arrays
Generates 2 arrays of random integers between 0 and 1000. Both arrays need to have length of N. Set N = 10 for now. Declare the arrays inside of main() and call them arrayA and arrayB. Then have a separate function randArrays that fills two arrays with random numbers. randArrays should be called with the two arrays from main.
B. Sorting Arrays
Sort the first array (call it arrayA) in ascending order. Sort the second array (call it arrayB) in descending order.
Have a function sortArr that takes in an array, array length, and a boolean value to indicate whether to sort in ascending or descending order. Function sortArr needs to call another function, call it swap2, for performing the array element swap operations required for sorting. You should call sortArr twice from main to sort each array.
Many sorting algorithms exist. A very simple one (though quite inefficient) is the Bubble Sort algorithm. The Bubble Sort implementation is given below and can be used as starter code: for(int i = length-1; i>0; i--)
{
for(int j = 0; j < i; j++){
if(arr[j]>arr[j+1]){
temp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = temp;
}
}
}
// Ascending or descending implementation?
C. Writing contents of 2 arrays into 1
Declare a new array of length 2N inside of main and call it arrayC. Have a function arrayMerge that gets called with 4 arguments (the 3 arrays and N). Copy the elements of arrayA into first half of arrayC and elements of arrayB into second half of arrayC.
D. Performance
Set N = 10e3. Inside of main, add timing code to measure the amount of time it takes to run the whole program starting with randArrays and ending with arrayMerge. Print the total execution time to the command prompt. If you have a local compilation environment setup, try running your program locally vs on JupyterHub. Is one faster than the other?
You can use the following technique for measuring execution time (make sure to include the ctime library):
int startTime, endTime;
startTime = clock();
// code you want to time here
endTime = clock();
double execTime = (double)(endTime-startTime)/CLOCKS_PER_SEC;
Codes so far:
randArrays:
#include
#include
using namespace std; void dispArray(int arr[], int length); // auxiliary function void randArrays(int arr[], int length);// function being tested
int main() { const int N = 10; int arrayA[]={1,3,2,0,2,2,7,0,2,0}; dispArray(arrayA,N); // calling the function through driver program randArrays(arrayA,N); dispArray(arrayA,N);
return 0; }
void dispArray(int arr[], int length) { // This is an auxiliary function. Not the function being //tested. for(int i=0;i // Function to populate the array with random values void randArrays(int arr[], int length) { // Function stub for now. To be completed later. // Lets just fill the array with -1s as dummy data. for(int i=0;i swap2: #include #include using namespace std; void dispArray(int arr[], int length); // auxiliary function void swap2(int arr[], int length);// function being tested int main() { const int N = 10; int arrayA[]={1,3,2,0,2,2,7,0,2,0}; dispArray(arrayA,N); swap2(arrayA,N); dispArray(arrayA,N); return 0; } void dispArray(int arr[], int length) { // This is an auxiliary function. Not the function being //tested. for(int i=0;i //function to swap every two adjacent elements void swap2(int arr[], int length) { // Function stub for now. To be completed later. // Lets just fill the array with -1s as dummy data. for(int i=0;i arrayMerge: #include #include using namespace std; void dispArray(int arr[], int length); // auxiliary function void arrayMerge(int arr1[], int arr2[], int arrmerged[], int length);// function being tested int main() { const int N = 10; int arrayA[]={1,2,3,4,5,6,7,8,9,10}; dispArray(arrayA,N); int arrayB[]={11,12,13,14,15,16,17,18,19,20}; int arrayAB[100]; arrayMerge(arrayA, arrayB, arrayAB, N); dispArray(arrayAB,2*N); return 0; } void dispArray(int arr[], int length) { // This is an auxiliary function. Not the function being //tested. for(int i=0;i //function to merge two arrays into one void arrayMerge(int arr1[], int arr2[], int arrmerged[], int length) { for(int i=0;i sortArr: #include #include using namespace std; void dispArray(int arr[], int length); // auxiliary function void sortArr(int arr[], int length);// function being tested int main() { const int N = 10; int arrayA[]={9,8,7,6,5,4,3,2,1,0}; dispArray(arrayA,N); sortArr(arrayA,N); dispArray(arrayA,N); return 0; } void dispArray(int arr[], int length) { // This is an auxiliary function. Not the function being //tested. for(int i=0;i void sortArr(int arr[], int length) { // Function stub for now. To be completed later. // Lets just fill the array with -1s as dummy data. for(int i=0;i
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