use*_*736 0 c arrays location numbers
我在数组中编写了一个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;
}
Run Code Online (Sandbox Code Playgroud)
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
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
240 次 |
最近记录: |