Answers for "passing 2d array to function"

C++
3

passing 2d array as parameter to function in c

#include <stdio.h>

//passing 2D array as a parameter to a function
void fun(int *arr, int m, int n)
{
	int i, j;
	for (i = 0; i < m; i++){
	for (j = 0; j < n; j++)
		printf("%d ", *((arr+i*n) + j));
		printf("n");
	}
}

int main(void)
{
	int arr[][4] = {{1, 2, 3,4}, {4, 5, 6,7}, {7, 8, 9,0}};
	int m = 3, n = 4; //m - no.of rows, n - no.of col

	fun(&arr, m, n);
	return 0;
}
Posted by: Guest on June-18-2020
0

passing 2d vector to function

void printFunc(vector < vector<int> > vec)
{
    for(int i=0; i<vec.size(); i++) 
		for(int j=0; j<vec[i].size(); j++) 
  			cout<<vec[i][j]<<" ";
        cout<<endl;
}

int main() 
{
    int rows = 2;
    int cols = 2;
    int val = 1;
	/*creates 2d vector “v[rows][cols]”
    and initializes all elements to “val = 1”*/
    vector< vector<int> > v(rows, vector<int> (cols, val));  
	printFunc(v);
}
Posted by: Guest on May-20-2021

Code answers related to "passing 2d array to function"

Browse Popular Code Answers by Language