简体   繁体   English

如何通过用户给出的输入将二维矩阵的元素打印为矩形

[英]How to print the elements of 2D matrix as rectangular shape by the input given by user

I've come across this question in one of the assessments in our college.我在我们大学的一次评估中遇到了这个问题。 None of my friends were able to help me out.我的朋友都无法帮助我。 I even checked all leading online platforms for my desired output criteria.我什至检查了所有领先的在线平台,了解我想要的 output 标准。 But no use, so I wanted to post my question in Stack Overflow.但没有用,所以我想在 Stack Overflow 中发布我的问题。

I request people who are watching this tag to help me out.我请求正在观看此标签的人帮助我。 Even though my assessment has been completed, I'm eagerly trying to solve this problem but I couldn't.即使我的评估已经完成,我也急切地试图解决这个问题,但我做不到。

#include <stdio.h>

int main()
{

     int a[4][4] = {{1, 2, 3, 4},
                    {5, 6, 7, 8},
                    {9, 10, 11, 12},
                    {13, 14, 15, 16}};

     int k, l, m, n, o, r;

     int temp[4][4] = {0};
     printf("Enter index of row & column: ");

     int p, q;
     scanf("%d %d", &p, &q);

     for (int i = p; i < 4; i++)
     {
          for (int j = q; j < 4; j++)
          {
               temp[i][j] = a[i][j];
               // initial given index number to left_most number
          }
          k = i + 1, l = 4 - q;

          for (k; k <= l; k++)
          {
               temp[k][l] = a[k][l];
               //rightmost element to bottom
          }
          m = k - 1, n = l - 1;

          for (n; n >= q; n--)
          {
               temp[m][n] = a[m][n];
               //bottomright to left elements upto the column index provided by user
          }
          o = m, r = q;

          for (o; o >= i + 1; o--)
          {
               temp[o][r] = a[o][r];
               //bottomleft to initial index element
          }
          break;
     }

     for (int i = 0; i < 4; i++)
     {
          for (int j = 0; j < 4; j++)
          {

               //if the element is zero then priting a space else printing the number
               if (temp[i][j] == 0)
               {
                    printf(" ");
               }

               else
               {
                    printf("%d ", temp[i][j]);
               }
          }
          printf("\n");
     }
     return 0;
}

Sample Input: Enter index of row & column: 0 1示例输入:输入行和列的索引:0 1

Sample Output:样品 Output:

2  3  4
6     8
10    12
14 15 16

My code only works for sample input and the following column.我的代码仅适用于示例输入和以下列。

So I need help on how to make my code work for any input given by user, it has to print the zero(0) shape out of it.因此,我需要有关如何使我的代码适用于用户提供的任何输入的帮助,它必须从中打印零(0)形状。

There is a pretty neat solution to this problem这个问题有一个非常巧妙的解决方案

  for (int r = 0; r < numrows; r++) {
      for (int c = 0; c < numcols; c++) {
          if (r >= p && c >= q && 
             (c == q || c == numcols - 1 || r == p || r == numrows - 1)){
             printf("%2d ",a[r][c]);
          }
          else {
             printf("  ");
          }
       }
       printf("\n");
   }

where numrows = 4 and numcols = 4其中numrows = 4numcols = 4

For starters there is no need to declare an auxiliary array if the task is just to output a sub-array of the original array.对于初学者,如果任务只是对 output 原始数组的子数组,则无需声明辅助数组。

A straightforward approach in C can look the following way. C 中的一种直接方法可以如下所示。

#include <stdio.h>

void rectangle_pattern( size_t n, int a[][n], size_t row, size_t col )
{
    for ( size_t i = row; i < n; ++i )
    {
        if ( i == row || i == n - 1 )
        {
            for ( size_t j = col; j < n; j++ )
            {
                printf( "%2d ", a[i][j] );
            }
        }
        else
        {
            printf( "%2d", a[i][col] );
            if ( col != n - 1 ) printf( "%*d", ( int )( 3 * ( n - col - 1 ) ), a[i][n-1] );
        }
        putchar( '\n' );
    }
}

int main(void) 
{
    enum { N = 4 };
    int a[N][N]= 
    { 
        {  1,  2,  3,  4 },
        {  5,  6,  7,  8 }, 
        {  9, 10, 11, 12 }, 
        { 13, 14, 15, 16 } 
    };

    size_t row = 0, col = 0;
    
    rectangle_pattern( N, a, row, col );
    
    putchar( '\n' );
    
    row = 0, col = 1;
    
    rectangle_pattern( N, a, row, col );
    
    putchar( '\n' );

    row = 0, col = 2;
    
    rectangle_pattern( N, a, row, col );
    
    putchar( '\n' );

    row = 0, col = 3;
    
    rectangle_pattern( N, a, row, col );
    
    putchar( '\n' );

    return 0;
}

The program output is程序 output 是

 1  2  3  4 
 5        8
 9       12
13 14 15 16 

 2  3  4 
 6     8
10    12
14 15 16 

 3  4 
 7  8
11 12
15 16 

 4 
 8
12
16 

The function declaration uses a variable length array as a parameter. function 声明使用可变长度数组作为参数。 You can change the function declaration declaring the parameter as having an array with a fixed size.您可以更改 function 声明,将参数声明为具有固定大小的数组。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM