Question: how to fix this code run to do everything in c++- #include using namespace std; // write the code that implemets the functions below //
how to fix this code run to do everything in c++- #include
// write the code that implemets the functions below
// The swapchar doesn't print or return any value, just swaps the 2 // characters at index i and j in the array a of size n. void swapchar(char *a,int n,int i,int j){ // insert your code here }
// The swap function takes in an array a[], of size n, // and swaps the content of the array at indexes i and j. // It returns false if there is no swap (i.e. if i and j are the same) // and true otherwise. // If there is a swap, for example swapping the first two elements in // [4,3,2,1] it also prints: // swapped 3 and 4 and array is now [3,4,2,1] bool swap(int *a,int n,int i,int j) { // insert your code here return true; }
// write a function called selectionSort that sorts an array of ints // a, of size n, either in descending order (e.g., 5,4,3...) when up // is true, or in ascending order (e.g., 3,4,5...) when up is false. // Every time a swap is made, it should add 1 to the variable swaps // (note that if swap() returns false, it should not increase swaps). // Every time a comparison is made, it should add 1 to the variable // comparisons. At the end it should print swaps and comparisons. // Also, every time elements in the int array a is swapped, the same // elements in the char array s should also be swapped. // The sorting algorithm should be Selection Sort. void selectionSort(int*a,char*s,int n,bool up){ int comparisons=0,swaps=0; // insert your code here cout
// Write a function with the same requirements as selectionSort, // but the sorting algorithm should be Bubble Sort. void bubbleSort(int*a,char*s,int n,bool up){ int comparisons=0,swaps=0; // insert your code here cout
// leave the code below in main as it is, write code only in the above functions int main() { int a[10]={4,1,3,2,0}; char s[]="ebdcafghij"; string type="selection"; int n=5; //simple hardcoded test for debugging bool sortUp=true; selectionSort(a,s,5,true); cout
cin>>type>>n>>sortUp; for(int i=0;i
if (type=="selection") { selectionSort(a,s,n,sortUp); } else if (type=="bubble") { bubbleSort(a,s,n,sortUp); } else if (type=="insertion") { insertionSort(a,s,n,sortUp); } cout




man() the way it is written, it first calls selection ort on a simple array to help you debug your code and verify that it works . The correct output before any input should be swapped 0 and 4 and array is now [0,1,3,2,4] swapped 2 and 3 and array is now [e,1,2,3,4] swaps 2, comparisons 10 abcdefghij after that, the code waits for unput, first the name of a sorting algonthm, then the size of an array up to 10, then a 0 for descending/descre easing order or 1 for ascending increasing, and then a set of numbers in the arr ay For example, if you copyipaste ths imput below selection 6 576493 (so selection sort, 6 stems, sort down [5,7,6,4,9,3]), you should get this output swapped 9 and 5 and array is now [9,7,6,4,5,3] swapped 5 and 4 and array is now [9,7,6,5,4,3] swaps-2, comparisons 15 ebcadfghij If instead you copy paste the same input but with sort up (1) selection 6 1 576 49 3 you should get this output swapped 3 and 5 and array is now [3,7,6,4,9,5] swapped 4 and 7 and array is now [3,4,6,7,9,5] swapped 5 and 6 and array is now [3,4,5,7,9,6] swapped 6 and 7 and array is now [3,4,5,6,9,7 swapped 7 and 9 and array is now [3,4,5,6,7,9] swaps 5,comparisons-15 dacbeghij would ustead be inscrtion sort insertion 6 1 576493 you should get ths output sHapped 7 and 6 and array is now 15,6,7,4,9,31 srapped 7 and 4 and array is now [S,6,4,7,9,3 insertion sort would instead be insertion 6 15 7649 3 you should get this output apped 7 and 6 and array is now [5,6,7,4,9 swapped 7 and 4 and array is now 15,6,4,7,9,3) swapped 6 and 4 and array is now [5,4,6,7,9,3] swapped 5 and 4 and array is now [4,5,6,7,9,31 swapped 9 and 3 and array is now [4,5,6,7,3,9] wapped 7 and 3 and array is now [4,5,6,3,7,9] swapped 6 and 3 and array is now [4,5,3,6,7,9] swapped 5 and 3 and array is now [4,3,5,6,7,9] swapped 4 and 3 and array is now [3,4,5,6,7,9 swaps 9, comparisons-9 fdacbeghij
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
