Advertisement
aimon1337

Untitled

Jan 25th, 2022
1,062
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.38 KB | None | 0 0
  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. #include <math.h>
  4. typedef struct
  5. {
  6.     double x, y;
  7. }Punct2D;
  8.  
  9. double distanta(Punct2D A, Punct2D B)
  10. {
  11.     return sqrt(((A.x - B.x) * (A.x - B.x)) + (A.y - B.y)*(A.y - B.y));
  12. }
  13.  
  14. void getMatrix(Punct2D *vec, double ***mat, int N)
  15. {
  16.     (*mat) = (double**)malloc(sizeof(double*) * N);
  17.     if(!(*mat))
  18.     {
  19.         printf("Eroare la alocare");
  20.         exit(1);
  21.     }
  22.     for(int i = 0; i < N; ++i)
  23.     {
  24.         (*mat)[i] = (double*)malloc(sizeof(double)*N);
  25.         if(!(*mat)[i])
  26.         {
  27.             printf("Eroare la alocare");
  28.             exit(1);
  29.         }
  30.     }
  31.     for(int i = 0; i < N; ++i)
  32.     {
  33.         for(int j = 0; j < N; ++j)
  34.         {
  35.             (*mat)[i][j] = distanta(vec[i], vec[j]);
  36.         }
  37.     }
  38. }
  39.  
  40. int main()
  41. {
  42.     int N;
  43.     double **mat;
  44.     Punct2D *vec;
  45.     FILE *fi = fopen("input.txt", "rt");
  46.     fscanf(fi, "%d", &N);
  47.     vec = (Punct2D*)malloc(N * sizeof(Punct2D));
  48.     if(!vec)
  49.     {
  50.         printf("Eroare la alocare");
  51.         exit(1);
  52.     }
  53.     for(int i = 0; i < N; ++i)
  54.     {
  55.         fscanf(fi, "%lf%lf", &vec[i].x, &vec[i].y);
  56.     }
  57.     getMatrix(vec, &mat, N);
  58.     FILE *fo = fopen("output.txt", "wt");
  59.     for(int i = 0; i < N; ++i)
  60.     {
  61.         fprintf(fo, "\n");
  62.         for(int j = 0; j < N; ++j)
  63.         {
  64.             fprintf(fo, "%lf ", mat[i][j]);
  65.         }
  66.     }
  67. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement