Although I am sharing the code, please try and type the code yourself. I am giving some steps with this answer, which should help you think through the code if you want to implement it all by yourself.
Here are some steps.
1) Input m
and n
int m, n;
scanf("%d%d", &m, &n);
2) Allocate memory to the array
char *arr[m];
for (i = 0; i < n; ++i) {
arr[i] = malloc(n * sizeof(char));
}
3) Now, since you mentioned in one of your comments, the program should generate random alphabets and not numbers, let me tell you, C allows casting of datatype from int
to char
. Here is how you will generate random characters.
for (i = 0; i < m; ++i) {
for (j = 0; j < n; ++j) {
c = (char) (rand() % 26) + 'a'; // Generate a random character between 'a' and 'z'
arr[i][j] = c;
}
}
Here is the complete code:
#include <stdio.h>
#include <stdlib.h>
int main() {
int m, n, i, j;
char c;
scanf("%d%d", &m, &n);
char *arr[m];
for (i = 0; i < n; ++i) {
arr[i] = malloc(n * sizeof(char));
}
for (i = 0; i < m; ++i) {
for (j = 0; j < n; ++j) {
c = (char) (rand() % 26) + 'a'; // Generate a random character between 'a' and 'z'
arr[i][j] = c;
printf("%c ", c);
}
printf("\n");
}
}
Input:
3
4
Output:
n w l r
b b m q
b h c d
I think this is the expected output!
Thanks.