lesson 11?

This commit is contained in:
deck committed 2026-10-08 19:15:21 +02:00
1 parent 9d3fc38b3c
commit 7ea0385b32
1 file changed
+23 -16
+23 -16
View File
@@ -1,23 +1,30 @@
#include <stdio.h>
typedef struct {
int x, y;
} Point;
/* TODO: return the squared distance between a and b. */
int dist2(Point a, Point b) {
int result = ((b.x - a.x) * (b.x - a.x)) + ((b.y - a.y) * (b.y - a.y));
return result;
}
#include <stdlib.h>
int main(void) {
int x1, y1, x2, y2;
scanf("%d %d %d %d", &x1, &y1, &x2, &y2);
int n;
if (scanf("%d", &n) != 1)
return 0;
Point a = {x1, y1};
Point b = {x2, y2};
/* TODO: build a from (x1, y1) and b from (x2, y2). */
/* TODO: replace NULL with a malloc call that reserves room for n ints. */
int *nums = malloc(n * sizeof n);
if (nums == NULL) {
fprintf(stderr, "no memory \n");
/* TODO: allocation failed: print a message to stderr and return 1. */
return 1;
}
printf("%d\n", dist2(a, b));
for (int i = 0; i < n; i++)
scanf("%d", &nums[i]);
long int sum = 0;
for (int i = 0; i < n; i++)
sum = sum + nums[i];
for (int i = n; n > 0; n--) {
printf("%d ", nums[n - 1]);
}
printf("%ld", sum);
free(nums);
return 0;
}