C 数组 - 最大数及其位置

标签 c arrays numbers location

我在数组中编写了一个 5x6 随机数的代码,如何找到其中最大的数字然后打印它的位置 (x,y)?

#include <time.h>
#include <stdlib.h>
#include <stdio.h>
int main () {
 int array[5][6];
 srand(time(NULL));
 int x, y;
 for(x = 0; x < 5; x++) {
    for(y = 0; y < 6; y++) {
        array[x][y] = rand() % 31 + 10;
        printf("%d \t", array[x][y]);
   }
   printf("\n");
  }
 return 0;
}

最佳答案

int maxX = -1, maxY = -1;
int maxValue = 0
for(int x = 0; x < 5; x++) {
    for(int y = 0; y < 6; y++) {
        if(maxValue <= array[x][y]) {
            maxValue = array[x][y];
            maxX = x;
            maxY = y;
        }
    }
}
// print maxX and maxY and maxValue

关于C 数组 - 最大数及其位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17947972/

相关文章:

c - 二维字符和数字数组

html - 如果太高,输入类型 "number"会更改值 - 但为什么呢?

c++ - pselect() 无法识别具有任何 IO 事件的套接字

c - '(' token 之前的预期标识符

javascript - 多维 json 数组的问题....用 fiddle

php - 数字和 CSS 类

c - PaStreamCallbackTimeInfo成员全部为0

c++ - 基于图 block 的横向卷轴游戏教程?

Java UDP 服务器只接收数组中的第一个整数

ios - NSArray 元素无法匹配 Swift 数组元素类型预期 NSMutableArray 但发现 __NSDictionaryI :