GregT
GregT

Reputation: 1099

extracting rows or columns from a 2D-array C++

I'd like to make a function that receives a 2-dimensional array and returns one of its rows ('which') as a simple array. I wrote this:

int *row(int *array, int lines, int columns, int which)
{
    int result[columns];

    for (int i=0; i<columns; i++)
    {
        result[i] = *array[which][i];
    }
    return result;
}

However, in line 7 I got the following error: invalid types 'int[int]' for array subscript. Any idea how to do this properly? I also tried to handle the 2D-array as an array of arrays, but didn't succeed. I'm novice, so please avoid too advanced concepts.

Thanks for the help!

UPDATE: thanks for the help! Now my code looks like:

int n;  //rows
int m;  //columns
int data[100][100];   
int array[100];

int *row(int *array, int rows, int columns, int which)
{
    int* result = new int[columns];
    for (int i=0; i<columns; i++)
    {
        result[i] = *array[which*columns+i];
    }
    return result;
    delete[] result;
}

int main()
{
    array=row(data, n, m, 0);
}

I still get an error in main: incompatible types in assignment of 'int*' to 'int [100]'

What could be the problem now? I also don't know where to use the delete[] function to free up the array.

Thank you very much for the help!

Upvotes: 1

Views: 10970

Answers (6)

Amna
Amna

Reputation: 21

double *row(double **arr, int rows, int columns, int which)
{
double* result = new double[columns];
for (int i=0; i<columns; i++)
{
    result[i] = arr[which][i];

}
return result;
delete[] result; 
}

This will return the row.

Upvotes: 0

Ivaylo Strandjev
Ivaylo Strandjev

Reputation: 70929

"array" is one dimensional. You can access the element with index [which][i] via: array[which*columns + i]. Also remove the asterisk as the array is only a single pointer.

EDIT: Also you can not return local array - you need to deal with dynamic memory:

int* result = new int[columns];

And then take special care to free this memory. Other option would be to use std::vector.

Upvotes: 2

Robᵩ
Robᵩ

Reputation: 168616

You can avoid all of this pointer arithmetic and memory allocation by using std::vector

#include <vector>
#include <iostream>

typedef std::vector<int> Row;
typedef std::vector<Row> Matrix;

std::ostream& operator<<(std::ostream& os, const Row& row) {
  os << "{ ";
  for(auto& item : row) {
    os << item << ", ";
  }
  return os << "}";
}

Row getrow(Matrix m, int n) {
  return m[n];
}

Row getcol(Matrix m, int n) {
  Row result;
  result.reserve(m.size());
  for(auto& item : m) {
    result.push_back(item[n]);
  }
  return result;
}

int main () {
  Matrix m = {
    { 1, 3, 5, 7, 9 },
    { 2, 4, 5, 6, 10 },
    { 1, 4, 9, 16, 25 },
  };

  std::cout << "Row 1: " << getrow(m, 1) << "\n";
  std::cout << "Col 3: " << getcol(m, 3) << "\n";  
}

Upvotes: 0

Jerry Coffin
Jerry Coffin

Reputation: 490108

The size of an array needs to be a compile-time constant.

Instead of messing with arrays, you should probably use std::vector (possibly along with a 2D matrix class).

Upvotes: 1

IamShifu
IamShifu

Reputation: 43

There are few errors that needs to be fixed first.

  1. You should never return a pointer to a local variable from a function. In the above code, you are trying to return a pointer to contents of 'result' which is a local variable.
  2. An array cannot be declared with a size that is variable, in your case the variable columns.
  3. If array is a two dimensional array, which i think is your intent, then array[which][i] gives you an int.You do not have to de-reference it.

Though I know I am not following the posting etiquette here, I recommend you to please start with a nice text book, grab the basics and come here when you encounter problems.

Upvotes: 1

chrisaycock
chrisaycock

Reputation: 37930

You can't just do this:

int result[columns];

You need a dynamic allocation:

int* result = new int[columns];

Also, your use of array looks wrong. If array is going to be a single pointer, then you want:

result[i] = array[which*columns + i];

Upvotes: 4

Related Questions