【问题标题】:How to find the highest and the lowest number and its position in a bidimensional array in C如何在C中找到二维数组中的最高和最低数字及其位置
【发布时间】:2019-01-26 20:49:40
【问题描述】:

我是新来的。 我开始学习编程和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];
                }
            }
        }
    }
}

非常感谢。 (对不起我的英语,我也在学习)。

【问题讨论】:

  • Plàcid Masvidal,您使用的编译器和设置没有警告int highest[0][0];

标签: c arrays multidimensional-array


【解决方案1】:
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;

你写出数组,就像你可以访问这两个数组之后的每一个地方

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


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

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

那句话什么也没做,可能是你想要的

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

?即使这看起来很奇怪


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

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 语言的书籍/教程

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

【讨论】:

  • 注意:0 不是标准 C 中的有效数组大小。“它的值应大于零”C11 §6.7.6.2 1
【解决方案2】:
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 

您正在声明长度为零的数组。如果您只是想保存最高和最低值,为什么不使用简单的整数呢?所以:

int highest = 0;
int lowest = 0;

这两个值应该设置为main开头的多维数组的第一个值。之后,您可以遍历数组并将当前数组元素与之前的最高和最低值进行比较。所以在 main 的开头:

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

此外,最高值和最低值的位置需要单独的参数:

int positionX_highest = 0;
int positionY_highest = 0;
int positionX_lowest = 0;
int positionY_lowest = 0;

你的循环应该没问题。但是,循环内的比较需要分开。最低值的 if 查询永远不会在您的代码中执行。分别检查条件:

for (int i = 0; i < sizef; i++) {
    for (int j = 0; j < sizec; j++) {
      if (array[i][j] > highest) 
      {
            //If the current array element is higher than the highest so far, save it
            highest = array[i][j];
            positionX_highest = i;
            positionY_highest = j;
      }
      if (array[i][j] < lowest)
      {
            //If the current array element is lower than the lowest so far, save it
            lowest = array[i][j];
            positionX_lowest = i;
            positionY_lowest = j;
      }
    }
}

这应该可以解决问题。

【讨论】:

    猜你喜欢
    • 2022-01-05
    • 1970-01-01
    • 1970-01-01
    • 2017-02-02
    • 2017-02-13
    • 1970-01-01
    • 2017-09-29
    • 2018-04-10
    • 2013-05-03
    相关资源
    最近更新 更多