-1

I am aware that an array can be passed to a function in quite a few ways.

#include <iostream>
#include <utility>

using namespace std;

pair<int, int> problem1(int a[]);

int main()
{
    int a[] = { 10, 7, 3, 5, 8, 2, 9 };
    pair<int, int> p = problem1(a); 
    cout << "Max =" << p.first << endl;
    cout << "Min =" << p.second << endl;
    getchar();
    return 0;
}

pair<int,int> problem1(int a[])
{
    int max = a[0], min = a[0], n = sizeof(a) / sizeof(int);

    for (int i = 1; i < n; i++)
    {
        if (a[i]>max)
        {
            max = a[i];
        }
        if (a[i] < min)
        {
            min = a[i];
        }

    }

    return make_pair(max,min);


}

My code above passes only the first element while it should be passing an array (or technically, a pointer to the array) and hence, the output is 10, 10 for both max and min (i.e. a[0] only).

What am I doing wrong, I guess this is the correct way.

Yu Hao
  • 119,891
  • 44
  • 235
  • 294
adrian008
  • 395
  • 6
  • 18

4 Answers4

5

The contents of the array are being passed to the function. The problem is:

n = sizeof(a) / sizeof(int)

Does not give you the size of the array. Once you pass an array to a function you can't get its size again.

Since you aren't using a dynamic array you can use a std::array which does remember its size.

You could also use:

template <int N>
void problem1(int (&a) [N]) 
{
    int size = N;
    //...
}
NathanOliver
  • 171,901
  • 28
  • 288
  • 402
4

No, you simply cannot pass an array as a parameter in C or C++, at least not directly.

In this declaration:

pair<int, int> problem1(int a[]);

even though a appears to be defined as an array, the declaration is "adjusted" to a pointer to the element type, so the above really means:

pair<int, int> problem1(int* a);

Also, an expression of array type is, in most contexts, implicitly converted to a pointer to the array's initial element. (Exceptions include an array as the operand of sizeof or unary &). So in a call to the above function:

int arr[10];
problem1(arr);

the array expression arr is equivalent to &arr[0], and that address (pointer value) is what's passed to the function.

Of course you can write code that does the equivalent of passing an array. You can make the array a member of a structure (but then it has to be of fixed length). Or you can pass a pointer to the initial element and pass a separate parameter containing the actual length of the array object.

Or you can use one of the C++ standard library classes that implement array-like data structures; then the length can be taken directly from the parameter.

I highly recommend reading section 6 of the comp.lang.c FAQ, which covers arrays and pointers. It's applicable to C++ as well (though it doesn't mention the C++ standard library).

Keith Thompson
  • 254,901
  • 44
  • 429
  • 631
0

In C++ language a function parameter declared as int a[] is immediately interpreted as and is equivalent to int *a parameter. Which means that you are not passing an array to your function. You are passing a pointer to the first element of an array.

Trying to apply the sizeof(a) / sizeof(int) technique to a pointer is useless. It cannot possibly produce the size of the argument arraay.

AnT stands with Russia
  • 312,472
  • 42
  • 525
  • 765
0

One alternative that hasn't been mentioned is writing your code as a template, and passing the array by reference so the template can deduce the size of the array:

template <class T, size_t n>
pair<T, T> problem1(T(&a)[n]) {
    T max = a[0], min = a[0];

    for (size_t i = 1; i < n; i++) {
        if (a[i]>max) {
            max = a[i];
        }
        if (a[i] < min) {
            min = a[i];
        }
    }
    return make_pair(max, min);
}

Note, however, that this will only work if you pass a real array, not a pointer. For example, code like this:

int *b = new int[10];

for (int i = 0; i < 10; i++)
    b[i] = rand();

auto result = problem1(b);

...won't compile at all (because we've defined problem1 to receive a reference to an array, and b is a pointer, not an array).

Jerry Coffin
  • 476,176
  • 80
  • 629
  • 1,111