Avatar of TheRealSami

TheRealSami's solution

to Saddle Points in the C Track

Published at Sep 25 2019 · 0 comments
Instructions
Test suite
Solution

Note:

This exercise has changed since this solution was written.

Detect saddle points in a matrix.

So say you have a matrix like so:

    1  2  3
  |---------
1 | 9  8  7
2 | 5  3  2     <--- saddle point at column 1, row 2, with value 5
3 | 6  6  7

It has a saddle point at column 1, row 2.

It's called a "saddle point" because it is greater than or equal to every element in its row and less than or equal to every element in its column.

A matrix may have zero or more saddle points.

Your code should be able to provide the (possibly empty) list of all the saddle points for any given matrix.

The matrix can have a different number of rows and columns (Non square).

Note that you may find other definitions of matrix saddle points online, but the tests for this exercise follow the above unambiguous definition.

Getting Started

Make sure you have read the "Guides" section of the C track on the Exercism site. This covers the basic information on setting up the development environment expected by the exercises.

Passing the Tests

Get the first test compiling, linking and passing by following the three rules of test-driven development.

The included makefile can be used to create and run the tests using the test task.

make test

Create just the functions you need to satisfy any compiler errors and get the test to fail. Then write just enough code to get the test to pass. Once you've done that, move onto the next test.

As you progress through the tests, take the time to refactor your implementation for readability and expressiveness and then go on to the next test.

Try to use standard C99 facilities in preference to writing your own low-level algorithms or facilities by hand.

Source

J Dalbey's Programming Practice problems http://users.csc.calpoly.edu/~jdalbey/103/Projects/ProgrammingPractice.html

Submitting Incomplete Solutions

It's possible to submit an incomplete solution so you can see how others have completed the exercise.

test_saddle_points.c

#include "vendor/unity.h"
#include "../src/saddle_points.h"
#include <stdlib.h>
#include <stdbool.h>

void setUp(void)
{
}

void tearDown(void)
{
}

static void check_count(size_t expected, size_t actual)
{
   TEST_ASSERT_EQUAL_UINT64(expected, actual);
}

static void check_points(size_t count, saddle_point_t expected[],
                         saddle_point_t actual[])
{
   // assumes array counts match
   for (size_t i = 0; i < count; ++i) {
      bool point_found = false;
      for (size_t j = 0; j < count; ++j) {
         if (expected[i].row == actual[j].row
             && expected[i].column == actual[j].column) {
            point_found = true;
            break;
         }
      }
      if (!point_found)
         TEST_FAIL();
   }
}

static void test_single_saddle_point(void)
{
   uint8_t matrix[3][3] = {
      {9, 8, 7},
      {5, 3, 2},
      {6, 6, 7}
   };

   size_t expected_count = 1;
   saddle_point_t expected_points[] = { {2, 1} };
   saddle_points_t *actual = saddlePoints(3, 3, matrix);
   check_count(expected_count, actual->count);
   check_points(expected_count, expected_points, actual->points);
   free(actual);
}

static void test_empty_matrix_has_no_saddle_points(void)
{
   TEST_IGNORE();               // delete this line to run test
   size_t expected_count = 0;
   saddle_points_t *actual = saddlePoints(0, 0, NULL);
   check_count(expected_count, actual->count);
   free(actual);
}

static void test_lack_of_saddle_points_when_there_are_none(void)
{
   TEST_IGNORE();
   uint8_t matrix[3][3] = {
      {1, 2, 3},
      {3, 1, 2},
      {2, 3, 1}
   };

   size_t expected_count = 0;
   saddle_points_t *actual = saddlePoints(3, 3, matrix);
   check_count(expected_count, actual->count);
   free(actual);
}

static void test_multiple_saddle_points_in_a_column(void)
{
   TEST_IGNORE();
   uint8_t matrix[3][3] = {
      {4, 5, 4},
      {3, 5, 5},
      {1, 5, 4}
   };

   size_t expected_count = 3;
   saddle_point_t expected_points[] = { {1, 2}, {2, 2}, {3, 2} };
   saddle_points_t *actual = saddlePoints(3, 3, matrix);
   check_count(expected_count, actual->count);
   check_points(expected_count, expected_points, actual->points);
   free(actual);
}

static void test_multiple_saddle_points_in_a_row(void)
{
   TEST_IGNORE();
   uint8_t matrix[3][3] = {
      {6, 7, 8},
      {5, 5, 5},
      {7, 5, 6}
   };

   size_t expected_count = 3;
   saddle_point_t expected_points[] = { {2, 1}, {2, 2}, {2, 3} };
   saddle_points_t *actual = saddlePoints(3, 3, matrix);
   check_count(expected_count, actual->count);
   check_points(expected_count, expected_points, actual->points);
   free(actual);
}

static void test_saddle_point_in_bottom_right_corner(void)
{
   TEST_IGNORE();
   uint8_t matrix[3][3] = {
      {8, 7, 9},
      {6, 7, 6},
      {3, 2, 5}
   };

   size_t expected_count = 1;
   saddle_point_t expected_points[] = { {3, 3} };
   saddle_points_t *actual = saddlePoints(3, 3, matrix);
   check_count(expected_count, actual->count);
   check_points(expected_count, expected_points, actual->points);
   free(actual);
}

