-2

I'm trying to work on a program where the user enters a matrix of size nxn 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 nxn 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!

John
  • 9
  • 2
  • 4
    Please read up about `for` loops - covered in Chapter one of any C programming book – Ed Heal Jul 22 '17 at 09:55
  • I figured I would have to use a for loop but how will I scan in multiple numbers in one line without knowing how many numbers will be entered in advance? – John Jul 22 '17 at 10:00
  • Repeated calls to `scanf` - i.e. `scanf(" %d", ....)` – Ed Heal Jul 22 '17 at 10:01
  • Perhaps read the manual page for `scanf` as well – Ed Heal Jul 22 '17 at 10:02
  • I understand how a for loop may be used if the user enters one number at a time, pressing enter after each time, but I'm not sure how to use it if the user is entering multiple numbers at a time separated by a space. – John Jul 22 '17 at 10:07
  • 1
    The user does not need to press return for `scanf` to work – Ed Heal Jul 22 '17 at 10:08

2 Answers2

2

So that would be something like:

for (int i=0; i<n; i++) {
    printf ("Enter data for row %d:\n",i+1);
    for (int j=0; j<n; j++)
        scanf("%d ",&matrix[i][j]);
}
Paul Ogilvie
  • 25,048
  • 4
  • 23
  • 41
0

Firstly, if you must use stdin, then set up a file, call it inputfile.txt and evoke the program via

./myprogram < inputfile.txt

otherwise entering a whole matrix becomes too unwieldy.

Make the first line n, i.e. n rows. Then loop over n, and read the lines, like this

int i;
int n;
char buff[1024];
scanf("%d", &n);

for(i=0;i<n;i++)
{
   fgets(buff, 1024, stdin);
   /* now process here */
}

Process the lines via strtok() and strtol() (assuming integer inputs).

Now how do you store the matrix? The easiest way is to forget all about 2D arrays and simply flatten it. So assuming square matrices, you allocate it like this

int *mtx = malloc(n * n * sizeof(int));

Then you access it like this (i and j are the index variables)

mtx[i*n+j] = x;
Malcolm McLean
  • 6,258
  • 1
  • 17
  • 18