Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
C
controle-c-002
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
This is an archived project. Repository and other project resources are read-only.
Show more breadcrumbs
florian.burgener
controle-c-002
Commits
c4e4983f
Commit
c4e4983f
authored
3 years ago
by
florian.burgener
Browse files
Options
Downloads
Patches
Plain Diff
Exercice 4
parent
878439f1
Branches
Branches containing commit
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
ex4/ex4.c
+78
-24
78 additions, 24 deletions
ex4/ex4.c
with
78 additions
and
24 deletions
ex4/ex4.c
+
78
−
24
View file @
c4e4983f
...
...
@@ -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
;
}
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment