Home > Net >  How to Print an n-dimension Matrix with r spaces between its columns?
How to Print an n-dimension Matrix with r spaces between its columns?

Time:10-29

Print a matrix with n dimensions with a r distance between its columns. For n = 4 and r = 7 this is the output as shown below, where there are 7 spaces between every column. (Not allowed to use matrices or arrays)

enter image description here

The main problem here is that I'm trying to figure out how I can generalize the positions of 1s in terms of n, r and i. I get stuck because there seems to be no easy way to do this, or I'm just missing it.

Is this a good approach to these kinds of problems or is there a better approach?

#include <stdio.h>

int main() {

  int n; // Dimensions
  int r; // Distance between columns

  printf("Enter n: ");
  scanf("%i", &n); // Dimensions

  printf("Enter the distance between columns in matrix: ");
  scanf("%i", &r); // Distance


  int i, j, k;
  for (i = 0; i < n; i  ) { // Row
    for (j = 0; j < (r   1) * n; j  ) {
      if (j % r == 0) {
        if (j == r * (1   i) && j != 0) { // For 1s
          printf("1");
        } else {
          printf("0"); // For -0s
        }
      } else {
        printf("X");
      }
    }
    printf("\n");
  }
  return 0;
}

CodePudding user response:

The picture is not very good. I assume that the first column is left aligned and the distance includes the digit.

#include <stdio.h>

void printMatrix(int n, int d)
{
    for(int row = 0; row < n; row  )
    {
        for(int col = 0; col < n; col  )
            printf("%*d", !!col * d   1, row == col);
        printf("\n");
    }
}


int main(void) {

  int n; // Dimensions
  int r; // Distance between columns

  printf("Enter n: ");
  scanf("%i", &n); // Dimensions

  printf("Enter the distance between columns in matrix: ");
  scanf("%i", &r); // Distance
  printf("\n");

  printMatrix(n,r);
}

https://godbolt.org/z/KTcvTre1E

If you are not allowed to use those "fancy" formats:

#include <stdio.h>

void printMatrix(int n, int d)
{
    for(int row = 0; row < n; row  )
    {
        for(int col = 0; col < n; col  )
        {
            printf("%d", row == col);
            for(int space = 0; col != n && space < d; space  )
                printf(" ");
        }
        printf("\n");
    }
}

int main(void) {

  int n; // Dimensions
  int r; // Distance between columns

  printf("Enter n: ");
  scanf("%i", &n); // Dimensions

  printf("Enter the distance between columns in matrix: ");
  scanf("%i", &r); // Distance
  printf("\n");

  printMatrix(n,r);
}

https://godbolt.org/z/Y5K8h74e5

CodePudding user response:

Note that the printf format specifiers (like %i for decimal integer output) can take an optional field width parameter; thus, printf("%8i", x);, when x has the value of 1, will print 1 (seven preceding spaces, making a total field width of 8). Furthermore, if you specify a * as the field width, that width is specified as an extra argument (with the value r 1, in your case) to the printf call, immediately preceding the argument to be printed.

From cppreference:

  • (optional) integer value or * that specifies minimum field width. The result is padded with space characters (by default), if required, on the left when right-justified, or on the right if left-justified. In the case when * is used, the width is specified by an additional argument of type int, which appears before the argument to be converted and the argument supplying precision if one is supplied. If the value of the argument is negative, it results with the - flag specified and positive field width. (Note: This is the minimum width: The value is never truncated.)

So, for your problem, assuming r spaces before the first column as well as between columns, and assuming you want 1 for each leading diagonal element (when row = column) and 0 for all others, the following, much simplified code will do the trick:

#include <stdio.h>

int main(void)
{
    int n; // Dimensions
    int r; // Distance between columns
    printf("Enter n: ");
    scanf("%i", &n); // Dimensions
    printf("Enter the distance between columns in matrix: ");
    scanf("%i", &r); // Distance
    for (int i = 0; i < n; i  ) { // Row
        for (int j = 0; j < n; j  ) { // Column
            int x = (j == i) ? 1 : 0;
            printf("%*i", r   1, x); // Field width is #spaces PLUS 1 for the digit itself
        }
        printf("\n");
    }
    return 0;
}
  • Related