I'm trying to work on a program where the user enters a matrix of size n
xn
where n is 10 or less, and the program has to rotate it by 90, 180, 270 degrees etc. The user will first enter how big the matrix will be ("enter n: ") and then will proceed to fill the matrix by entering multiple numbers at a time separated by spaces. So the program will say "enter row 0 of matrix: " and the user will type "1 2 3", "enter row 1 of matrix: " etc.
My questions is, how do I write a program where I scan integers into a n
xn
multidimensional array when I wouldn't know how big n
is going to be until the user enters it. I know scanf("%d %d %d", matrix[0][0], matrix [0][1], matrix[0][2]);
can be used for the first row if I know in advance that it's going to be a 3 by 3 matrix or scanf("%d %d %d %d", matrix[0][0], matrix [0][1], matrix[0][2], matrix[0][3]);
for a 4 by 4 matrix, but in this case I wouldn't know n until after the code is written and the user enters it. All I can think of right now is:
printf("Enter n: ");
scanf("%d", &n);
if (n == 3){
printf("Enter row 0 of matrix: );
scanf("%d %d %d", matrix[0][0], matrix [0][1], matrix[0][2]);
printf("Enter row 1 of matrix: );
scanf("%d %d %d", matrix[1][0], matrix [1][1], matrix[1][2]);
printf("Enter row 2 of matrix: );
scanf("%d %d %d", matrix[2][0], matrix [2][1], matrix[2][2]);
// code to rotate 3x3 matrix
}
else if (n == 4){
printf("Enter row 0 of matrix: );
scanf("%d %d %d", matrix[0][0], matrix [0][1], matrix[0][2], matrix[0][3]);
printf("Enter row 1 of matrix: );
scanf("%d %d %d", matrix[1][0], matrix [1][1], matrix[1][2], matrix[1][3]);
printf("Enter row 2 of matrix: );
scanf("%d %d %d", matrix[2][0], matrix [2][1], matrix[2][2], matrix[2][3]);
printf("Enter row 3 of matrix: );
scanf("%d %d %d", matrix[3][0], matrix [3][1], matrix[3][2], matrix[3][3]);
// code to rotate 4x4 matrix
}
else if (n == 5) {
// and so on...
}
However, I know this will take way too long. Is there anyone that can help? Thanks!