How to draw 2D Array? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

How to draw 2D Array?

I've tried this, but have error #include <stdio.h> #include <stdlib.h> void Draw_2D_Array(int raws, int cols, int *Arr) { int i,j; for (i = 0; i < raws; i++) { for (j = 0; j < cols; j++) { printf("%d ", Arr[i][j]); } printf("\n"); } return 0; } int main() { int a[3][3]={1,2,3,4,5,6,7,8,9}; Draw_2D_Array(3,3,a[3][3]); return 0; }

17th Oct 2018, 12:18 PM
Aisha Ali
Aisha Ali - avatar
1 Answer
+ 3
Try it like this; #include <stdio.h> void Draw_2D_Array(int rows, int cols, int Arr[rows][cols]) { int i,j; for (i = 0; i < rows; i++) { for (j = 0; j < cols; j++) { printf("%d ", Arr[i][j]); } printf("\n"); } return; // void function doesn't return // int, just use return. } int main() { // Initialize your array clearly int a[3][3]={{1,2,3},{4,5,6},{7,8,9}}; Draw_2D_Array(3,3,a); return 0; }
17th Oct 2018, 12:43 PM
Ipang