arsaKasra
arsaKasra

Reputation: 189

Passing rows and columns to functions

I've written a function with a one dimensional array as input. In the main() part I need to pass rows of a two dimensional array as inputs to that function. I have no idea if that is possible and how (it is possible in Matlab, but in C++, I don't know). So, I appreciate it if any one will tell me.

Upvotes: 1

Views: 7820

Answers (3)

JSQuareD
JSQuareD

Reputation: 4786

Passing a column to a function can be done quite easily:

void gimmeColumn(int x[]) {}

int main() {
    int table[10][10];
    int columnToPass = 3;
    gimmeColumn( table[columnToPass] );
}

However, passing a row in a similar fashion is not possible. You could consider writing your own class to facilitate this behavior.

template<type T>
class SelectableTable {
private:
    T** table;
    int rows, columns;
public:
    SelectableTable( int numRows, int numColumns )
    :
        rows( numRows ),
        columns( numColumns )
    {
        table = new T*[columns];
        for( int i=0; i<columns; i++ )
            table[i] = new T[rows];
    }

    ~SelectableTable() {
        for( int i=0; i<columns; i++ )
            delete[] table[i];
        delete[] table;
    }

    T* operator[](int column) {
        return table[column];
    }

    int getRows() {
        return rows;
    }

    int getColumns() {
        return columns;
    }

    TableRow selectRow(int row) {
        return TableRow<T>( *this, row );
    }
};

template<type T>
class TableRow {
private:
    int row;
    SelectableTable<T>& table;
public:
    TableRow( SelectableTable<T>& fromTable, int selectedRow )
    :
        table(fromTable),
        row(selectedRow)
    {}

    T& operator[](int column) {
        return table[column][row];
    }

    int size() {
        return table.getColumns();
    }
};

And then you can simply use this as such:

void gimmeRow( TableRow<int> row ) {
    for( int i=1; i<row.size(); i++ )
        row[i] = row[i-1];
}

int main() {
    SelectableTable<int> table(10,10);
    int rowToPass = 3;
    gimmeRow( table.selectRow(rowToPass) );
}

Of course, to get this code to work, you should add some forward declarations, best using headers. For uniformity, you could also implement a class TableColumn.

Upvotes: 1

Joseph Mansfield
Joseph Mansfield

Reputation: 110748

I presume your function looks something like this:

void function(int row[]) { ... }

The first important thing to understand is that there's no such thing as a parameter of array type. The row parameter above may look like an array, but it's not. In fact, it's a pointer equivalent to int* row.

The second thing to understand is that the name of an array can be implicitly converted to a pointer to its first element. Let's say you have an array int array[] = {1, 2, 3, 4, 5};, you could do function(array) to pass it to the function. However, you're not actually passing the array. What's really happening is that the array argument being passed is being converted to a pointer to its first element - so a pointer to the value 1 in the array. As we've just seen, the function parameter row is really a pointer, so it's fine to pass this pointer to the first element to it.

Now, I imagine you have a two dimensional array that looks something like:

int array[N][M] = { ... };

If we consider N to be the number of rows and M to be the number of columns (you could imagine it either way round, but this is consistent with your matlab knowledge), you know that array[0][1] will access the second element on the first row (because we index from 0 in C++). If you just provide a single subscript, like array[0], this represents an entire row of the two dimensional matrix. That expression, array[0], is an array of ints. Since this array of ints can be converted to a pointer to its first element, you can pass array[0] and the function will receive, as row, a pointer to the first element of the first row (or 0th row).

So you can then do:

function(array[0]);

However, because the row is really a pointer, inside the function you've lost any idea about what size the array actually is. It is typical to pass along the size of the array at the same time:

void function(int row[], int size);

function(array[0], M);

However, it is generally not the best practice in idiomatic C++ to use array types like this. We prefer to use containers provided by the standard library. For example, a std::array<std::array<int, M>, N> is a nice fixed size 2-dimensional array analog:

void function(std::array<int, M> row);

std::array<std::array<int, M>, N> array;
// Fill array...
function(array[0]);

Now you don't have to pass along the size of the array, because the std::array and other standard container types provides mechanisms for getting the size.

Upvotes: 1

SomeWittyUsername
SomeWittyUsername

Reputation: 18368

Assuming your function signature is void foo(int a[N]) and your input array is defined as int x[M][N], you can pass the rows of it to foo by calling foo(x[i]), where i denotes the line number of your 2D array.

Upvotes: 2

Related Questions