Skip to content
Snippets Groups Projects
Commit 19c4eeaa authored by florian.burgener's avatar florian.burgener
Browse files

Validation ex4

parent aa10b067
No related branches found
No related tags found
No related merge requests found
...@@ -9,18 +9,21 @@ ...@@ -9,18 +9,21 @@
* *
*/ */
#include <math.h>
#include <stdbool.h>
#include <stdint.h> #include <stdint.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h>
typedef struct Point { typedef struct Point {
int32_t x; int32_t x;
int32_t y; int32_t y;
} Point; } Point;
/**
* @brief Read the points that are in the input.
*
* @param points_length
* @return Point**
*/
Point **read_points_from_input(int32_t points_length) { Point **read_points_from_input(int32_t points_length) {
Point **points = (Point **)malloc(sizeof(Point) * points_length); Point **points = (Point **)malloc(sizeof(Point) * points_length);
...@@ -33,6 +36,13 @@ Point **read_points_from_input(int32_t points_length) { ...@@ -33,6 +36,13 @@ Point **read_points_from_input(int32_t points_length) {
return points; return points;
} }
/**
* @brief Create a new matrix of size row x column.
*
* @param row_count
* @param column_count
* @return int32_t**
*/
int32_t **new_matrix(int32_t row_count, int32_t column_count) { int32_t **new_matrix(int32_t row_count, int32_t column_count) {
int32_t **matrix = (int32_t **)malloc(sizeof(int32_t *) * row_count); int32_t **matrix = (int32_t **)malloc(sizeof(int32_t *) * row_count);
...@@ -43,10 +53,24 @@ int32_t **new_matrix(int32_t row_count, int32_t column_count) { ...@@ -43,10 +53,24 @@ int32_t **new_matrix(int32_t row_count, int32_t column_count) {
return matrix; return matrix;
} }
/**
* @brief Calculate the distance between two points.
*
* @param p1
* @param p2
* @return int32_t
*/
int32_t calculate_distance(Point *p1, Point *p2) { int32_t calculate_distance(Point *p1, Point *p2) {
return abs(p2->x - p1->x) + abs(p2->y - p1->y); return abs(p2->x - p1->x) + abs(p2->y - p1->y);
} }
/**
* @brief Calculates all possible distances with the list of points.
*
* @param distances
* @param points
* @param points_length
*/
void compute_distances(int32_t **distances, Point **points, int32_t points_length) { void compute_distances(int32_t **distances, Point **points, int32_t points_length) {
for (int32_t i = 0; i < points_length; i += 1) { for (int32_t i = 0; i < points_length; i += 1) {
for (int32_t j = 0; j < points_length; j += 1) { for (int32_t j = 0; j < points_length; j += 1) {
...@@ -55,6 +79,13 @@ void compute_distances(int32_t **distances, Point **points, int32_t points_lengt ...@@ -55,6 +79,13 @@ void compute_distances(int32_t **distances, Point **points, int32_t points_lengt
} }
} }
/**
* @brief Displays the matrix.
*
* @param matrix
* @param row_count
* @param column_count
*/
void print_matrix(int32_t **matrix, int32_t row_count, int32_t column_count) { void print_matrix(int32_t **matrix, int32_t row_count, int32_t column_count) {
for (int32_t i = 0; i < row_count; i += 1) { for (int32_t i = 0; i < row_count; i += 1) {
for (int32_t j = 0; j < column_count; j += 1) { for (int32_t j = 0; j < column_count; j += 1) {
...@@ -65,6 +96,12 @@ void print_matrix(int32_t **matrix, int32_t row_count, int32_t column_count) { ...@@ -65,6 +96,12 @@ void print_matrix(int32_t **matrix, int32_t row_count, int32_t column_count) {
} }
} }
/**
* @brief Deletes the matrix from the memory.
*
* @param matrix
* @param row_count
*/
void delete_matrix(int32_t **matrix, int32_t row_count) { void delete_matrix(int32_t **matrix, int32_t row_count) {
for (int32_t i = 0; i < row_count; i += 1) { for (int32_t i = 0; i < row_count; i += 1) {
free(matrix[i]); free(matrix[i]);
...@@ -73,6 +110,12 @@ void delete_matrix(int32_t **matrix, int32_t row_count) { ...@@ -73,6 +110,12 @@ void delete_matrix(int32_t **matrix, int32_t row_count) {
free(matrix); free(matrix);
} }
/**
* @brief Deletes the points from the memory.
*
* @param points
* @param points_length
*/
void delete_points(Point **points, int32_t points_length) { void delete_points(Point **points, int32_t points_length) {
for (int32_t i = 0; i < points_length; i += 1) { for (int32_t i = 0; i < points_length; i += 1) {
free(points[i]); free(points[i]);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment