Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

A saddle point of a matrix is as an element whose value is greater than or equal

ID: 3572452 • Letter: A

Question

A saddle point of a matrix is as an element whose value is greater than or equal to every element in its row, but less than or equal to every element in its column. For example, the matrix A = (1 2 3 4 5 6 7 8 9) has only one saddle point and it is in the [1, 3] position. Write a function that finds all the saddle points of a given matrix (note that there may be more than one saddle point!). Your function should return a matrix of indices that has exactly two columns. Each row of this matrix should correspond to one saddle point with the first element of the row containing the row index of the saddle point and the second clement containing the column index. If there is no saddle point in M, then your function should return an empty array.

Explanation / Answer

#include<stdio.h>

main(){
   // test data
   int data[3][3] = {{1,2,3},{4,5,6},{7,8,9}};
   int rows = sizeof(data)/sizeof(data[0]);
   int columns = sizeof(data[0])/sizeof(data[0][0]);
  
   int saddle_points[rows*columns][2];
   int current_index = 0;
  
   int row,column,currentRow,currentColumn;
   for(row=0;row<rows;row++){
       for(column=0;column<columns;column++){
          
           // checking element is saddle point or not
           bool isSaddle = true;
          
           // checking in current row
           for(currentColumn=0;currentColumn<columns;currentColumn++){
               if(data[row][column] < data[row][currentColumn]){
                   isSaddle = false;
                   break;
               }
           }
           if(isSaddle == false){
               continue;
           }
          
           // checking in current column
           for(currentRow=0;currentRow<rows;currentRow++){
               if(data[row][column] > data[currentRow][currentColumn-1]){
                   isSaddle = false;
                   break;
               }
           }
          
           // storing the point
           if(isSaddle == true){
               saddle_points[current_index][0] = row + 1;
               saddle_points[current_index][1] = column + 1;
               current_index++;
           }
       }
   }
  
   // printing saddle points
   for(row=0;row<current_index;row++){
       printf("Row = %d, Column = %d",saddle_points[row][0],saddle_points[row][1]);
   }  
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote