-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram_112.c
More file actions
35 lines (26 loc) · 739 Bytes
/
Copy pathProgram_112.c
File metadata and controls
35 lines (26 loc) · 739 Bytes
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
// Program 112 :Write a program to create a dynamic 2D array using malloc().
#include <stdio.h>
#include <stdlib.h>
int main() {
int r, c;
int **arr;
printf("Enter rows and columns: ");
scanf("%d %d", &r, &c);
arr = (int **)malloc(r * sizeof(int *));
for(int i = 0; i < r; i++)
arr[i] = (int *)malloc(c * sizeof(int));
printf("Enter elements:\n");
for(int i = 0; i < r; i++)
for(int j = 0; j < c; j++)
scanf("%d", &arr[i][j]);
printf("Matrix:\n");
for(int i = 0; i < r; i++) {
for(int j = 0; j < c; j++)
printf("%d ", arr[i][j]);
printf("\n");
}
for(int i = 0; i < r; i++)
free(arr[i]);
free(arr);
return 0;
}