[c] Σημεία σε κύβο 1x1

Viewer

copydownloadembedprintName: Σημεία σε κύβο 1x1
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <math.h>
  4. #include <time.h>
  5.  
  6. struct point {
  7.     double x;
  8.     double y;
  9.     double z;
  10. };
  11.  
  12. struct point input(){
  13.     struct point p;
  14.     p.x = (double)rand() / (double) RAND_MAX;
  15.         p.y = (double)rand() / (double) RAND_MAX;
  16.         p.z = (double)rand() / (double) RAND_MAX;
  17.     return p;
  18. }
  19.  
  20. double dist(struct point p1, struct point p2){
  21.     double d;
  22.     d = sqrt(pow(p1.x-p2.x,2)+pow(p1.y-p2.y,2)+pow(p1.z-p2.z,2));
  23.     return d;
  24. }
  25.  
  26. int main(){
  27.     srand(time(0));
  28.     int n, i, j,c1,c2,tests,i1,i2,i3,j1,j2,j3,k;
  29.     double max, s, mv, disTemp, firstMax, finalMax, firstMv, finalMv, difmax,amax,bmax,difmv,amv,bmv;
  30.     printf("Give number of points: ");
  31.     scanf("%d",&n);
  32.     struct point cube[n];
  33.     printf("Give number of tests: ");
  34.     scanf("%d",&tests);
  35.     double maxMatr[tests], mvMatr[tests];
  36.     for (=0; k<tests;k++){
  37.         for (= 0; i < n; i++)
  38.     {
  39.         cube[i] = input();
  40.     }
  41.     max = dist(cube[0],cube [0]);
  42.     s = 0;
  43.     c1 =0;
  44.     c2 = 0;
  45.     for (i=0; i<n;i++){
  46.         for (j=0; j<n;j++){
  47.             disTemp = dist(cube[i],cube[j]);
  48.             s = s+ disTemp;
  49.             if (disTemp>max)
  50.             {
  51.                 max = disTemp;
  52.                 c1 = i;
  53.                 c2 = j;
  54.             }
  55.             
  56.         }
  57.     }
  58.     mv = s/n;
  59.         maxMatr[k] = max;
  60.         mvMatr[k] = mv;
  61.     }
  62.     firstMax = maxMatr[0];
  63.     finalMax = maxMatr[0];
  64.     firstMv = mvMatr[0];
  65.     finalMv = mvMatr[0];
  66.  
  67.     for (i=0;i<tests;i++){
  68.         if (maxMatr[i]<firstMax){
  69.             firstMax = maxMatr[i];
  70.         }
  71.         else if (maxMatr[i]>finalMax){
  72.             finalMax = maxMatr[i];
  73.         }
  74.         if (mvMatr[i]<firstMv){
  75.             firstMv = mvMatr[i];
  76.         }
  77.         else if (mvMatr[i]>finalMv){
  78.             finalMv = mvMatr[i];
  79.         }
  80.     }
  81.     difmax = finalMax-firstMax;
  82.     amax = firstMax+difmax/3;
  83.     bmax = finalMax-difmax/3;
  84.     difmv = finalMv-firstMv;
  85.     amv = firstMv+difmv/3;
  86.     bmv = finalMv-difmv/3;
  87.     i1=0;
  88.     i2=0;
  89.     i3=0;
  90.     j1=0;
  91.     j2=0;
  92.     j3=0;
  93.     for (k=0;k<tests;k++){
  94.         if (maxMatr[k]<amax){
  95.             i1 =i1+1;
  96.         }
  97.         else if (maxMatr[k]<bmax){
  98.             i2 = i2+1;
  99.         }
  100.         else {
  101.             i3 = i3+1;
  102.         }
  103.         if (mvMatr[k]<amv){
  104.             j1 =j1+1;
  105.         }
  106.         else if (mvMatr[k]<bmv){
  107.             j2 = j2+1;
  108.         }
  109.         else {
  110.             j3 = j3+1;
  111.         }
  112.     }
  113.     printf("Maximum______\n");
  114.     printf("In [%lf,%lf]:%d \n",firstMax,amax,i1);
  115.     printf("In [%lf,%lf]:%d \n",amax,bmax,i2);
  116.     printf("In [%lf,%lf]:%d \n",bmax,finalMax,i3);
  117.     printf("Mean value_______\n");
  118.     printf("In [%lf,%lf]:%d \n",firstMv,amv,j1);
  119.     printf("In [%lf,%lf]:%d \n",amv,bmv,j2);
  120.     printf("In [%lf,%lf]:%d \n",bmv,finalMv,j3);
  121. }

Editor

You can edit this paste and save as new:


File Description
  • Σημεία σε κύβο 1x1
  • Paste Code
  • 23 Jan-2022
  • 2.86 Kb
You can Share it: