Advertisement
2607

struct vec

Sep 30th, 2021
985
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.99 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <math.h>
  4.  
  5. struct vect {
  6.     int x;
  7.     int y;
  8.     int z;
  9. };
  10.  
  11. int main() {
  12.     int n;
  13.     char c;
  14.     if (scanf("%d%c", &n, &c) == 2 && c == '\n') {
  15.         int f = 1;
  16.         //int x, y, z;
  17.         struct vect *vects = malloc(n * sizeof(int));
  18.         //int *arr = malloc(n * sizeof(int));
  19.         char s;
  20.         for (int i = 0; i < n; i++) {
  21.             if (scanf("%d %d %d%c", &vects[i].x, &vects[i].y, &vects[i].z, &s) == 4 && s == '\n') {
  22.                 continue;
  23.             } else {
  24.                 f = 0;
  25.                 printf("n/a");
  26.                 break;
  27.             }
  28.         }
  29.         if (f == 1)
  30.             for (int i = 0; i < n; i++) {
  31.                 printf("%d %d %d ", vects[i].x, vects[i].y, vects[i].z);
  32.                 printf("%lf\n", sqrt(vects[i].x*vects[i].x+ vects[i].y*vects[i].y + vects[i].z*vects[i].z));
  33.             }
  34.         free(vects);
  35.     } else {
  36.         printf("n/a");
  37.     }
  38.     return 0;
  39. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement