【问题标题】:Why a wrong number of 1 is printed?为什么打印错误的数字 1?
【发布时间】:2021-04-03 07:17:04
【问题描述】:

我的函数应该随机插入一个用户选择的数字 1 到我的矩阵中。困难在于如果一个单元格包含 1,则它周围的单元格必须设置为 0。为什么我的代码打印错误的数字 1?在下面的代码中,我曾想过首先将整个矩阵设置为 0,然后随机生成一个设置为 1 的单元格,在检查它包含 0 并且该单元格与其他包含 1 的单元格之间的距离 >= 1。所有这样做直到用户输入的数字 m 变为 0。

#include <stdio.h>
#include <stdlib.h>
#include <math.h>

void initialize(int n, int a[n][n]);
void createMap(int n, int a[n][n], int m);
int check (int i, int j, int v, int w);
void print(int n, int a[n][n]);

int main(){
    
    int n;
    printf("Insert square matrix size: ");
    scanf("%d", &n);
    
    int m;
    printf("Insert 1s number: ");
    scanf("%d", &m);
    
    int a[n][n];
    
    initialize(n,a);
    createMap(n,a,m);
    
}

//Filling the matrix with 0
void initialize(int n, int a[n][n]){
    for (int i = 0; i < n; i++){
        for (int j = 0; j < n; j++){
            a[i][j] = 0;
        }
    }
}

//Setting in random position 1 value
void createMap(int n, int a[n][n], int m){
    int x1; int x2;
    int b[0][0];
    while (m > 0){
        int i = rand() % n;
        int j = rand() % n;
        if (a[i][j] == 0 && (check(i,j,x1,x2) == 1)){
            a[i][j] = 1;
            m--;
            //I have to fill b array with coordinates and then to pass
            //b array to check function to do the check in the whole b array
        }
    }
    print(n,a);
}

//checking if I can set the value to 1
int check (int x1, int y1, int x2, int y2){
    if (sqrt(pow((x1-x2),2) + pow((y1-y2),2)) >= 1){
        return 1;
    } else {
        return 0;
    }
}

//Printing the matrix
void print(int n, int a[n][n]){
    for (int i = 0; i < n; i++){
        for (int j = 0; j < n; j++){
            printf("\t%d",a[i][j]);
        }
        puts("");
    }
}

【问题讨论】:

    标签: arrays c function matrix random


    【解决方案1】:

    生成坐标 i 和 j 后,您应该计算该单元格与设置为 1 的其他单元格之间的距离。您可以使用 Manhattan formula 来执行此操作。 如果新生成的单元格与其他设置为 1 的单元格的距离大于等于 1,则可以继续生成其他单元格,否则应将其设置回零并生成新坐标。

    【讨论】:

    • 你能给我看几行代码吗?我怎么知道所有其他设置为 1 的单元格在哪里?
    • @ChiaraTumminelli 您应该将其他单元格的坐标保存在数据结构(数组或链表)中。 distance = sqrt(pow((x1 - x2), 2) + pow((y1 - y2), 2)); x1,y1 是第一个单元格的坐标,x2,y2 是另一个单元格的坐标。如果距离 >= 1,则两个单元格之间的距离足够远
    • 我尝试编写函数,我需要弄清楚如何将坐标插入新数组并检查整个数组。你能帮助我吗?我用修改后的代码编辑了帖子。
    【解决方案2】:

    这是作业吧?

    几个问题:

    1. 您有一个未使用的变量 matrix
    2. 您在知道旧值是什么之前设置了a[k][l] = 1;,因此无法判断是否应该增加m
    3. 您的边界检查很混乱,很难看出它是否正确,如果您正确更新ma[k][l] 周围的所有数组元素。除非他们得到报酬,否则没有人愿意查看凌乱的代码。他们只会认为这是错误的。所以这部分代码根据定义是错误的,没有人能告诉你原因。
    4. 您的边界检查假定您有一个 4x4 数组。

    ...和其他一些人。

    第 1 点很容易解决,并不重要。它只会增加代码的丑陋性。

    如果 a[k][l] 为零,则仅通过更新 a[k][l]m 即可解决第 2 点:

    if (a[k][l] == 0)
      {
        a[k][l] = 1;
        m--;
      }
    

    可以通过创建一个单独的函数来最容易地修复第 3 点,该函数通过边界检查来重置单个数组元素。因为我认为这是家庭作业,所以我不会为你写这篇文章,除非你自己先诚实地尝试。函数签名可能如下所示:

    /*
       Set value of `a[line][column]` to zero and increment `*m` if value
       was changed.
       Nothing is done if `line` or `column` are out of bounds.
    */
     void matrix_element_reset(int n, a[n][n], int line, int column, int *m);
    

    然后您可以像这样轻松地重置 a[k][l] 周围的数组元素:

    /* This can be simplified with loops */
    matrix_element_reset(n, a, k-1, l, &m);
    matrix_element_reset(n, a, k, l, &m);
    matrix_element_reset(n, a, k+1, l, &m);
    .....
    

    第 4 点:您的变量 n 存储矩阵的维度。使用它。

    【讨论】:

    • 我的目的不是“让你做我的功课”,而是要明白我做错了什么。这将是项目的一小部分(我当然不必用 0 和 1 填充矩阵)。这是我为自己做的一个练习,以便能够理解这件事是如何工作的,这样我就可以更有意识地完成我的项目。无论如何,我发现您的建议非常宝贵。
    • 我的代码仍然存在一些问题,但它现在可读性更高,我相信很快我就能找到解决方案。如果您想看一下,代码在编辑后的帖子中。无论如何,非常感谢您的宝贵帮助。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-04-08
    • 2013-07-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多