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

Exercice 4

parent 878439f1
Branches
No related tags found
No related merge requests found
......@@ -16,30 +16,84 @@
#include <stdlib.h>
#include <string.h>
typedef struct Point {
int32_t x;
int32_t y;
} Point;
Point **read_points_from_input(int32_t points_length) {
Point **points = (Point **)malloc(sizeof(Point) * points_length);
for (int32_t i = 0; i < points_length; i += 1) {
Point *point = (Point *)malloc(sizeof(Point));
scanf("%d %d", &point->x, &point->y);
points[i] = point;
}
return points;
}
int32_t **new_matrix(int32_t row_count, int32_t column_count) {
int32_t **matrix = (int32_t **)malloc(sizeof(int32_t *) * row_count);
for (int32_t i = 0; i < row_count; i += 1) {
matrix[i] = (int32_t *)malloc(sizeof(int32_t) * column_count);
}
return matrix;
}
int32_t calculate_distance(Point *p1, Point *p2) {
return abs(p2->x - p1->x) + abs(p2->y - p1->y);
}
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 j = 0; j < points_length; j += 1) {
distances[i][j] = calculate_distance(points[i], points[j]);
}
}
}
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 j = 0; j < column_count; j += 1) {
printf("%d ", matrix[i][j]);
}
printf("\n");
}
}
void delete_matrix(int32_t **matrix, int32_t row_count) {
for (int32_t i = 0; i < row_count; i += 1) {
free(matrix[i]);
}
free(matrix);
}
void delete_points(Point **points, int32_t points_length) {
for (int32_t i = 0; i < points_length; i += 1) {
free(points[i]);
}
free(points);
}
int main() {
// int32_t values_length = 5;
// double values[values_length];
// for (int32_t i = 0; i < values_length; i += 1) {
// double value;
// scanf("%lf", &value);
// values[i] = value;
// }
// int32_t values_length = 5;
// int32_t values[values_length];
// for (int32_t i = 0; i < values_length; i += 1) {
// int32_t value;
// scanf("%d", &value);
// values[i] = value;
// }
// char a[100];
// int32_t b;
// scanf("%s %d", a, &b);
// printf("%s %d\n", a, b);
printf("ex4\n");
int32_t points_length;
scanf("%d", &points_length);
Point **points = read_points_from_input(points_length);
int32_t **distances = new_matrix(points_length, points_length);
compute_distances(distances, points, points_length);
printf("\n");
print_matrix(distances, points_length, points_length);
delete_matrix(distances, points_length);
delete_points(points, points_length);
return EXIT_SUCCESS;
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment