【问题标题】:Check if an array is mirrored in C检查数组是否在 C 中镜像
【发布时间】:2021-02-03 01:57:31
【问题描述】:

我正在尝试检查用户输入的数组是否被镜像

用户输入:

//for the rows and column
3 3
//elements inside array
1 0 1
2 0 3
4 0 4

输出应该告诉数组的第一个索引和最后一个索引是否相同,但我唯一做的就是镜像它并打印镜像,即使它不一样

int main()
{                       
    int arr[10][10];   
    int col,row;   
 
    scanf("%d %d", &col, &row);    
 
    for (int i = 0; i < col; i++) {
        for (int j = 0; j < row; j++) {
            scanf("%d", &arr[i][j]);
        }
    }       
 
    for (int i = 0; i < col; i++) {
        for(int j = row - 1; j >= 0; j--) {
            printf("Mirror"); 
        }
    }       
} 

数组和元素的大小取决于用户,谢谢你

【问题讨论】:

  • 嗨 Qwet,您已经向我们展示了一些代码,但您有问题吗?
  • 我忘了说抱歉,我想知道如何比较二维数组的最后一个索引和第一个索引,因为如果这些索引相同,我想放置镜像,如果索引不一样
  • 输入 5 5 5 4 5 2 1 2 3 3 3 1 1 1 输出:镜像输入 5 5 5 4 5 2 1 2 3 3 3 1 1 9 输出:不
  • 数组的第一个索引和最后一个索引相同。这真的是您对镜像矩阵的定义吗?比较是:arr[0][0] == arr[col-1][row-1]。但我觉得这个定义很奇怪。

标签: arrays c


【解决方案1】:

这是我对您的问题的解决方案,此代码可能有一些问题,但通常按照您的要求进行。适应您的特定需求。

输出:

This program will check if an array is mirrored. Press enter to proceed!
 
Insert row*col -> 3*3
Insert row 1 elements, one by one!
Element 1 -> 1
Element 2 -> 0
Element 3 -> 1
Insert row 2 elements, one by one!
Element 1 -> 2
Element 2 -> 0
Element 3 -> 3
Insert row 3 elements, one by one!
Element 1 -> 4
Element 2 -> 0 
Element 3 -> 4
Mirror found at row 1!
1 0 1 
Mirror found at row 3!
4 0 4 

代码:

#include <stdio.h>

#define MAXCOLUMN 10
#define MAXROW    10

void clean(){
  int temp;
  while ((temp = getchar()) != '\n' && temp != EOF);
}

void print(int dim,int r, int row[][MAXCOLUMN]){
  for(int index = 0; index < dim; index++)
    printf("%d ",row[r][index]);
  putchar('\n');
}

int main(void){
  int arr[MAXROW][MAXCOLUMN];
  int col,row,mirror;

  puts("This program will check if an array is mirrored. Press enter to proceed!");

  do{
    clean();
    printf("Insert row*col -> ");
  } 
  while(scanf("%d*%d", &row, &col)!=2);

  for(int r = 0; r < row; r++){
    printf("Insert row %d elements, one by one!\n",r+1);
    for(int c = 0; c < col; c++){
      do{
        clean();
        printf("Element %d -> ", c+1);
      }while(!scanf("%d",&arr[r][c]));
    }
  }

  for(int r = 0; r < row; r++){
    mirror = 1;
    for(int cFH = 0, cSH = col-1; cFH < col/2; cFH++, cSH--)//cFH -> column index first half | cSH -> column index second half
      if(arr[r][cFH] != arr[r][cSH]){
        mirror = 0;
        break;
      }
    if(mirror){
      printf("Mirror found at row %d!\n",r+1);
      print(col,r,arr);
    }
  }

  return 0;
}

【讨论】:

  • 为什么他们必须调整这个代码转储以满足他们的需要?你说它可能有问题——什么问题?为什么不能通过提供一个简单、清晰的答案来解决问题,解释如何解决他们需要的特定部分,并避免使用无意义的输出或引入混乱的代码示例来调整你回答问题的方式一堆OP可能不熟悉的语法。
  • 是的,我是新手,这是我找到帮助的方式,如果它的混淆多于帮助,我会删除我的答案
  • 这样说:假设您已经编写了一些代码,并且需要一些帮助来找出其中的一小部分。你问一个关于那一点的问题,然后有人为你编写了一个全新的程序。它有不同的变量名、不同的输入语法、不同的输出,它使用函数、宏、逗号运算符、do-while 循环和其他几个原始代码中没有的标准库调用......现在,我从来没有遇到过擅长阅读别人的代码并理解它的初学者,更不用说自己的了。不要删除你的答案,但我希望你明白我的意思。
  • 是的,我明白你的意思并同意这一点,看起来更好,我走得太远了,我会在我的下一个答案中考虑这一点,因为我说我也是初学者,不是完全但仍然......所以我希望我的代码没有大问题,我尽力做到最好
【解决方案2】:

通常您遍历每一行的列,这意味着通常i(外循环)代表行,j 代表列。

这段代码展示了一种检查二维数组是否水平镜像的方法:

int mirror = 1;

for(int i = 0; i < row; i++) {
    
    for(int j = 0; j < col/2; j++) {
        
        // comparing first element with last element of row `i`
        // than comparing the second element with the second last
        // and so on, till you reach the center of the row, `j==col/2`
        // if the comparison is true just one time, the array is not mirrored
        if( array[i][j] != array[i][col-j-1] ) { 
            
            mirror = 0;
            i = row; // to break the outer loop
            break;
        }                  
    }                                    
}                                                        

mirror ? puts("Mirror!") : puts("No mirror.");

在程序结束时,如果变量mirror 的值仍然是1,则得到一个镜像数组。

【讨论】:

  • 您遇到了一个错误,它将溢出您的数组读取。在编写帮助初学者的答案时,检查自己的代码很重要。否则你只会让他们更加困惑。
  • 你说得对,他本可以写另一个问题来问为什么这段代码不起作用。
猜你喜欢
  • 2023-03-11
  • 1970-01-01
  • 2012-10-26
  • 2016-05-28
  • 2021-03-15
  • 1970-01-01
  • 2012-02-23
  • 1970-01-01
  • 2022-01-17
相关资源
最近更新 更多