0

I need help passing an array to a function by reference. The array is created beforehand being passed to the function but the value of the array after being passed through the function is not what I want - it remains unchanged. The code is shown below. The function is to take an array, a position p and a value val. The array is assumed to be sorted in ascending order up to position p and the value val must be placed such that the array is sorted in ascending order up to position p+1.

#include <iostream>
using namespace std;

// Function that takes an array a, a position,
// and a float value val to insert.
// a must already be sorted in acsending order
// up to p. val is then inserted such that
// the a is sorted up to p+1.
// p = 0 means position a[0].
void insert(double (&a)[], int aSize, int p, double val)
{
    try {
        // Throw error if p > size of array
        if(p > aSize || p < 0) {
            throw logic_error("Position is greater than size of array or less than zero.");
        }

        int newSize = aSize + 1;
        double* vTemp = new double[newSize]; // create new bigger array
        for(int i = 0; i <= p; i++) {
            if(val >= a[i]) {
                vTemp[i] = a[i];
            } else {
                vTemp[i] = val;
                for(int j = i + 1; j < newSize; j++) {
                    vTemp[j] = a[j - 1];
                }
                break;
            }
        }
        cout << "Size of vTemp = " << newSize << endl;
        for (int k = 0; k < newSize; k++){
            cout << "vTemp[" << k << "] = " << vTemp[k] << endl;
        }
        a = vTemp;
        delete[] vTemp;

    } catch(const logic_error& e) {
        cout << "Error in input: " << e.what() << endl;
    }
}

int main()
{
    // Declare variables
    double myArray[] = { 1, 2, 4, 8, 16, 20, 50, 30, 153 }; // sample array to test function
    int p = 5;                                              // position
    double val = 7.2;                                       // value to insert
    int arraySize = sizeof(myArray) / sizeof(myArray[0]);   // no. of elements in array
    int newSize = 0;                                        // size of expanded matrix

    // Insert val
    insert(myArray, arraySize, p, val);
    cout << "Size of original array: " << arraySize << endl;

    // Display new expanded matrix
    newSize = sizeof(myArray) / sizeof(myArray[0]); // size of expanded matrix
    cout << "Size of expanded array: " << newSize << endl << endl;

    for(int i = 0; i < newSize; i++) {
        cout << myArray[i] << " ";
    }
    cout << endl;

    // Return success
    return 0;
}
2
  • It's largely pointless to pass a C-array by reference. Values not changing is a different problem than whether you're passing by value or not. Commented Feb 8, 2021 at 17:22
  • 1
    Having looked at the code now, your issue has nothing to do with passing an array by reference or not. It's that you don't know enough about arrays. You start with a stack-allocated array and think that you can transform it into a heap-allocated array. That's not the case at all. Commented Feb 8, 2021 at 17:34

2 Answers 2

2

Don't use raw arrays. The class you are looking for is called std::vector (reference).

Just create a vector instead of an array and pass it by reference and you get what you need.

Sign up to request clarification or add additional context in comments.

Comments

0

The problem doessn't come from the array being passed by reference.

The issues is assigning a dynamic array to a built-in array. In the case above, when the following statement is executed.

a = vTemp;

The "array , which by that point is a pointer reference" will change is value to point to the dynamic array.

But don't forget that you are deleting the array.

delete[] vTemp;

you are not actually copying each element that the dynamic allocated array have into your array. when execution goes back to main, it points to your normal array.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.