Question: C + + . I have a project with a header file, Sort.h , and a . cpp file, Driver.cpp . There's an error in

C++. I have a project with a header file, Sort.h, and a .cpp file, Driver.cpp. There's an error in the Driver and I'm not sure how to fix it. Specifically int arr[i] needs a constant value. I would appreciate any help in fixing this and I would greatly appreciate an explanation. Sort.h #include
#include
#include
#include
using namespace std;
void swap(int& a, int& b){//swap the content of a and b
int temp;
temp = a;
a = b;
b = temp;
}
void insertionSort(int* array, int size){
int key, j;
for (int i =1; i < size; i++){
key = array[i];//take value
j = i;
while (j >0 && array[j -1]> key){
array[j]= array[j -1];
j--;
}
array[j]= key; //insert in right place
}
}
void selectionSort(int* array, int size){
int i, j, imin;
for (i =0; i < size -1; i++){
imin = i; //get index of minimum data
for (j = i +1; j < size; j++)
if (array[j]< array[imin])
imin = j;
//placing in correct position
swap(array[i], array[imin]);
}
}
void bubbleSort(int* array, int size){
for (int i =0; i < size; i++){
int swaps =0; //flag to detect any swap is there or not
for (int j =0; j < size - i -1; j++){
if (array[j]> array[j +1]){//when the current item is bigger than next
swap(array[j], array[j +1]);
swaps =1; //set swap flag
}
}
if (!swaps)
break; // No swap in this pass, so array is sorted
}
} Driver.cpp: #include "Sort.h"
int main()
{
//time variables
clock_t start, stop;
//create different array sizes
for (int i =100; i <=100000; i *=10){
printf("
Array of size %d
", i);
int arr[i];
for (int j =0; j < i; j++){
//generate random numbers for array
arr[j]= rand();
}
//for insertionSort
start = clock();
insertionSort(arr, i);
stop = clock();
printf("
Insertion sort: %lfus", (double)stop - start / CLOCKS_PER_SEC);
for (int j =0; j < i; j++){
//generate random numbers for array
arr[j]= rand();
}
//for bubbleSort
start = clock();
bubbleSort(arr, i);
stop = clock();
printf("
Bubble sort: %lfus", (double)stop - start / CLOCKS_PER_SEC);
for (int j =0; j < i; j++){
//generate random numbers for array
arr[j]= rand();
}
//for selectionSort
start = clock();
selectionSort(arr, i);
stop = clock();
printf("
Selection sort: %lf",(double)stop - start / CLOCKS_PER_SEC);
}
return 0;
}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!