passing 2D arrays into a function - c++

I want to pass two 2D arrays into a function in order to copy the whole array. I am using double pointers to do this but it is showing error.
void copy_arr(float **new_table,float **table,int m,int n)
{
//code
return;
}
It is showing error for the 2D array 'new_table' only.
The function calling :
void optimize(float **table,int m,int n)
{
int pivot[2];
find_pivot(table,m,n,pivot);
float new_table[m][n];
//code
copy_arr(new_table,table,m,n);
return;
}
error: cannot convert 'float (*)[(((sizetype)(((ssizetype)n) + -1)) + 1)]' to 'float**' for argument '1' to 'void copy_arr(float**, float**, int, int)'

In C doesn't natively exist the concept of multidimensional array. I.e. what we call a bidimensional array:
float fArray[10][10];
Is in reality interpreted by compiler as an array of 10 arrays of float. For this reason the operator [], that can only retrieve values from single arrays, needs to know all following dimensions to access a single element.
Said that you have two ways to pass a multidimensional array, the first case apply when the second dimension is known (extending the concept to multidimensional arrays all other dimensions, but the first, must be know):
void copy_arr(float new_table[][10], float table[][10], int m)
{
//code
return ;
}
If the dimensions are variable you must compute yourself the pointer to the element:
void copy_arr(float *new_table, float *table, int m, int n)
{
//code
memcopy (new_table, table, sizeof(float)*m*n);
//Access last element assuming table[m][n]
printf("This is the last element: %f\n", table + ((m-1)*n + (n-1))*sizeof(float));
return ;
}
In C++ you can use std::vector

Related

How to send a 2D array as a parameter to a recursive function?

