2

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;
}
Jarod42
  • 203,559
  • 14
  • 181
  • 302
SS Android
  • 31
  • 1
  • 4
  • This isn't Java. Native arrays don't have a `size()` member. – WhozCraig Oct 09 '14 at 03:24
  • 2
    if the size isn't fixed at compile time, you should use an `std::vector<>` – quantdev Oct 09 '14 at 03:26
  • 1
    You cannot "call .size on the array". Built-in arrays in C++ are not classes, they do not have methods. You cannot "call" anything on the array. And it is generally impossible to determine array size if you don't know that size already. – AnT stands with Russia Oct 09 '14 at 03:31
  • @AndreyT it's always possible to determine the size of a named array – M.M Oct 09 '14 at 08:23
  • 2
    @Matt McNabb: Not really. A pointer or reference to an array of unknown size (as the one in the example above) can be properly initialized (even if it will require a cast), and then used to legally access the content of some named array. Yet it provides you with no means to determine the actual size of that array. – AnT stands with Russia Oct 09 '14 at 14:08

4 Answers4

1

It's important to remember that C array's are just pointers to the first element of the array. Passing the array is easy, you would just do something like:

void foo(int *array)

or

void foo(int array[])

However, since it's just a pointer to it's base type it has no member functions to call and it has no clue what the memory structure looks like beyond it (ie. has no concept of length). If you wanted to know the length of a dynamic array that was passed then you need to pass the length as a second parameter, presumably whatever created the array should know its length.

void foo(int *array, unsigned int length)

Or, you can avoid all of this and use vectors which are conceptually similar to an ArrayList in java.

Kyle W
  • 64
  • 5
  • Great. That clears things up A LOT. Thank you. So there is no way to do this without adding another parameter? – SS Android Oct 09 '14 at 03:57
  • Check out vectors, they will make your life oh so much easier. http://www.cplusplus.com/reference/vector/vector/ – Kyle W Oct 09 '14 at 04:00
  • And I want to remember you that the statement 'C array's are just pointers' is just like saying that the Earth is flat (and it's not). – AnArrayOfFunctions Mar 27 '15 at 15:22
1

When you do:

std::cin >> Asize;
int A[Asize]; // Not standard

You use extension of your compiler to use VLA (Variable length array). prefer to use std::vector instead (and then you have void insertionSort(std::vector<int> &v)).

if you cannot use std::vector, you may use:

std::unique_ptr<int[]> A(new int [Asize]);

As the size is known only at runtime, you have to pass the size to your function:

void insertionSort(int* a, std::size_t size)

and call insertionSort as follow:

insertionSort(A.get(), ASize);

With a known compile time size of array,

void insertionSort(int (&A)[42])

is the right way to pass array by reference.

Jarod42
  • 203,559
  • 14
  • 181
  • 302
0

Arrays can be passed by reference, for example:

void somefunc(int (&arr)[30]) {}

This will ensure that you cannot pass any other size for this array (fixed size array): So, you cannot do this:

int a[40];
func(a); // compilation error

However, arbitrary sized array is also possible to pass by reference, for example:

template<typename T, size_t N>
void somefunc2(T (&arr)[N])
{
    // N can be used as size, as required, instead of querying size of the array
}

So, corrected function is as below:

template<typename T, size_t N>
    void insertionSort(T (&A)[N]) // ok, now
    {
        for (size_t j=1; j < N; j++) 
        {
            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;
        }
    }
Dr. Debasish Jana
  • 6,980
  • 4
  • 30
  • 69
-6

Try using Array.length that should work it does in Borland c++ builder

h3x0m3ga
  • 14
  • 2
  • I just tried doing .length instead and I got the same error, but with the word "length" in place of "size" haha – SS Android Oct 09 '14 at 03:32