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
inttype as opposed to an int array type