-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path2darray.c
More file actions
41 lines (35 loc) · 1.06 KB
/
2darray.c
File metadata and controls
41 lines (35 loc) · 1.06 KB
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
#include <stdio.h>
#include <stdlib.h>
int main() {
int rows, columns;
printf("Enter the number of rows: ");
scanf("%d", &rows);
printf("Enter the number of columns: ");
scanf("%d", &columns);
// Allocate memory for an array of pointers to rows
int** array = (int**)malloc(rows * sizeof(int*));
for (int i = 0; i < rows; i++) {
// Allocate memory for each row
array[i] = (int*)malloc(columns * sizeof(int));
}
// Fill the array with values
for (int i = 0; i < rows; i++) {
for (int j = 0; j < columns; j++) {
array[i][j] = i * columns + j;
}
}
// Print the array
printf("2D array:\n");
for (int i = 0; i < rows; i++) {
for (int j = 0; j < columns; j++) {
printf("%d ", array[i][j]);
}
printf("\n"); // Move to the next line after printing each row
}
// Free the memory
for (int i = 0; i < rows; i++) {
free(array[i]); // Free each row's memory
}
free(array); // Free the array of pointers
return 0;
}