【问题标题】:How can i find smallest number in main diagonal of multidimensional array?如何在多维数组的主对角线上找到最小的数字?
【发布时间】:2022-01-24 13:49:14
【问题描述】:

我需要编写一个程序来加载一个 3x3 维度的整数矩阵并找到第一个对角线上的最小元素。 我编写了这段代码,但它只给了我整个矩阵中最小的整数:

#include <stdio.h>

int main() {
    int mat[10][10];
    int i,j,smallest;
    printf(" ");
    for(i = 0; i < 10; i++)
        for (j = 0; j < 10; j++)
        scanf("%d", &mat[i][j]);
        smallest = mat[0][0];
    for ( i = 0; i < 10; i++){ 
        for(j = 0; j < 10;  j++){
            if(mat[i][j] < smallest)
            smallest = mat[i][j];
        }
    }
    printf("%d",smallest);
    return 0;
}

你能给我解决这个问题的方法吗?

【问题讨论】:

  • 是的,不要遍历整个矩阵,只遍历对角线。
  • 如果一个值在主对角线上是什么意思,你如何访问这样的值?
  • @dbush 我想说的是矩阵的第一个对角线。谷歌一下,看看我的意思。
  • @Azra 我知道这意味着什么。我在问你是否理解它的含义,以及你认为你会如何访问那个对角线上的值。
  • 我知道这意味着什么,但我不知道如何访问该对角线上的值。

标签: c multidimensional-array


【解决方案1】:

答案非常简单,你肯定在谷歌上也能找到。

但这里有一个提示可以找到此类问题的答案:

在纸上画出这个矩阵。 (如果不是 10x10,请尝试 2​​x2,然后是 4x4,然后是 5x5)。 然后只写下主对角线元素的索引。

例如在 4x4 矩阵的情况下,这些将是:

[0][0], [1][1], [2][2], [3][3]

你看到上面的模式了吗?

这种模式也会遵循 10x10。

现在,由于您需要在主对角线上找到最小的元素,因此您只需要在主对角线上的那些元素上循环。 (使用上面的模式为 10x10 设计循环,然后再试一次。)

【讨论】:

  • 谢谢,我明白了!!
【解决方案2】:

以下代码将对此有所帮助,

    #include <stdio.h>

int main() {
    int mat[3][3];
    int i,j,smallest;
    printf(" ");
    for(i = 0; i < 3; i++)
        for (j = 0; j <3; j++)
        scanf("%d", &mat[i][j]);
        smallest = mat[0][0];
    for ( i = 0; i < 3; i++){ 
        for(j = 0; j < 3;  j++){
            if(i==j)     //logic for main diagnol
                if(smallest>mat[i][j])
                    smallest = mat[i][j];
        }
    }
    printf("%d",smallest);
    return 0;
}

【讨论】:

  • 非常感谢 Girija 解决了我的问题
  • 第二个循环根本不需要。
  • 我不知道哪个循环
  • 计算最小值时,内部循环是多余的。就做for (i = 0; i &lt; 3; i++) if (smallest &gt; mat[i][i]) smallest = mat[i][i];
  • 好的,我知道了,谢谢
【解决方案3】:
  1. 使用函数
  2. 为索引使用正确的类型 (size_t)
  3. 在测试代码时,请尽量避免用户输入。它会在调试过程中为您节省大量时间。
int findSmallestDiagonal(size_t size, int (*array)[size])
{
    int result = array[0][0];
    for(size_t index = 1; index < size; index++)
        if(array[index][index] < result) result = array[index][index];
    return result;
}

void fillOrPrint(size_t size, int (*array)[size], int print, int max)
{
    for(size_t r = 0; r < size; r++)
    {
        for(size_t c = 0; c < size; c++)
        {
            if(print) printf("[%6d] ", array[r][c]);
            else array[r][c] = rand() % max;
        }
        if(print) printf("\n");
    }

}

#define SIZE 16

void main()
{
    int array[SIZE][SIZE];
    int smallest;

    srand(time(NULL));

    fillOrPrint(SIZE, array, 0, 1000); //fill array
    fillOrPrint(SIZE, array, 1, 0);    //print array

    smallest = findSmallestDiagonal(SIZE, array);

    printf("The smalles diagonal is: %d\n", smallest);
}

https://godbolt.org/z/75fzj8Tsq

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-05-25
    • 2020-07-24
    • 2014-03-21
    • 1970-01-01
    相关资源
    最近更新 更多