C++: Pass two-dimensional array to function - c++

This is probably so simple, but I can't figure out why this won't compile.
void display(int);
const int rows = 2;
const int cols = 2;
int main()
{
int ray[rows][cols] = {{1,2 },
{3,4}};
display(ray);
return 0;
}
void display(const int ray[][cols]){
for(int i = 0; i < 2; i ++){
for(int j = 0; j < 2; j ++){
cout << ray[i][j] << endl;
}
}
}
invalid conversion from ‘int (*)[2]’ to ‘int’ [-fpermissive]|

This code will work
const int rows = 2;
const int cols = 2;
void display( const int ray[][cols]);
int main()
{
int ray[rows][cols] = {{1,2 },
{3,4}};
display(ray);
return 0;
}
void display( const int ray[][cols]){
for(int i = 0; i < 2; i ++){
for(int j = 0; j < 2; j ++){
cout << ray[i][j] << endl;
}
}
}
Your function definition and function declaration do not match, one of them is of type int while the other is type void
Your function declaration is
void display(int);
and the definition is
int display(const int ray[0][cols])
and
int display(const int ray[0][cols])
^
0 ?

I think I see the problem here
The prototype is wrong. If you want a 2D array arg, it's more like this
int display(const int ray**);

The forward declaration of display is wrong. Either fix forward declaration or change order of functions:
#include <iostream>
using namespace std;
const int rows = 2;
const int cols = 2;
int display(const int ray[0][cols]){
for(int i = 0; i < 2; i ++){
for(int j = 0; j < 2; j ++){
cout << ray[i][j] << endl;
}
}
}
int main()
{
int ray[rows][cols] = {{1,2 },
{3,4}};
display(ray);
return 0;
}
Live Example

Since you use global variables anyway, the easy thing is to change your declaration of display:
Declare display like this:
void display(int** array)
{...}
In this case you will actually be able to send your 2D array to the function because the type will match. A 2D array is an array of arrays and an array is just a pointer associated with some memory.

I am not giving the appropriate answer to this question. But an alternative.
As C++ suggests to use std::string inplace of char* . It also suggests to use vectors instead of array wherever applicable.
#include <iostream>
#include <vector>
void display( std::vector<std::vector<int>> );
int main()
{
std::vector<std::vector<int>> int_vec{ { 1 , 2 } , { 3 , 4 } };
display( int_vec );
system("PAUSE");
return EXIT_SUCCESS;
}
void display( std::vector<std::vector<int>> integer_vector )
{
for( auto& i : integer_vector )
{
for( auto& j : i )
{
std::cout << j << std::endl;
}
}
}
The global variables as rows and cols are gone :).

Related

declaring a function with arrays

First of all, im a c++ noob! Ok with that being said, i need to declare a function that initializes a grid. The function takes an array of int as the input and needs to return an array of int. I have:
array<int> InitializeGrid (array<int>)
{
const int NB_ROWS = 10;
const int NB_COLUMN = 10;
const int WATER = 0;
int grid[NB_ROWS][NB_COLONN];
for (int i = 0; i < NB_ROWS; i++)
{
for (int j = 0; j < NB_COLONN; j++)
{
grid[i][j] = WATER;
cout << grid[i][j] << " ";
}
cout << endl;
}
return ??
}
You don't need to return anything if you pass the array by reference:
#include <array>
#include <iostream>
static const int NB_ROWS = 10;
static const int NB_COLUMN = 10;
static const int WATER = 0;
void InitializeGrid (std::array<std::array<int, NB_COLUMN>, NB_ROWS> &grid)
{
for (auto &row : grid)
{
for (auto &col : row)
{
col = WATER;
std::cout << col << " ";
}
std::cout << '\n';
}
}
int main()
{
std::array<std::array<int, NB_COLUMN>, NB_ROWS> grid;
InitializeGrid(grid);
}
btw, if your WATER is 0 it is sufficive to write
std::array<std::array<int, NB_COLUMN>, NB_ROWS> grid{};
to initialize all elements to zero.

Copying one array to another using pointers

I have to use pointers to copy values of one array to another. The problem is I'm not allowed to use'[ ]' operators, which makes this more difficult for me. Here is my attempt:
#include <iostream>
using namespace std;
void cpyia(int old_array[],int new_array[],int length){
int *p1 = old_array;
int *p2 = new_array;
int *x = p2;
for(int i=0 ; i<length ; i++){
p2 = x;
p2 = p2 + i;
p2 = p1 + i;
}
for(int i=0; i<5; ++i){
cout << p2[i] << endl;
}
}
int main() {
int a[5]={1,2,3,4,5};
int b[5];
cpyia(a, b, 5);
}
An easier way to do it would be to put p2[i] = p1[i] in the loop, but I cant do that. Any help is appreciated.
The standard way of implementing your function is as follow:
for(int i = 0; i < length; ++i)
*new_array++ = *old_array++;
To be a bit more explicit, it's the same as:
void cpyia(int old_array[],int new_array[],int length){
int *p1 = old_array;
int *p2 = new_array;
for(int i=0 ; i<length ; i++){
*(p2+i) = *(p1+i);
// another way: *(p2++) = *(p1++);
}
}
In real code, you would use std::copy before even thinking about rewriting such a simple thing yourself.
Here is a complete example:
#include <iostream>
#include <algorithm>
void cpyia(int old_array[],int new_array[],int length){
std::copy(old_array, old_array + length, new_array);
}
int main() {
int a[5]={1,2,3,4,5};
int b[5];
cpyia(a, b, 5);
// test results:
for (int index = 0; index < 5; ++index)
{
std::cout << a[index] << " <-> " << b[index] << "\n";
}
}
However, your question says that you are "not allowed to use" something, which sounds a lot like a homework assignment. In that case, you could look at possible implementations of std::copy to get an idea of how to do it. Here is one way:
void cpyia(int old_array[],int new_array[],int length){
int* first = old_array;
int* last = old_array + length;
int* d_first = new_array;
while (first != last) {
*d_first++ = *first++;
}
}
#include<iostream>
using namespace std;
int main() {
const int size = 5;
int arr1[size] = { 4,21,43,9,77 };
int arr2[size];
int *ptr_a = arr1;
int *ptr_b = arr2;
for (int i = 0; i < size; i++)
{
*(ptr_b + i) = *(ptr_a + i);
cout << *(ptr_b + i) << " ";
}
}

What is going wrong with my multidimentional array function?

