(C language) Program to output the maximum value of elements in a two-dimensional array, which requires pointers to be implemented.

(C language) Program to output the maximum value of elements in a two-dimensional array, which requires pointers to be implemented.

#include<stdio.h>
#include<stdlib.h>
#define N 6 //行数 
#define M 8 //列数 
int main(){
    
    
	
	int a[N][M] = {
    
    {
    
    4,5,6,7,8,9,10,11,}, {
    
    8,9,10,11,12,13,14,15} ,{
    
    6,7,8,9,10,11,12,13},{
    
    10,11,12,13,14,15,16,17},
	{
    
    20,21,22,23,24,25,26,27},{
    
    30,31,32,33,34,35,36,37}};
	int i,j=0,max;
	/*for(i=0;i<N;i++){
		for(j=0;j<M;j++){
			printf("a[%d][%d]=",i,j);
			scanf("%d",&a[i][j]);
		}
	}*///此处为建立二维数组的过程
	//数组的方法寻找最大值 
	for(i=0;i<N;i++){
    
    
		for(j=0;j<M;j++){
    
    
			if(max<a[i][j]){
    
    
				max=a[i][j];
			}
		}
	}
	printf("二位数组的最大值为:%d\n",max);
	
	//指针的方法寻找最大值 
	int (*p)[M]=a;
	for(i=0;i<M;i++){
    
    
		for(j=0;j<N;j++){
    
    
			if(max<*((*p+i)+j)){
    
    
				max=*((*p+i)+j);
			}
		}
	}
	printf("二位数组的最大值为:%d\n",max);
	
	
}

Guess you like

Origin blog.csdn.net/WuwuwuH_/article/details/113752492