MisterIvan
MisterIvan

Reputation: 49

making a function that takes an array input ,prints elements of 2d c++

Scope: Make a function that takes a 2d array as input and prints each element individually.

I start with just having the array within the function and this works perfectly

#include<iostream>
void printArray();
int main()
{
  printArray();
  return 0; 
}

void printArray()
{
  int array[4][2] = {{1,2},{3,4},{5,7},{8,9}};
  int rows = sizeof(array)/sizeof(array[0]);
 int cols = sizeof array[0] / sizeof array[0][0];
 for (int i = 0 ; i < rows ; i++)
   {
     for ( int j = 0; j < cols; j++ )
       {
     std::cout<<array[i][j]<<' ';
       }
     std::cout<<'\n';
   }

 std::cout<<rows<<"    "<<cols;

}

So I move on and tried to make printArray() take an input

#include<iostream>
void printArray(int array);
int main()
{
  int arr[4][2] = {{1,2},{3,4},{5,7},{8,9}};
  printArray(arr);
  return 0; 
}

void printArray(int array)
{

 int rows = sizeof(array)/sizeof(array[0]);
 int cols = sizeof array[0] / sizeof array[0][0];
 for (int i = 0 ; i < rows ; i++)
   {
     for ( int j = 0; j < cols; j++ )
       {
     std::cout<<array[i][j]<<' ';
       }
     std::cout<<'\n';
   }

 std::cout<<rows<<"    "<<cols;

}

now I am getting an error call: error: invalid types ‘int[int]’ for array subscript

what is going wrong ?

the output I want is

1 2 
3 4 
5 7 
8 9 
4    2

Upvotes: 1

Views: 339

Answers (2)

Pratik Sampat
Pratik Sampat

Reputation: 330

You're passing an integer and not an array.

There are 3 ways through which you can pass a 2D array as parameter

Method1: Pass it as a pointer which holds ints

int *arr[4];
for (i = 0; i < 2; i++)
    arr[i] = new int[3]
void printArray(int *arr[4]) { ... }

Method2: Pointer of pointers

int **arr;
arr = new int *[4];
for (i = 0; i < 2; i++)
    arr[i] = new int[3];
void printArray(int **arr) { ... }

Method3:

Here your parameter itself is a 2D array

int arr[4][2] = {{1,2},{3,4},{5,7},{8,9}};
void printArray(int arr[][2]) { ... }

Upvotes: 1

Mihir Luthra
Mihir Luthra

Reputation: 6769

Just modified you code and commented where changes were required:

#include<iostream>

// as you are passing an array to your function,
// this is one valid syntax to do so:
void printArray(int array[4][2], int rows, int cols);

int main()
{
  int arr[4][2] = {{1,2},{3,4},{5,7},{8,9}};

  // You can't get sizeof arrays that have been passed to
  // a function. Functions accept them as pointers
  // and sizeof(arr) inside the function will actually
  // return the sizeof pointer.
  int rows = sizeof(arr)/sizeof(arr[0]);
  int cols = sizeof(arr[0])/ sizeof(arr[0][0]);

  printArray(arr, rows, cols);
  return 0;
}

void printArray(int array[4][2], int rows, int cols)
{

  for (int i = 0 ; i < rows ; i++)
   {
     for ( int j = 0; j < cols; j++ )
       {
     std::cout<<array[i][j]<<' ';
       }
     std::cout<<'\n';
   }

 std::cout<<rows<<"    "<<cols;

}



Some reading material:

How are arrays passed to functions.

sizeof operator on an array passed to a function

Upvotes: 1

Related Questions