Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdlib.h>
- #include <stdio.h>
- #include <math.h>
- typedef struct
- {
- double x, y;
- }Punct2D;
- double distanta(Punct2D A, Punct2D B)
- {
- return sqrt(((A.x - B.x) * (A.x - B.x)) + (A.y - B.y)*(A.y - B.y));
- }
- void getMatrix(Punct2D *vec, double ***mat, int N)
- {
- (*mat) = (double**)malloc(sizeof(double*) * N);
- if(!(*mat))
- {
- printf("Eroare la alocare");
- exit(1);
- }
- for(int i = 0; i < N; ++i)
- {
- (*mat)[i] = (double*)malloc(sizeof(double)*N);
- if(!(*mat)[i])
- {
- printf("Eroare la alocare");
- exit(1);
- }
- }
- for(int i = 0; i < N; ++i)
- {
- for(int j = 0; j < N; ++j)
- {
- (*mat)[i][j] = distanta(vec[i], vec[j]);
- }
- }
- }
- int main()
- {
- int N;
- double **mat;
- Punct2D *vec;
- FILE *fi = fopen("input.txt", "rt");
- fscanf(fi, "%d", &N);
- vec = (Punct2D*)malloc(N * sizeof(Punct2D));
- if(!vec)
- {
- printf("Eroare la alocare");
- exit(1);
- }
- for(int i = 0; i < N; ++i)
- {
- fscanf(fi, "%lf%lf", &vec[i].x, &vec[i].y);
- }
- getMatrix(vec, &mat, N);
- FILE *fo = fopen("output.txt", "wt");
- for(int i = 0; i < N; ++i)
- {
- fprintf(fo, "\n");
- for(int j = 0; j < N; ++j)
- {
- fprintf(fo, "%lf ", mat[i][j]);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement