Passing unknown Array to Function by reference

Hey there.

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.

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’


ERRORS IN BOLD

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56

#include <iostream>
#include <array> - says no such file or directory

using namespace std;


void insertionSort(int (&A)[])
{
    for (int j=1; j <= A->size(); 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;
    }
}

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;
}
Last edited on
Your code won't compile, even if your function worked. You can't have a user-inputted size of an array unless the array is dynamic- that is, created using new[] and removed using delete[]. Which, if you were to do that, then you could just pass the pointer object to the function instead of the whole array.
(1) In the C language, arrays are by default passed by reference, so there is no need to explicitly state it in function calls.

(2) There is no .size() function that you can call on arrays.

(3) You have to hard code the size of an array before compilation, so line 31 is invalid because you cannot dynamically declare memory like that.

(4) You can dynamically declare memory however! What I imagine you want to do is dynamically allocate memory based on user input. You'll need to study up on pointers, and the new[] operator.

http://www.cplusplus.com/doc/tutorial/pointers/
http://www.cplusplus.com/reference/new/operator%20new%5B%5D/
Topic archived. No new replies allowed.