Reputation: 1099
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
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
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
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
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
Reputation: 43
There are few errors that needs to be fixed first.
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
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