Commit c493bd4c by chazog

Upload New File

parent 53d6d295
Showing with 110 additions and 0 deletions
#include <stdio.h>
#include <math.h>
#define NoOfDimensions 2
void distancesf(float points[5][3], float distances[10]);
void MeanPointf(float points[5][3], float MeanPoint[3]);
float AvgDist(float points[5][3], float MeanPoint[3], float MeanDist[5], int PointNumber[5]);
int main()
{
int i, j, swap, PointNumber[5];
float MeanPoint[3];
float MeanDist[5];
float points[5][3] = {{1, 3.1, 2.1},
{2, -3.2, 23},
{2.3, 12.8, 2},
{2, 1.4, -23},
{12, 2, -2.3}};
float distances[10];
distancesf(points, distances);
for (i = 0; i < 10; i++)
{
printf("%.2f\n", distances[i]);
}
float MinDist = distances[0];
for (i = 1; i < 10; i++)
{
if (distances[i] < MinDist)
{
MinDist = distances[i];
}
}
MeanPointf(points, MeanPoint);
printf("Min Dist is :\t%.2f", MinDist);
printf("\nMean point is:\t");
for (i = 0; i < 3; i++)
{
printf("%.2f ", MeanPoint[i]);
}
printf("\nAverage distance to mean point is:\t%.2f\n", AvgDist(points, MeanPoint, MeanDist, PointNumber));
for (i = 0; i < 5; i++)
{
for (j = 0; j < 4; j++)
{
if (MeanDist[j] > MeanDist[j + 1])
{
swap = MeanDist[j];
MeanDist[j] = MeanDist[j + 1];
MeanDist[j + 1] = swap;
}
}
}
printf("from lowest to biggest distance to the mean point:\n");
for (i = 0; i < 5; i++)
{
printf("point %d:\t%.2f\n", PointNumber[i], MeanDist[i]);
}
}
void distancesf(float points[5][3], float distances[10])
{
int N = 0;
int k = 0;
int i, j, h;
float x = 0;
for (j = 0; j < 5; j++)
{
N++;
for (i = N; i < 5; i++)
{
for (h = 0; h < NoOfDimensions; h++)
{
x += pow((points[i][h] - points[j][h]), 2);
}
distances[k] = pow(x, 0.5);
k++;
}
}
}
void MeanPointf(float points[5][3], float MeanPoint[3])
{
int i;
float SumX = 0, SumY = 0, SumZ = 0;
for (i = 0; i < 5; i++)
{
SumX += points[i][0];
SumY += points[i][1];
SumZ += points[i][2];
}
MeanPoint[0] = (SumX / 5);
MeanPoint[1] = (SumY / 5);
MeanPoint[2] = (SumY / 5);
}
float AvgDist(float points[5][3], float MeanPoint[3], float MeanDist[5], int PointNumber[5])
{
float answer;
float SumDist = 0;
int i;
for (i = 0; i < 5; i++)
{
MeanDist[i] = pow((pow(MeanPoint[0] - points[i][0], 2) + pow(MeanPoint[1] - points[i][1], 2) + pow(MeanPoint[1] - points[i][2], 2)), 0.5);
SumDist += MeanDist[i];
PointNumber[i] = (i + 1);
}
answer = (SumDist / 5);
return answer;
}
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