Finding saddle points in a two-dimensional array (C++ exercises)

what's saddle point?

A saddle point in a two-dimensional array means that the element value at that position is the maximum value in the row and the minimum value in the column. There may also be no saddle points.

Up code

#include<iostream>
#include <stdlib.h> 
#include <time.h>
#include<iomanip>
using namespace std;
#define N 4 //行 
#define M 5 //列 

bool judge(int arr[N][M],int value,int row_set,int col_set) {
    
    

	//求每行最大 0行 5列 s<5  e是传进来的每个数组值 
    for(int s=0;s<M;s++){
    
    
        if (value < arr[row_set][s])
            return false;
    }
    
	//求每列最大 0列 4行 k<4   e是传进来的每个数组值 
    for(int k=0;k<N;k++){
    
    
        if (value > arr[k][col_set])
            return false;
    }
    return true;
}

//找鞍点函数  
void find(int arr[N][M], int* row, int* col){
    
    
	
	for(int i=0;i<N;i++){
    
    
        for(int j=0;j<M;j++){
    
    
            int value = arr[i][j];
            bool result = judge(arr,value,i,j);
           
            row =&i;
            col =&j;
            
            if(result){
    
    
            	cout << value <<" :yes saddle point"<<endl;
                cout<<"鞍点位于第"<<*row<<"行"<<*col<<"列"<<endl; 
			}
        }
    }
	
}



int main(){
    
    
	int arr[N][M]={
    
    
	1,2,3,4,5,
	2,4,6,8,10,
	3,6,9,12,15,
	4,8,12,16,20
	};
	cout<<"二维数组为:"<<endl; 
	for(int i=0;i<N;i++){
    
    
        for(int j=0;j<M;j++){
    
    
			cout<<setw(4)<<arr[i][j];
		}
		cout<<endl;
	}
	cout<<endl;
	int row=0,col=0;
	find(arr,&row,&col);
    return 0;
} 

Test effect

Insert image description here

Guess you like

Origin blog.csdn.net/weixin_45833112/article/details/127322027