I have spent a good hour trying to figure this out - how do I write this function (at top of code - insertionSort) that allows me to pass an array by reference to it. In a way that allows me to call '.size' on the array. It has to be an array for this assignment.
I have tried not passing it by reference, dereferencing the array before calling size on it, etc. I keep getting errors :(.
This is the most recent compiler error for this code:
insertionSort.cpp:11: error: parameter ‘A’ includes reference to array of unknown bound ‘int []’ insertionSort.cpp: In function ‘void insertionSort(int (&)[])’: insertionSort.cpp:13: error: request for member ‘size’ in ‘(int)A’, which is of non-class type ‘int’
#include <iostream>
//#include <array> - says no such file or directory
using namespace std;
void insertionSort(int (&A)[]) <-----ERROR HERE
{
for (int j=1; j <= A->size(); j++) <-----ERROR HERE
{
int key = A[j];
//now insert A[j] into the sorted sequence a[0...j-1].
int i = j-1;
while (i >= 0 && A[i] > key)
{
A[i+1] = A[i];
i -= 1;
}
A[i+1] = key;
}
}
int main()
{
int Asize = 0;
cout << "Hello. \nPlease enter a number value for the insertionSort Array size and then hit enter: " << endl;
cin >> Asize;
int A[Asize];
char Atype;
cout << "There are three ways to order your inserstionSort array; \nb - for best case \nw - for worst case \na - for average case" << endl << "Which type do you desire for this array? \nPlease enter 'b', 'w', or 'a': " << endl;
cin >> Atype;
if (Atype == 'b')
{
cout << "You have chosen type b." << endl;
}
else if (Atype == 'w')
{
cout << "You have chosen type w." << endl;
}
else if (Atype == 'a')
{
cout << "You have chosen type a." << endl;
}
cout << "Terminate Program" << endl;
}