 # fjarboleda's solution

## to Saddle Points in the C Track

Published at Oct 13 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.

``````#include "vendor/unity.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[],
{
// 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();
}
}

{
uint8_t matrix = {
{9, 8, 7},
{5, 3, 2},
{6, 6, 7}
};

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

{
TEST_IGNORE();               // delete this line to run test
size_t expected_count = 0;
check_count(expected_count, actual->count);
free(actual);
}

{
TEST_IGNORE();
uint8_t matrix = {
{1, 2, 3},
{3, 1, 2},
{2, 3, 1}
};

size_t expected_count = 0;
check_count(expected_count, actual->count);
free(actual);
}

{
TEST_IGNORE();
uint8_t matrix = {
{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} };
check_count(expected_count, actual->count);
check_points(expected_count, expected_points, actual->points);
free(actual);
}

{
TEST_IGNORE();
uint8_t matrix = {
{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} };
check_count(expected_count, actual->count);
check_points(expected_count, expected_points, actual->points);
free(actual);
}

{
TEST_IGNORE();
uint8_t matrix = {
{8, 7, 9},
{6, 7, 6},
{3, 2, 5}
};

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

{
TEST_IGNORE();
uint8_t matrix = {
{3, 1, 3},
{3, 2, 4}
};

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

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

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

static void
{
TEST_IGNORE();
uint8_t matrix = {
{2, 5, 3, 5}
};

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

int main(void)
{

RUN_TEST
RUN_TEST

return UnityEnd();
}``````

``````#ifndef SADDLE_POINTS_H

#include <stdint.h>

typedef uint8_t coord_t;
typedef uint8_t value_t;

typedef struct
{
coord_t row;
coord_t column;
}
point_t;

typedef struct
{
size_t count;
size_t capacity;
point_t *points;
}
vector_point_t;

value_t matrix[][columns]);

#endif``````

``````#include <stdlib.h>
#include <stdbool.h>

static bool greater_or_equal_row(
const value_t value,
const coord_t columns,
const value_t *matrix, coord_t x)
{
value_t *i_matrix = (value_t *)matrix + (x*columns);

for(coord_t k=0; k<columns; ++k)
{
if( value < *i_matrix )
return false;

i_matrix++;
}
return true;
}

static bool less_or_equal_column(
const value_t value,
const coord_t rows, const coord_t columns,
const value_t *matrix, coord_t y)
{
value_t *i_matrix = (value_t *)matrix +y;

for(coord_t k=0; k<rows; ++k)
{
if( value > *i_matrix )
return false;

i_matrix +=columns;
}
return true;
}

{
}

static vector_point_t* saddlePointsCreate( const size_t Capacity )
{
vector_point_t* result = (vector_point_t*) calloc (1, sizeof(vector_point_t));
if(!result) exit(EXIT_FAILURE);

if(0 < Capacity)
{
result->points = (point_t *) calloc (Capacity, sizeof(point_t));
if(!result->points) exit (EXIT_FAILURE);
}

result->capacity = Capacity;

return (result);
}

{
size_t nc = new_capacity;

if(self->count >= nc)
{
nc = self->count;
return nc;
}

if(!nc)
{
free(self->points);
self->points = NULL;
}
else
{
self->points = (point_t *) realloc (self->points, nc * sizeof(vector_point_t) );
if(!self->points) exit (EXIT_FAILURE);
}

return nc;
}

static void saddlePointsAlgol(const coord_t rows, const coord_t columns,
const value_t *matrix, vector_point_t* result)
{
result->count = 0;

value_t *i_mat = (value_t *)matrix;

for(coord_t i=0;i<rows;++i)
{
for(coord_t j=0;j<columns;++j)
{
value_t mij = *i_mat++;

if( greater_or_equal_row(mij, columns, matrix, i) &&
less_or_equal_column(mij, rows, columns, matrix, j) )
}
}
}

{
free(self->points);
free(self);
}

value_t matrix[][columns])
{
vector_point_t* result = saddlePointsCreate( rows );

if(result)
{
}

}``````