An m x n matrix is said to have a saddle point if some entry a[i][j] is the smallest value inrow i and the largest value in j. Write C/ C++ function that determines the location of asaddle point if one exists.
--------------------------------------------------------------------------------------------------------------------------
#include<iostream>
using namespace std;
//Author : Vivek S. Sharma
//Copyright : Your copyright notice
class matrix{
int a[10][10];
int r,c;
public :
void get_data(){
cout<<"Enter no of rows and columns";
cin>>r>>c;
cout<<"\n Enter the elements \n";
for(int i=0 ; i<r ; i++){
for(int j=0 ; j<c ; j++){
cin>>a[i][j];
}
}
}
void display(){
cout<<"\n the matrix u entered is : \n";
for(int i=0 ; i<r ; i++){
for(int j=0 ; j<c ; j++){
cout<<" "<<a[i][j];
}
cout<<"\n";
}
}
void saddle(){
int x,y;
for(int i=0 ; i<r ; i++){
for(int j=0 ; j<c ; j++){
x=0,y=0;
for(int k=0 ; k<c ; k++){
if(a[i][j]<a[i][k] || j==k)
x++;
}
if(x==c){
for(int l=0 ; l<r ; l++){
if(a[i][j]>a[l][j] || i==l){
y++;
if(y==r)
{
cout<<"Yes, saddle point exists, and it is : "<<a[i][j];
}
else cout<<"\nSaddle point does not exist";
}
}
}
}
}
}
};
int main(){
matrix m;
m.get_data();
m.display();
m.saddle();
return 0;
}
--------------------------------------------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------------------------------------------
#include<iostream>
using namespace std;
//Author : Vivek S. Sharma
//Copyright : Your copyright notice
class matrix{
int a[10][10];
int r,c;
public :
void get_data(){
cout<<"Enter no of rows and columns";
cin>>r>>c;
cout<<"\n Enter the elements \n";
for(int i=0 ; i<r ; i++){
for(int j=0 ; j<c ; j++){
cin>>a[i][j];
}
}
}
void display(){
cout<<"\n the matrix u entered is : \n";
for(int i=0 ; i<r ; i++){
for(int j=0 ; j<c ; j++){
cout<<" "<<a[i][j];
}
cout<<"\n";
}
}
void saddle(){
int x,y;
for(int i=0 ; i<r ; i++){
for(int j=0 ; j<c ; j++){
x=0,y=0;
for(int k=0 ; k<c ; k++){
if(a[i][j]<a[i][k] || j==k)
x++;
}
if(x==c){
for(int l=0 ; l<r ; l++){
if(a[i][j]>a[l][j] || i==l){
y++;
if(y==r)
{
cout<<"Yes, saddle point exists, and it is : "<<a[i][j];
}
else cout<<"\nSaddle point does not exist";
}
}
}
}
}
}
};
int main(){
matrix m;
m.get_data();
m.display();
m.saddle();
return 0;
}
--------------------------------------------------------------------------------------------------------------------------
**If some mistakes, plz let me know!-----Vivek S. Sharma
No comments:
Post a Comment