here I have a problem. I want to manipulate the following Matrix
Matrix A =
1---------2----------3
4---------5----------6
7---------8----------9
into
7---------8----------9
4---------5----------6
1---------2----------3
#include<iostream>
using namespace std;
void f(int ArrayName, int Size);
int main()
{
int x[3][3]={{1,2,3}, {4, 5,6},{7,8,9}};
f(x, 3);
system("pause");
}
void f(int ArrayName, int Size)
{
int holder;
for(int i=0; i<Size; i++){
for(int j=0; j<Size; j++)
{
holder=ArrayName[i][j];
ArrayName[i][j]=ArrayName[i+2][j+2];
ArrayName[i+2][j+2]=holder;
}
}
for(int k=0; k<Size; k++)
for(int l=0; l<Size; l++)
{
cout<<ArrayName[k][l];
if(l=3) cout<<"\n";
}
}
Errors:
E:\Semester 2\CPrograms\Array2.cpp In function `int main()':
10 E:\Semester 2\CPrograms\Array2.cpp invalid conversion from `int (*)[3][3]' to `int'
10 E:\Semester 2\CPrograms\Array2.cpp initializing argument 1 of `void f(int, int)'
E:\Semester 2\CPrograms\Array2.cpp In function `void f(int, int)':
22 E:\Semester 2\CPrograms\Array2.cpp invalid types `int[int]' for array subscript
(Last error repeated for five times)
All is done before us.:)
You can do quickly the assignment using standard algorithm std::reverse declared in header <algorithm>
For example
#include <iostream>
#include <algorithm>
#include <iterator>
int main()
{
int a[3][3] =
{
{ 1, 2, 3 },
{ 4, 5, 6 },
{ 7, 8, 9 }
};
for ( const auto &row : a )
{
for ( int x : row ) std::cout << x << ' ';
std::cout << std::endl;
}
std::cout << std::endl;
std::reverse( std::begin( a ), std::end( a ) );
for ( const auto &row : a )
{
for ( int x : row ) std::cout << x << ' ';
std::cout << std::endl;
}
return 0;
}
The program output is
1 2 3
4 5 6
7 8 9
7 8 9
4 5 6
1 2 3
As for your code then already this function declaration
void f(int ArrayName, int Size);
is wrong.
The first parameter should be either a reference to the array or a pointer to its first element.
For example
void f( int ( &ArrayName )[3][3] );
or
void f( int ( *ArrayName )[3], int Size );
If you want to write the function yourself then it can look the following way
#include <iostream>
void reverse( int ( *ArrayName )[3], size_t Size )
{
for ( size_t i = 0; i < Size / 2; i++ )
{
for ( size_t j = 0; j < 3; j++ )
{
int tmp = ArrayName[i][j];
ArrayName[i][j] = ArrayName[Size - i - 1][j];
ArrayName[Size - i - 1][j] = tmp;
}
}
}
int main()
{
int a[3][3] =
{
{ 1, 2, 3 },
{ 4, 5, 6 },
{ 7, 8, 9 }
};
for ( const auto &row : a )
{
for ( int x : row ) std::cout << x << ' ';
std::cout << std::endl;
}
std::cout << std::endl;
::reverse( a, 3 );
for ( const auto &row : a )
{
for ( int x : row ) std::cout << x << ' ';
std::cout << std::endl;
}
return 0;
}
The program output will be the same as above.
The prototype and declaration of your function should be :
void f(int ArrayName[][3], int Size)
Modify the code as:
#include<iostream>
using namespace std;
void f(int ArrayName[][3], int Size); //change here
int main()
{
int x[3][3]={{1,2,3}, {4, 5,6},{7,8,9}};
f(x, 3);
system("pause");
}
void f(int ArrayName[][3], int Size) //change here
{
int holder;
for(int i=0; i<Size/2; i++){ //change here
for(int j=0; j<Size; j++)
{
holder=ArrayName[i][j];
ArrayName[i][j]=ArrayName[size-i-1][size-i-1]; //change here
ArrayName[size-i-1][size-i-1]=holder; //change here
}
}
Try the solution in the code below. It uses the matrix as a vector, but has no dimension dependencies in the f() function prototype.
You have some errors in your code:
The array x[][] has to be passed by reference/pointer and not by value, then the function f() has to receive a pointer (or a reference) to int and not an int!
The for-loop scanning the matrix rows doesn't have to be for(int i=0; i<Size; i++), but for(int i=0; i<Size/2; i++). Using i<Size you swaps two times all the rows and then it seems nothing happens!
The following code:
ArrayName[i][j]=ArrayName[i+2][j+2];
ArrayName[i+2][j+2]=holder;
should be:
ArrayName[i][j]=ArrayName[Size-i-1][j];
ArrayName[Size-i-1][j]=holder;
The following code:
if(l=3) cout<<"\n";
l=3 assigns a value, don't do a comparison.
It should be:
if(l==Size-1) cout<<"\n";
#include<iostream>
using namespace std;
void f(int * ArrayName, int Size);
int main()
{
static const int Size=4;
int x[Size][Size];
//Loads the numbes into the matrix
for(int i=0;i<Size;i++)
for(int j=0;j<Size;j++)
x[i][j]=i*Size+j+1;
f(&x[0][0], Size);
}
void f(int *ArrayName, int Size)
{
int holder;
cout << "Input\n";
for(int k=0; k<Size; k++) {
for(int l=0; l<Size; l++)
cout<<ArrayName[k*Size+l]<<" ";
cout << "\n";
}
// Elaborate the matrix
// ----------------------------------------
for(int i=0; i<Size / 2; i++){
for(int j=0; j<Size; j++) {
holder=ArrayName[i*Size+j];
ArrayName[i*Size+j]=ArrayName[(Size-i-1)*Size+j];
ArrayName[(Size-i-1)*Size+j]=holder;
}
}
// ----------------------------------------
cout << "----------\n" << "Output\n";
for(int k=0; k<Size; k++) {
for(int l=0; l<Size; l++)
cout<<ArrayName[k*Size+l]<<" ";
cout << "\n";
}
}

How to pass and return 3d array to a function in c++?

I have created a 3d array into main function because one of its size came from used input. I am using C++
std::cin >> size;
typedef int T[8][3];
T* tables = new T[size];
It is basically tables[size][8][3]
Now I have to use this 3d table in different functions and have to store values into it. The best way to do it by make this table as a global variable. But I am not sure that I can do it after main function. The other option I have, that I have to pass this table as a parameter and have to return that at the end of the function.
I have tried both the approach but I am having error. Please help me about this issue. I don't know which approach to choose and how to do it.
Thank you in advance.
**Example:**This an example what I really want to do. Here I create a 3d array in main function and through another function I gave some input into that array and again print that in main function.
#include <iostream>
#include <conio.h>
using namespace std;
class M
{
public:
int i,j,k;
public:
int pass(int (*table)[8][3],int size);
}
int M:: pass(int (*table)[8][3],int s)
{
for (i=0;i<s;i++)
{
//int a = tables[i][2][1];
for(j=0;j<8;j++)
{
for(k=0;k<3;k++)
{
table[i][j][k]=i;
}
}
}
return (*table)[8][3]; // not sure about this
}
int main()
{
int size,i,j,k;
std::cin >> size;
typedef int T[8][3]; // T is your 2d array type
T* tables = new T[size];
cout << "test";
M mx;
mx.pass(tables,size); // not sure
for (i=0;i<size;i++)
{
for(j=0;j<8;j++)
{
for(k=0;k<3;k++)
{
cout<<tables[i][j][k];
cout<<" ";
}
cout<<endl;
}
cout<<endl;
cout<<"..........." << i <<endl;
}
getch();
}
I don't know if I completely understand your problem. But you can definitely store the pointer locally in your object and reference it elsewhere. Something like this:
class M
{
public:
M(int(*tbl)[8][3]) : table(tbl) { }
int(*table)[8][3];
int i, j, k;
public:
void pass(int size);
};
void M::pass(int s)
{
for (i = 0; i<s; i++)
{
for (j = 0; j<8; j++)
{
for (k = 0; k<3; k++)
{
table[i][j][k] = i;
}
}
}
}
int main()
{
int size, i, j, k;
std::cin >> size;
typedef int T[8][3]; // T is your 2d array type
T* tables = new T[size];
cout << "test";
M mx(tables);
mx.pass(size); // not sure
for (i = 0; i<size; i++)
{
for (j = 0; j<8; j++)
{
for (k = 0; k<3; k++)
{
cout << tables[i][j][k];
// or you can also:
// cout << mx.table[i][j][k];
cout << " ";
}
cout << endl;
}
cout << endl;
cout << "..........." << i << endl;
}
_getch();
}
Since you are creating a dynamic 3D array whose two dimensions are fixed, Use a std::array<std::array<int, 3>, 8> as your 2D array. Use a std::vector<__2D_ARRAY_TYPE> to create the 3D array.
#include <iostream>
#include <array>
#include <vector>
int main() {
std::array<std::array<int, 3>, 8> array_2d ;
std::vector<decltype(array_2d)> array_3d ;
int size = 4 ;
for(int i = 0; i < size; ++i)
{
for(int j = 0; j < 8; ++j)
for(int k = 0; k < 3; ++k)
array_2d[j][k] = j + k ;
array_3d.push_back(array_2d);
}
return 0;
}
Something like this you can use easily which does the job more easily without any manual memory management.
You can pass it to a function. The signature would be :
return_type function_name(const std::vector<std::array<std::array<int, 3>, 8>>& array_3d)
{ .... }
In
class M
{
public:
int i,j,k;
public:
int pass(int (*table)[8][3],int size);
}
you don't have to write public twice. You can simply put all of the public member data under the keyword public.
Also, you seem to be re-writing your function over again at the very end. Instead of
cout<<tables[i][j][k];
You can write
cout<<*tables

Copying, printing and inputing an array in C++

For a class assignment I am required to create 4 functions to test in a program. I must use the copyArray function, PrintArray function and InputArray function. The problem that I am having the most trouble with is the copyArray portion. I have completed most of the code on my own but I need to know if I am close to the solution or not close at all.The code, I'm sure, is a mess. If someone could help me get in the right direction of how to finish this I will be very grateful.
#include <iostream>
using std::cin;
using std::cout;
using std::endl;
using std::istream;
using std::ostream;
void inputArray(istream &, int[], int*);
void printArray(ostream &, const int[], int);
float a[4] = { 0, 1, 2, 3 };
float b[4];
void copyArray(const int orig[], int dup[], int);
void main()
{
int a[4] = { 7, 14, 9, 10 };
int b[4];
cout << "The input data : " << endl;
inputArray(cin, a, b);
cout << "The printArray data : " << endl;
printArray(cout, b, 4);
}
//----------------------------------------------------------------------------
void inputArray(istream & in, int t[], int howMany)
{
for (int i = 0; i < howMany; i++)
in >> t[i];
return;
}
//-----------------------------------------------------------------------------
void printArray(ostream & out, const int r[], int cnt)
{
for (int i = 0; i< cnt; i++)
out << r[i] << endl;
return;
}
void copyArray(const int orig [], int dup [], int);
for (int i = 0; i < 4; i++){
b[i] = a[i];
}
}
Of course it would be better to define function copyArray as a template function. For example
template <typename T, size_t N>
void copyArray( T ( &dst )[N], const T ( &src )[N] )
{
for ( size_t i = 0; i < N; i++ ) dst[i] = src[i];
}
As for your function declaration then its definition can look like
void copyArray( int dst[], const int src[], size_t n )
for ( size_t i = 0; i < n; i++ ) dst[i] = src[i];
}
In the function definition shown by you you have to remove the semicolon after the closing parenthesis and to use function parameters instead of the global variables.
Take into account that there are standard algorithms std::copy, std::copy_n, std::copy_if, std::copy_backward declared in header <algorithm> that can be used for coping arrays.