c - 如何在C中的二维数组中找到最高和最低的数字及其位置

标签 c arrays multidimensional-array

我是新来的。 我开始学习编程和 C++。 我必须做一个程序,向用户请求数字来填充一个数组,然后是一个函数来查找最高值和最低值及其在数组中的位置。 这就是我现在所拥有的,它可以找到最高的数字及其位置,但它无法找到最低的,找到的最低值不正确,或者它的位置:

int main() {
    int array[SIZEF][SIZEC];
    int zero = 0;
    int highest[0][0];  //to find the highest, array from 0 value.
    int lowest[0][0];   //to find the lowest, takes the highest value and then compare, if its lower than the current value of the array, it takes its value 

    highest[0][0] = zero;

    fill_array(array, SIZEF, SIZEC);

    highlow(array, SIZEF, SIZEC, highest, lowest);

    getchar();

    return 0;
}

void fill_array(int array[][SIZEC], int sizef, int sizec) {
    //code to fill the array, no problem here.    
}

void highlow(int array[][SIZEC], int sizef, int sizec, int highest[][0], int lowest[][0]) {
    int positionX = 0;
    int positionY = 0;

    for (int i = 0; i < sizef; i++) {
        for (int j = 0; j < sizec; j++) {
            if (array[i][j] > highest[0][0]) {
                //if the current value of the array is higher than highest value, highest value takes its value.
                highest[0][0] = array[i][j];
                positionX = i;
                positionY = j;
                lowest[0][0] == highest[0][0]; //now lowest value its the highest value

                if (array[i][j] < lowest[i][j]) { //so, if current array value its lower than lowest value
                                                  //lowest value takes its value.                
                    lowest[0][0] = array[i][j];
                }
            }
        }
    }
}

非常感谢。 (请原谅我的英语,我也在学习)。

最佳答案

int highest[0][0];  //to find the highest, array from 0 value.

int lowest[0][0];   //to find the lowest, takes the highest value and then compare, if its lower than the current value of the array, it takes its value 

这两个数组允许包含 0 个元素,维度为 0(注意 ISO C 禁止零大小数组)

所以在

highest[0][0]=zero;

你写出数组,并且在你可以访问这两个数组之后的每个地方都喜欢

你为什么不将它们设置为数组?我这么说是因为 lowest[i][j] 在你程序的其他地方,即使这看起来很奇怪


如果我忘了这两个 vector 的维数问题,在

lowest[0][0]==highest[0][0];

这句话什么都不做,可能是你想要的

lowest[0][0]=highest[0][0];

?即使这看起来很奇怪


如果我忘了这两个 vector 的维数问题,在

if(array[i][j] < lowest[i][j])

除了 [0][0] 之外,你永远不会写最低的,所以 lowest[i][j] 是未定义的,除非 i 和 j 为 0


您在 main 中调用了 fill_arrayhighlow 而没有声明/定义,因为它们是在 main,编译器将使用您的调用中的默认声明,这很危险,将它们移到 main 之前或在 main

之前声明它们

关于维度:

int a[10] 允许存储 10 个 int,索引为 0 .. 9

int highest[2][3] 允许存储 2*3 = 6 int,第一个索引是 0..1,第二个是 0..2

等等

您的数组允许存储 0*0 = 0 个元素,您是否只需要访问 highest[0][0] 您需要将它们定义为 int highest[1] [1]; 和另一个数组一样,但在那种情况下,有趣的是什么?你只需要一个 int var,而不是一个数组

我鼓励你需要阅读有关 C 语言的书籍/教程

我还鼓励您在编译时请求高级别警告/错误,例如,如果您使用 gcc do gcc -pedantic -Wall ....

关于c - 如何在C中的二维数组中找到最高和最低的数字及其位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54382619/

相关文章:

java - 诊断性能问题

matlab - 如何在不编写 FOR 循环的情况下查看数组结构的所有元素?

PHP - 检查值不会在数组中出现两次

c - 从 C 中的命令行和控制台(STDIN)获取输入

c - Sem_wait() 在第一个 true 条件后不阻塞

c - 链表,分配字符数组 [C]

javascript - 使用点符号作为键不可能从数组的对象创建修改后的数组

arrays - 在 x86 程序集中交换数组值时出现错误值

c - 如何继续 fork 进程,直到 "almost"内存不足

java - 动态数组算法