How to find the highest and the lowest number and its position in a bidimensional array in C

时间:2019-01-26 20:49:41

标签: c arrays multidimensional-array

I'm new here. I'm beginning studying programming and C++. I have to do a program that requests numbers to the user to fill an array, and then a function to find the highest and the lowest value and its position in the array. This is what I have now, it works to find the highest number and its position, but it does not work to find the lowest, the lowest value that has found isn't correct and either its position:

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];
                }
            }
        }
    }
}

Thank you very much. (excuse my english, i'm learning too).

2 个答案:

答案 0 :(得分:3)

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

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

答案 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;
      }
    }
}

这应该可以解决问题。