static void test_saddle_points_in_non_square_matrix(void)
{
   TEST_IGNORE();
   uint8_t matrix[2][3] = {
      {3, 1, 3},
      {3, 2, 4}
   };

   size_t expected_count = 2;
   saddle_point_t expected_points[] = { {1, 3}, {1, 1} };
   saddle_points_t *actual = saddlePoints(2, 3, matrix);
   check_count(expected_count, actual->count);
   check_points(expected_count, expected_points, actual->points);
   free(actual);
}

static void
    test_saddle_points_in_single_column_matrix_are_those_with_the_minimum_value
    (void) {
   TEST_IGNORE();
   uint8_t matrix[4][1] = {
      {2},
      {1},
      {4},
      {1}
   };

   size_t expected_count = 2;
   saddle_point_t expected_points[] = { {2, 1}, {4, 1} };
   saddle_points_t *actual = saddlePoints(4, 1, matrix);
   check_count(expected_count, actual->count);
   check_points(expected_count, expected_points, actual->points);
   free(actual);
}

static void
test_saddle_points_in_single_row_matrix_are_those_with_the_maximum_value(void)
{
   TEST_IGNORE();
   uint8_t matrix[1][4] = {
      {2, 5, 3, 5}
   };

   size_t expected_count = 2;
   saddle_point_t expected_points[] = { {1, 2}, {1, 4} };
   saddle_points_t *actual = saddlePoints(1, 4, matrix);
   check_count(expected_count, actual->count);
   check_points(expected_count, expected_points, actual->points);
   free(actual);
}

int main(void)
{
   UnityBegin("test/test_saddle_points.c");

   RUN_TEST(test_single_saddle_point);
   RUN_TEST(test_empty_matrix_has_no_saddle_points);
   RUN_TEST(test_lack_of_saddle_points_when_there_are_none);
   RUN_TEST(test_multiple_saddle_points_in_a_column);
   RUN_TEST(test_multiple_saddle_points_in_a_row);
   RUN_TEST(test_saddle_point_in_bottom_right_corner);
   RUN_TEST(test_saddle_points_in_non_square_matrix);
   RUN_TEST
       (test_saddle_points_in_single_column_matrix_are_those_with_the_minimum_value);
   RUN_TEST
       (test_saddle_points_in_single_row_matrix_are_those_with_the_maximum_value);

   return UnityEnd();
}

src/saddle_points.c

#include <stdlib.h>
#include <stdint.h>
#include "saddle_points.h"


struct matrix_dimension {
  int rows;
  int cols;
};

struct matrix_position {
  int row_index;
  int col_index;
};


static int is_max_in_row( struct matrix_position* pos,
			   struct matrix_dimension* dim,
			   uint8_t matrix[][ dim->cols ] );
static int is_min_in_col( struct matrix_position* pos,
			   struct matrix_dimension* dim,
			   uint8_t matrix[][ dim->cols ] );
static void add_saddle_point( struct matrix_position* position,
			        saddle_points_t* points );


saddle_points_t* saddlePoints( int rows, int cols, uint8_t matrix[rows][cols] )
{
  saddle_points_t* points = calloc( sizeof( saddle_points_t ), 1 );
  struct matrix_dimension dim = { rows, cols };

  for( int i = 0; i < rows; i++ )
  {
    for( int j = 0; j < cols; j++ )
    {
      struct matrix_position position = { i, j };
      
      if( is_max_in_row( &position, &dim, matrix ) &&
	  is_min_in_col( &position, &dim, matrix ) )
	add_saddle_point( &position, points );
    }
  }

  return points;
}


static int is_max_in_row( struct matrix_position* position,
			   struct matrix_dimension* dim,
			   uint8_t matrix[][ dim->cols ] )
{
  int current_value = matrix[ position->row_index ][ position->col_index ];
  
  for( int i = 0; i < dim->cols; i++ )
    if( matrix[ position->row_index ][ i ] > current_value )
      return 0;

  return 1;
}

static int is_min_in_col( struct matrix_position* position,
			  struct matrix_dimension* dim,
			  uint8_t matrix[][ dim->cols ] )
{
  int current_value = matrix[ position->row_index ][ position->col_index ];
  
  for( int i = 0; i < dim-> rows; i++ )
    if( matrix[ i ][ position->col_index ] < current_value )
      return 0;

  return 1;
}

static void add_saddle_point( struct matrix_position* position,
			        saddle_points_t* points )
{
  points->points[ points->count ].row = position->row_index + 1;
  points->points[ points->count ].column = position->col_index + 1;
  points->count++;
}

src/saddle_points.h

#ifndef SADDLE_POINTS_H
#define SADDLE_POINTS_H

#define MAX_SADDLE_POINTS 32

typedef struct {
  int row;
  int column;
} saddle_point_t;

typedef struct {
  int count;
  saddle_point_t points[ MAX_SADDLE_POINTS ];
} saddle_points_t;


saddle_points_t* saddlePoints( int rows, int cols, uint8_t matrix[rows][cols] );


#endif

Community comments

Find this solution interesting? Ask the author a question to learn more.

What can you learn from this solution?

A huge amount can be learned from reading other people’s code. This is why we wanted to give exercism users the option of making their solutions public.

Here are some questions to help you reflect on this solution and learn the most from it.

  • What compromises have been made?
  • Are there new concepts here that you could read more about to improve your understanding?