mirror of
https://github.com/nolongeractivemp3/shipthatcode-c.git
synced 2026-11-24 10:45:12 +01:00
lesson 11?
This commit is contained in:
1 parent
9d3fc38b3c
commit
7ea0385b32
1 file changed
+23
-16
@@ -1,23 +1,30 @@
|
|||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
|
#include <stdlib.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;
|
|
||||||
}
|
|
||||||
|
|
||||||
int main(void) {
|
int main(void) {
|
||||||
int x1, y1, x2, y2;
|
int n;
|
||||||
scanf("%d %d %d %d", &x1, &y1, &x2, &y2);
|
if (scanf("%d", &n) != 1)
|
||||||
|
return 0;
|
||||||
|
|
||||||
Point a = {x1, y1};
|
/* TODO: replace NULL with a malloc call that reserves room for n ints. */
|
||||||
Point b = {x2, y2};
|
int *nums = malloc(n * sizeof n);
|
||||||
/* TODO: build a from (x1, y1) and b from (x2, y2). */
|
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;
|
return 0;
|
||||||
}
|
}
|
||||||
Reference in new issue
Block a user