C language: Find the maximum value in a two-dimensional array

  • Find the maximum value in a two-dimensional array
  • Idea:
     Create a variable to store the first element of the array
     and use a for loop to traverse the array. If it is larger than the first element of the array, replace max with a larger number 
#include<stdio.h>
int main() {
//  二维数组中求最大值
//	创建一个变量存储数组第一个元素
//  用for循环以此遍历数组,如果比数组第一个元素大,就把max替换为大的数
//	int a[2][3]= {
   
   {100,757,36},{121,899,989}};
	int a[2][3]= {100,757,36,121,899,989};
	int max=a[0][0];
	int index_i=0,index_j=0;
	for(int i=0; i<2; i++) {
		for(int j=0; j<3; j++)  {//for循环嵌套遍历二维数组 
			if(max<a[i][j]) {
				max=a[i][j];
				index_i=i;//获取下标 
				index_j=j;
			}
		}
	}
	printf("二维数组最大值为:%d\n",max);
	printf("下标i=%d\t,j=%d",index_i,index_j);
	return 0;
}

The output is as follows

 

Guess you like

Origin blog.csdn.net/weixin_63987141/article/details/129171761