fork download
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <math.h>
  4.  
  5. void distCalc(double *a, double *b, double *c) {
  6. *c = sqrt(pow(b[0] - a[0], 2) +
  7. pow(b[1] - a[1], 2) +
  8. pow(b[2] - a[2], 2));
  9. }
  10.  
  11. void usage(void) {
  12. printf("Enter X1, X2 and X3 coordinates for Point A and B.\n");
  13. printf("Example: pointdist 1 2 3 1 2 3\n");
  14. exit(1);
  15. }
  16.  
  17. void errorMessage(int a, int *b) {
  18. if (a == 0 && *b < 7) {
  19. printf("Too few arguments!\n");
  20. usage();
  21. }
  22.  
  23. if (a == 0 && *b > 7) {
  24. printf("Too many arguments!\n");
  25. usage();
  26. }
  27.  
  28. if (a > 3 && *b == 7) {
  29. printf("Invalid X%i coordinate for point B!\n", a - 3);
  30. usage();
  31. }
  32.  
  33. if (a > 0 && a < 4 && *b == 7) {
  34. printf("Invalid X%i coordinate for point A!\n", a);
  35. usage();
  36. }
  37. }
  38.  
  39. int main(int argc, char *argv[]) {
  40. int i = 0;
  41. double dist = 0;
  42. double vectorA[3] = {0};
  43. double vectorB[3] = {0};
  44.  
  45. errorMessage(&i, &argc);
  46.  
  47. for (i = 0; i < 3; i++) {
  48. if (sscanf(argv[i + 1], "%lf", &vectorA[i]) != 1) {
  49. errorMessage(i + 1, &argc);
  50. }
  51.  
  52. if (sscanf(argv[i + 4], "%lf", &vectorB[i]) != 1) {
  53. errorMessage(i + 4, &argc);
  54. }
  55. }
  56.  
  57. distCalc(vectorA, vectorB, &dist);
  58.  
  59. printf("%.2lf\n", dist);
  60.  
  61. return 0;
  62. }
  63.  
Runtime error #stdin #stdout 0s 9288KB
stdin
Standard input is empty
stdout
Standard output is empty