0
created in class this program that calculates the distance between two points and then gives the option of 3 different paths: ABC, BAC or CAB.
I would like tips to optimize my code, ideas on how to improve it and also tips on new features...
follows the code:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int main(int argc, char *argv[])
{
float ax, bx,cx, ay, by, cy;
float AB, BC, AC;
float distancia;
int rota;
printf("Digite o valor de Ax\n");
scanf("%f", &ax);
printf("Digite o valor de Ay\n");
scanf("%f", &ay);
printf("Digite o valor de Bx\n");
scanf("%f", &bx);
printf("Digite o valor de By\n");
scanf("%f", &by);
printf("Digite o valor de Cx\n");
scanf("%f", &cx);
printf("Digite o valor de Cy\n");
scanf("%f", &cy);
AB=sqrt(pow((bx-ax),2)+pow((by-ay),2));
printf("A distancia entre os pontos A e B eh %f\n", AB);
AC=sqrt(pow((cx-ax),2)+pow((cy-ay),2));
printf("A distancia entre os pontos A e C eh %f\n", AC);
BC=sqrt(pow((cx-bx),2)+pow((cy-by),2));
printf("A distancia entre os pontos B e C eh %f\n\n", BC);
printf("Escolha uma rota: ABC = 1, BAC = 2 ou CAB = 3\n");
scanf("%d", &rota);
switch (rota)
{
case 1:
distancia = AB + BC;
printf("A distancia entre os pontos ABC eh %f\n", distancia);
break;
case 2:
distancia = AB + BC;
printf("A distancia entre os pontos BAC eh %f\n", distancia);
break;
case 3:
distancia = AB + BC;
printf("A distancia entre os pontos CAB eh %f\n", distancia);
break;
default :
printf("ROTA INVALIDA!!!");
}
system("pause");
return 0;
}
+1 for suggesting breaking code into subroutines and using data structures. I would only recommend using
.x
and.y
instead of.xcoord
and.ycoord
, which is redundant, and pass the parameters todistance()
by reference, rather than by value.– Wtrmute
Thanks for the tips, Anderson!
– EDU