-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathdynamic2d.c
49 lines (48 loc) · 1018 Bytes
/
dynamic2d.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
/*
* DYNAMIC 2D ARRAY
*/
#include <stdio.h>
#include <stdlib.h>
int main()
{
int rows;
printf("Enter the rows of the array\n");
scanf("%d",&rows);
int cols;
printf("Enter the cols of the array\n");
scanf("%d",&cols);
int **arr = (int**)malloc(sizeof(int *)*rows);
for(register int i=0;i<rows;i++)
{
arr[i] = (int*)malloc(sizeof(int)*cols);
}
printf("Enter the elements in the array\n");
//entered array
for(register int i=0;i<rows;i++)
{
for(register int j=0;j<cols;j++)
{
scanf("%d",&arr[i][j]);
}
}
printf("Entered array\n");
for(register int i=0;i<rows;i++)
{
for(register int j=0;j<cols;j++)
{
if(j==cols-1)
{
printf("%d\n",arr[i][j]);
}
else
{
printf("%d ",arr[i][j]);
}
}
}
for(register int i=0;i<rows;i++)
{
free(arr[i]);
}
free(arr);
}