求二维数组的最小值及其下标
时间: 2024-06-17 10:07:01 浏览: 18
要求二维数组的最小值及其下标,可以使用两重循环遍历数组,并使用变量来记录最小值和最小值所在的行、列下标。具体实现如下:
```c++
int min_val = arr[0];
int min_row = 0;
int min_col = 0;
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
if (arr[i][j] < min_val) {
min_val = arr[i][j];
min_row = i;
min_col = j;
}
}
}
// 输出最小值及其下标
cout << "最小值为:" << min_val << endl;
cout << "所在行下标为:" << min_row << endl;
cout << "所在列下标为:" << min_col << endl;
```
相关问题
Java实现 找二维数组最小值及下标
以下是Java程序实现找二维数组最小值及下标的示例代码:
```java
public class FindMinIn2DArray {
public static void main(String[] args) {
int[][] arr = { { 2, 3, 4 }, { 5, 6, 1 }, { 8, 9, 7 } };
int min = arr[0][0];
int minRow = 0;
int minCol = 0;
for (int i = 0; i < arr.length; i++) {
for (int j = 0; j < arr[i].length; j++) {
if (arr[i][j] < min) {
min = arr[i][j];
minRow = i;
minCol = j;
}
}
}
System.out.println("最小值为:" + min);
System.out.println("最小值所在行:" + minRow);
System.out.println("最小值所在列:" + minCol);
}
}
```
运行结果:
```
最小值为:1
最小值所在行:1
最小值所在列:2
```
该程序通过遍历二维数组的每个元素,找到最小值及其所在行列的下标,并输出结果。
用c语言求二维数组中的最小数及其下标
以下是用C语言求二维数组中的最小数及其下标的示例代码:
```
#include <stdio.h>
int main() {
int arr[3][4] = {{5, 3, 2, 4}, {9, 7, 8, 6}, {1, 10, 12, 11}}; // 3x4的二维数组
int min = arr[0][0]; // 假设第一个元素为最小值
int row = 0, col = 0; // 最小值的行和列
// 遍历整个数组,找到最小值及其下标
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 4; j++) {
if (arr[i][j] < min) {
min = arr[i][j];
row = i;
col = j;
}
}
}
printf("最小值为:%d\n", min);
printf("最小值的下标为:(%d,%d)\n", row, col);
return 0;
}
```
输出结果为:
```
最小值为:1
最小值的下标为:(2,0)
```
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://img-home.csdnimg.cn/images/20210720083646.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)