Sorting

I apologize if this is answered elsewhere, i was unable to answer my questions after searching. I have 3 distances in my final output that i need to be printed in ascending order. Do i need to set up an array? I'm not entirely sure how i might create on in this instance and then sort it.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44

#include <stdio.h>

# define TRUE 1
int main(){

float x1=0.00;
float x2=0.00;
float x3=0.00;
float y1=0.00;
float y2=0.00;
float y3=0.00;
double homex=0.00;
double homey=0.00;

printf("What are the x-y coordinates of the first location?\n");
scanf("%f %f", &x1, &y1);

printf("What are the x-y coordinates of the second location?\n");
scanf("%f %f", &x2, &y2);

printf("What are the x-y coordinates of the third location?\n");
scanf("%f %f", &x3, &y3);


homex= ((x1)+(x2)+(x3))/3; // centroid x-value
homey= ((y1)+(y2)+(y3))/3; // centroid y-values

printf("You should live at (%.2f, %.2f)", homex, homey); //location of centroid

double distance1=0;
double distance2=0;
double distance3=0;

distance1 = sqrt(pow((homex-x1),2)+pow((homey-y1),2));
distance2 = sqrt(pow((homex-x2),2)+pow((homey-y2),2));
distance3 = sqrt(pow((homex-x3),2)+pow((homey-y3),2));

printf("The corresponding distances are %.2f,%.2f and %.2f.", distance1, distance2, distance3); //need these values printed in ascending order

printf("\n\n");
    system("PAUSE");
    return 0;
}
I would set up an array or some other container (like a vector) and use std::sort().
Topic archived. No new replies allowed.