Commit 85192d7e by unknown

Lab 12 tasks

parent 595fcfc9
make:
gcc task_1,2,3,bonus.c -o out -std=c99
./out
#include <stdio.h>
#include <math.h>
typedef struct {
float distance;
int points[2];
float midPoint[3];
} distance;
distance getDistance(float[], float[]);
void findMidPoint(distance[], float[][3]);
void sort(float[]);
int main() {
float points[][3] = {
{1, 3.1, 21},
{2, -3.2, 23},
{2.3, 12.8, 2},
{2, 1.4, -23},
{12, 2, -2.3}
};
int start = 1;
distance disList[10];
int loc = 0;
for (int i = 0; i < 5; i++) {
for (int j = start; j < 5; j++) {
disList[loc] = getDistance(points[i], points[j]);
disList[loc].points[0] = i;
disList[loc].points[1] = j;
loc++;
}
start++;
}
float shortDis = disList[0].distance;
distance shortPoint = disList[0];
for (int i = 1; i < loc; i++) {
distance dis = disList[i];
if (dis.distance < shortDis) {
shortDis = dis.distance;
shortPoint = dis;
}
}
printf("Shortest point is: POINT%d and POINT%d with distance: %f\n",
shortPoint.points[0],
shortPoint.points[1],
shortPoint.distance
);
findMidPoint(disList, points);
distance midDisList[10];
float midPointsList[10][3];
for (int i = 0; i < 10; i++) {
printf("Mid point of POINT%d and POINT%d is: (%.2f, %.2f, %.2f)\n",
disList[i].points[0],
disList[i].points[1],
disList[i].midPoint[0],
disList[i].midPoint[1],
disList[i].midPoint[2]
);
midPointsList[i][0] = disList[i].midPoint[0];
midPointsList[i][1] = disList[i].midPoint[1];
midPointsList[i][2] = disList[i].midPoint[2];
}
start = 1;
loc = 0;
for (int i = 0; i < 5; i++) {
for (int j = start; j < 5; j++) {
midDisList[loc] = getDistance(midPointsList[i], midPointsList[j]);
midDisList[loc].points[0] = i;
midDisList[loc].points[1] = j;
loc++;
}
start++;
}
float allMidDist[10];
for (int i = 0; i < 10; i++) {
allMidDist[i] = midDisList[i].distance;
}
sort(allMidDist);
printf("Sorted mean distance: ");
for (int i = 0; i < 10; i++) {
printf("%.3f, ", allMidDist[i]);
}
printf("\n");
return 0;
}
distance getDistance(float point1[], float point2[]) {
float x = pow(point1[0] - point2[0], 2);
float y = pow(point1[1] - point2[1], 2);
float z = pow(point1[2] - point2[2], 2);
distance dis;
dis.distance = sqrt(x + y + z);
return dis;
}
void findMidPoint(distance disList[], float points[][3]) {
for (int i = 0; i < 10; i++) {
int a = disList[i].points[0];
int b = disList[i].points[1];
float x = (points[a][0] + points[b][0]) / 2;
float y = (points[a][1] + points[b][1]) / 2;
float z = (points[a][2] + points[b][2]) / 2;
disList[i].midPoint[0] = x;
disList[i].midPoint[1] = y;
disList[i].midPoint[2] = z;
}
}
void sort(float input[]) {
int sorted = 0;
while (!sorted) {
sorted = 1;
for (int i = 0; i < 9; i++) {
if (input[i] > input[i + 1]) {
sorted = 0;
float a = input[i];
input[i] = input[i + 1];
input[i + 1] = a;
}
}
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment