I'm new to the C language and I'm working in this program as a way to study matrices. The program below is intended to read the rows and columns from the user (I will implement other tasks that are accessed by choosing the number). The matrix is given randomly according to the size given by the user. When 1 is given, the program should ask again about the rows and columns, but it is not working, it only changes the matrix, not its size. It is probably something related to pointers, but I couldn't figure it out. Below is a compilable code.
#include<stdio.h>
#include <time.h>
#include <stdlib.h>
#define max_row 20
#define max_col 20
typedef int Matrix[max_row][max_col];
void Read_Matrix(int *row, int *col)
{
while(*row > max_row || *row < 1 || *col > max_col || *col < 1)
{
printf("\n***** Matrix *****");
printf("\nEnter rows >> ");
scanf(" %d", row);
printf("\nEnter columns >> ");
scanf(" %d", col);
}
}
void Random_Matrix(Matrix M, int row, int col)
{
time_t seg = time(NULL);
srand(seg);
int i, j;
for(i = 0; i < row; i++)
{
for(j = 0; j < col; j++)
{
M[i][j] = rand()%10;
}
}
}
void Show_Matrix(Matrix M, int row, int col)
{
int i, j;
for (i = 0; i < row; i++)
{
printf("\n");
for (j = 0; j < col; j++)
{
printf(" %d ", M[i][j]);
}
}
printf("\n");
}
int main()
{
int choice, row, col;
Matrix M;
while(1)
{
/*
Tasks concerning the Matrix
*/
printf("\n\nEnter the task >> ");
scanf(" %d", &choice);
switch(choice)
{
case 1:
Read_Matrix(&row, &col);
Random_Matrix(M, row, col);
Show_Matrix(M, row, col);
break;
case 2:
printf("\nExit\n");
exit(0);
break;
}
}
}