I have a trivial test function that takes a row and column index for a 2D array, and checks to see if the value of the 2D at that location is 2. If not, it checks the next row index until it has checked every element along that row for the value 2:
1 bool rowContainsTwo(const int rowIndex, const int columnIndex, const int size, const int grid[size][size]) {
2 if (grid[rowIndex][columnIndex] == 2)
3 return true;
4 else
5 return (rowIndex + 1 < size) ? rowContainsTwo(rowIndex + 1, columnIndex, size, grid) : false;
6 }
When I compile this test function, I get the following compile error:
$ g++ -std=c++11 test.cpp -o test
test.cpp:5:84: error: cannot initialize a parameter of type 'const int (*)[*]' with an lvalue of type 'const int (*)[size]'
return (rowIndex + 1 < size) ? rowContainsTwo(rowIndex + 1, columnIndex, size, grid) : false;
^~~~
test.cpp:1:90: note: passing argument to parameter 'grid' here
bool rowContainsTwo(const int rowIndex, const int columnIndex, const int size, const int grid[size][size]) {
^
1 error generated.
I have seen all sorts of articles about passing 2D arrays as parameters of functions, but I have yet to see anything that tells me about this error I am getting, and how to pass the array to the function recursively.
I was hoping for a straightforward answer to how to pass a static array recursively as a parameter, without having to make it a dynamic array.
Note, I don't need to modify the array elements, I just need to look at the elements.
Also note that I am aware using a vector would be better, and that I could use for loops and all sorts of iterative methods - the constraints are set that I want to do this with a static 2D array, and I want to pass the array recursively as a parameter.
Thanks for reading!

Using a 2-d Array in a function

I am trying to solve a dynamic programming problem and I need to take the user input in the form of a 2-d array and use the values from the 2-d array inside the function.
The values of the 2-d array will not be changed when used inside the function.
In the function int dp i am getting the
error:
declaration of 'a' as multidimensional array must have bounds for all dimensions except the first
int max(int a,int b,int c)
{
if(a>=b && a>=c)return a;
if(b>=c && b>=a)return b;
else return c;
}
int max2(int a,int b)
{
if(a>b)return a;
else return b;
}
int dp(int i,int j,int a[][],int p,int q)
{
if((i-1)>=0 && (j-1)>=0 &&(i+1)<p &&(j+1)<q )
return max(a[i][j]+dp(i-1,j+1,a,p,q),a[i][j]+dp(i+1,j+1,p,q),
a[i][j]+dp(i,j+1,p,q));
if(i==0 && j!=0 && (j+1)<q)
return max2(a[i][j]+dp(i+1,j+1,p,q),a[i][j]+dp(i,j+1,p,q));
}
int main()
{
int p,q,r,s,T,a,b,i,j,k;
scanf("%d",&T);
for(a=0;a<T;a++)
{
scanf("%d %d",p,q);
int z[p][q];
int max=0;
for(i=0;i<q;i++)
{
for(j=0;j<p-1;j++)
scanf("%d ",&z[j][i]);
scanf("%d",&z[j+1][i]);
}
for(i=0;i<p;i++)
{
if(dp(i,0,z,p,q)>max)
max=dp(i,0,z,p,q);
}
}
}
It's all in the error message:
declaration of 'a' as multidimensional array must have bounds for all dimensions except the first
Your function signature does not have bounds for a's 2nd dimension:
int dp(int i,int j,int a[][],int p,int q)
// ^^^^^
You need to fill it in with a[][N] where N is whatever the correct bound is. The issue is that you are using VLAs here:
scanf("%d %d",p,q);
int z[p][q];
That is non-standard C++, and basically means you cannot write the signature of dp, since the second bound has to be known as a compile-time constant. You could either make it a single-dimensional array:
int* z = new int[p*q];
int dp(int i, int j, int* a, int p, int q)
// ^^^^^^
or dynamically allocate it in 2 dimensions and just pass it in that way:
int** z = new int*[p];
for (int i = 0; i < p; ++i) {
z[i] = new int[q];
}
int dp(int i, int j, int** a, int p, int q)
// ^^^^^^^
The function dp needs some information to perform meaningful index calculations, either done by the compiler or in the actual inplementation. Either a dimension must be specified in the type or the argument a could be of type int** while its dimensions are provided as separate arguments to dp. As this is C++, a type of std::vector< std::vector< int > > might be more suitable for the task.
You get that error because you cannot leave both the index(row,column) empty in int a[][] in your function declaration. You must have both specified or atleast the value of column index.
Use dynamic declaration
int **z = new int*[p];
for (int i = 0; i < p; i++)
z[i] = new int[q];
Change the parameter int a[][] to int **a
You can't dynamically declare an array on the stack as the size has to be known at compile time. The only way to do this would be by allocating memory for the array on the heap using the new keyword, then you could declare the size at run time.
Far easier, however, would be just to use a container class, or in your case, a container of containers like a vector of vector of ints;
#include <vector>
vector< vector<int> > arrArray(rows, vector<int>(columns));
The syntax might look a bit strange, but breaking it down;
vector<int> - a vector of type int
vector< vector<int> > - a vector of vectors of type int
arrArray(rows, vector<int>(columns)); - here in the first parameter, we are saying; create rows number of vector<int>'s in our array, and the second parameter initalises the array to some value. If it were just a 2D array of int, we might initalise it to 0, or omit the second parameter and rely on the default value of int. But, because our multidimensional vector also contains vectors, we set each row of our main vector to store a vector of int's which holds columns amount of integers.
Now you can access the array like you would any other;
arrArray[2][0] = 5;
You also get all the added benefits that container classes contain, including iterators and a lot of useful class methods for manipulating and checking your array. Once you understand the syntax of creating container classes, you'll find them much easier to work with than arrays. You also don't have to worry about having to manage your own memory, and have the ability to do bounds checking before accessing vector elements.

Function call with 2d array as argument

I have to call a function which passes 2d array as argument. Function call:
int n;
char ch;
cin>>n;
bool b[n][n];
for(int i=0;i<n;i++){
for(int j=0;j<n;j++){
cin>>ch;
if(ch=='X'){b[i][j]=1;} //reads input from a file
}
}
for(int i=0;i<n;i++){
for(int j=0;j<n;j++){
cout<<evaluate(n,b, i, j); //shows error no matching function for call to evaluate
}
cout<<endl;
}
However,it displays an error no matching function to the call
Here's my function evaluate's declaration:
int evaluate(int n,bool** b,int x,int y){
//body
}
I have tried variations in function declaration as:evaluate(int n,bool b[n][n],int x,int y);
but it gives the same error. Also on removing the 2d array argument,the function works.
Please suggest what my mistake is.
Array names converted to pointer to first element of array when passed to a function. A 2D array is a 1D array having all its elements of type 1D array, i.e it is an array of arrays.
When 2D array name passed to a function then it decays to pointer to first element of the array. As explained above, the element itself is a 1D array, therefore the type of array name becomes pointer to array.
pointer to array and pointer to pointer both are of different types (incompatible with each other). To pass a pointer to array type, you need to declare your function as
int evaluate(int n, bool (*b)[n], int x, int y);
or simply
int evaluate(int n, bool b[][n], int x, int y);
What you need to understand is that a 2D array (a variable declared as int foo[m][n];) is something very different from a double pointer (a variable of type int **foo;). Even though you may use foo[i][j] in both cases, what happens is radically different:
In the case of a 2D array, foo[i][j] is effectively evaluated as foo[i*m + j], all the lines of the 2D array are contiguous in memory, and there is no index structure.
In the case of a double pointer, foo[i][j] first loads a pointer from memory at foo[i], then it loads an int from behind that loaded pointer. That is, foo must point to an array of pointers, that indices into the different line arrays. The line arrays may be independent of each other, they are connected via the index array.
Consequently, you cannot just pass a 2D array as an int** to a function since there is no index array in the 2D array.
However, you can pass a pointer to an array (pseudocode, I omitted declaring some variables):
const int width = ...;
void foo(int (*twoDArray)[width], int x, int y) {
//do something with twoDArray[x][y]
}
int main() {
int myArray[height][width];
foo(myArray, x, y);
}
Unfortunately, width has to be a compile time constant in C++. C can handle dynamic array size, C++ cannot. This severely limits the usability of this approach.
Do you defined your function (callee) before main (caller) function?
you should do like this:
int evaluate(int n,bool** b,int x,int y);
int main(){
//some code
evaluate(n, b, i, j);
//some code
}
int evaluate(int n,bool** b,int x,int y){
//body
}
or this way:
int evaluate(int n,bool** b,int x,int y){
//body
}
int main(){
//some code
evaluate(n, b, i, j);
//some code
}

Using two dimensional arrays as parameters

I am trying to make some functions working on two dimensional arrays:
void display_matrix(int**, int, int);
void gen_matrix(int**, int, int);
int main()
{
srand(time(0));
int m=5, n=3;
int my_matrix[m][n];
gen_matrix(my_matrix, m, n);
display_matrix(my_matrix, m, n);
}
I don't know what's wrong, but I get the following error when I call the functions:
[Error] cannot convert 'int ()[(((sizetype)(((ssizetype)n) + -1)) + 1)]' to 'int*' for argument '1' to 'void gen_matrix(int**, int, int)'
I know I can use vector but I am trying to practise and remember the use of pointers and arrays.
Declaring a matrix in the form <type> <name>[<dim1>][<dim2>] defines a block of memory with an implicit stride of dim1. Internally elements are accessed by using multiples of dim1 to reach the correct row and offsetting from there by the second dimension.
the type <type> <name>** is a pointer to an array of pointers - very different. The structure consists of an array of pointers to rows of data. These have to be allocated and linked appropriately before calling the subroutine. There is also no requirement that they are contiguously allocated, and an indirect lookup needs to be done to each an element on each new row.
The advantage is that the rows can be different lengths suiting some algorithms that do not have rectangular structure.
Change the code the following way
const int n = 3;
void display_matrix( int ( * )[n], int );
void gen_matrix( int ( * )[n], int);
int main()
{
srand(time(0));
const int m = 5;
int my_matrix[m][n];
gen_matrix( my_matrix, m );
display_matrix(my_matrix, m );
}
Or you can keep your functions as defined but call them for example as
gen_matrix( reinterpret_cast<int **>( my_matrix ), m, n);

Passing 2-D array as argument

I am trying to pass a 2-d array to a function which accept a pointer to pointer. And I have learnt that a 2-d array is nothing a pointer to pointer(pointer to 1-D array). I when I compile the below code I got this error.
#include<iostream>
void myFuntion(int **array)
{
}
int main()
{
int array[][]= {{1,2,3,4},{5,6,7,8,9},{10,11,12,13}};
myFuntion(array);
return 0;
}
In function 'int main()':
Line 5: error: declaration of 'array' as multidimensional array must have bounds for all dimensions except the first
compilation terminated due to -Wfatal-errors.
Can anybody clear my doubt regarding this and some docs if possible for my more doubts.
void myFunction(int arr[][4])
you can put any number in the first [] but the compiler will ignore it. When passing a vector as parameter you must specify all dimensions but the first one.
You should at least specify the size of your second dimension.
int array[][5] = { { 1, 2, 3, 4 }, { 5, 6, 7, 8, 9 }, { 10, 11, 12, 13 } };
There is also an error which is often repeated. To pass a 2D array as argument, you have to use the following types:
void myFuntion(int (*array)[SIZE2]);
/* or */
void myFuntion(int array[SIZE1][SIZE2]);
Why don't use std::vector instead of "raw" arrays. Advantages:
1. It can dynamically grow.
2. There is no issues about passing arguments to the function. I.e. try to call void myFuntion(int array[SIZE1][SIZE2]); with array, that has some different sizes not SIZE1 and SIZE2
Another templated solution would be:
template<int M, int N>
void myFunction(int array[N][M])
{
}
#include<iostream>
void myFuntion(int arr[3][4]);
int main()
{
int array[3][4]= {{1,2,3,4},{5,6,7,8},{10,11,12,13}};
myFuntion(array);
return 0;
}
void myFuntion(int arr[3][4])
{
}
http://liveworkspace.org/code/0ae51e7f931c39e4f54b1ca36441de4e
declaration of ‘array’ as multidimensional array must have bounds for all dimensions except the first
So you have to give
array[][size] //here you must to give size for 2nd or more
For passing the array in function , array is not a pointer to a pointer but it's pointer to an array so you write like this
fun(int (*array)[])
Here if you miss the parenthesis around (*array) then it will be an array of pointers
because of precedence of operators [] has higher precedence to *