繁体   English   中英

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

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

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


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

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

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

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

即使那看起来很奇怪


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

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

除了[0][0]之外,您永远不会写成最低的,因此除lowest[i][j]为0之外,没有定义lowest[i][j]


highlowmain中调用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 highest[1][1]; 与其他数组相同,但是在这种情况下,间隔是多少? 您只需要一个int var,而不是一个数组

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

我也鼓励您在编译时要求高水平的警告/错误,例如,如果您使用gcc do gcc -pedantic -Wall ....

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

这应该可以解决问题。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM