Please use C++, using the provided solution files, I hope I can get the full code make the following modifications: Note

Business, Finance, Economics, Accounting, Operations Management, Computer Science, Electrical Engineering, Mechanical Engineering, Civil Engineering, Chemical Engineering, Algebra, Precalculus, Statistics and Probabilty, Advanced Math, Physics, Chemistry, Biology, Nursing, Psychology, Certifications, Tests, Prep, and more.
Post Reply
answerhappygod
Site Admin
Posts: 899603
Joined: Mon Aug 02, 2021 8:13 am

Please use C++, using the provided solution files, I hope I can get the full code make the following modifications: Note

Post by answerhappygod »

Please use C++, using the provided solutionfiles, I hope I can get the full code
make the following modifications:
Note how long it takes for each run and include that in yoursubmission.
NOTE: that for small values of N, you might not have ameasurable time. In these cases, you will need to add a loop thatrepeats the bubble sort algorithm some number of times, and do yourtiming outside of that loop. Then divide the time observed by thenumber of times the loop ran to get the average run time of onesort of the array. Also, since the bubble sort has a "shortcircuit" built in, you will need to start each one of these sortswith a fresh copy of the unsorted array.
Source.cpp
#include "arrayutils.h"#include "sorting_algorithms.h"
const int ARRAY_SIZE = 10000;
int main(int argc, char* argv){int* array = generateRandomIntArray(ARRAY_SIZE);int* workingArray = generateIntArrayCopy(ARRAY_SIZE, array);
cout << "Copy done" << endl;
//printIntArray(ARRAY_SIZE, workingArray);
bubbleSort(ARRAY_SIZE, workingArray);//printIntArray(ARRAY_SIZE, workingArray);
cout << "Bubble sort done" << endl;return 0;}
sorting_algorithms.h
#pragma once
void bubbleSort(int size, int* intArray) {bool swapped = true;
while (swapped) {swapped = false;
for (int i = 0; i < size - 1; i++) {if (intArray > intArray[i + 1]){// swapint tmp = intArray;intArray = intArray[i + 1];intArray[i + 1] = tmp;swapped = true;}}}
}
arrayutils.h
#pragma once
#pragma once#include <cstdlib>#include <iostream>#include <cstring>#include <ctime>
using namespace std;
int* generateRandomIntArray(int size){// First dynamically allocate the arrayint* randArray = (int*)malloc(4 * size);
// Then loop over the array, randomly generating a numberbetween 0 and size - 1srand(time(NULL));for (int i = 0; i < size; i++) {randArray = rand() % size;}
// Return the address of the arrayreturn randArray;}
int* generateIntArrayCopy(int size, int* originalArray){int* secondArray = (int*)malloc(4 * size);
memcpy(secondArray, originalArray, 4*size);
return secondArray;}
void printIntArray(int size, int* intArray) {for (int i = 0; i < size; i++){cout << (i > 0 ? ", " : "") << intArray;}
cout << endl;}
Join a community of subject matter experts. Register for FREE to view solutions, replies, and use search function. Request answer by replying!
Post Reply