Advertisement
tsounakis

3.3 WORKING GOOD

Mar 23rd, 2020
299
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 6.19 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #define N 10
  4. #define M 20
  5.  
  6. const float right = -5.0, left = 4.0, up = 2.0, down = 3.0;
  7.  
  8. void occurence(float t[N][M]);
  9. void createL(float t[N][M]);
  10. void createU(float t[N][M]);
  11. void createR(float t[N][M]);
  12. void createD(float t[N][M]);
  13. void calcCorners(float t[N][M]);
  14. void printarray(float*);
  15. void initialize(float t[N][M]);
  16. void denormalize(float t[N][M]);
  17. void loadTemp(float tTemp[N][M], float*);
  18. void normalize(float t[N][M]);
  19. float next(int, int, float t[N][M]);
  20. int round(float);
  21.  
  22. int main() {
  23.     float t[N][M], tNew[N][M], tTemp[N][M];
  24.     float* ptr;
  25.     float* ptrNew;
  26.     float* ptrTemp;
  27.     int i, j, k;
  28.     int time;
  29.    
  30.     ptr = &t[0][0];
  31.     ptrNew = &tNew[0][0];
  32.    
  33.     initialize(t);
  34.     normalize(t);
  35.     printarray(ptr);
  36.     denormalize(t);
  37.    
  38.     while(1)
  39.     {
  40.         ptrTemp = &t[0][0];
  41.         printf("Enter an integer for the time: ");
  42.         scanf("%d", &time);
  43.        
  44.         if (time!=0)
  45.         {
  46.             loadTemp(tTemp, ptrTemp);
  47.             for (k=0; k<time; k++)
  48.             {
  49.                 for (i=0; i<N; i++)
  50.                 {
  51.                     if (i != 0 && i != N - 1)
  52.                     {
  53.                         for (j=0; j<M; j++)
  54.                         {
  55.                             if (j != 0 && j != M - 1)
  56.                             {
  57.                                 tNew[i][j] = next(i, j, tTemp);
  58.                             }
  59.                         }
  60.                     }
  61.                 }
  62.                 createL(tNew);
  63.                 createR(tNew);
  64.                 createD(tNew);
  65.                 createU(tNew);
  66.                 calcCorners(tNew);
  67.                 ptrTemp = &tNew[0][0];
  68.                 loadTemp(tTemp, ptrTemp);
  69.                 /*normalize(tNew)
  70.                 printarray(&tTemp[0][0]);*/
  71.             }
  72.         }
  73.         else
  74.         {
  75.             return 0;
  76.         }
  77.         normalize(tNew);
  78.         printarray(ptrNew);
  79.         occurence(tNew);
  80.     }
  81.     system("pause");
  82.    
  83.     return 0;
  84. }
  85.  
  86. /* i am tired */
  87.  
  88. void occurence(float t[N][M])
  89. {
  90.     int count, i, j, l, k;
  91.     float tOcc[N][M], hasAppeared[10];
  92.     for (i=0; i<10; i++)
  93.     {
  94.         hasAppeared[i] = 0.0;
  95.     }
  96.     for (i=0; i<N; i++)
  97.     {
  98.         for (j=0; j<M; j++)
  99.         {
  100.             count = 0;
  101.              for (k=0; k<N; k++)
  102.             {
  103.                 for (l=0; l<M; l++)
  104.                 {
  105.                     if (t[i][j] == t[k][l])
  106.                     {
  107.                         count++;
  108.                     }
  109.                 }
  110.             }
  111.             tOcc[i][j] = count;
  112.             /*printf("%g appears %g times.\n", t[i][j], tOcc[i][j]);*/
  113.         }
  114.     }
  115.     printf("Temperature:\tOccurency:\tStars:\n");
  116.     for (i=0; i<N; i++)
  117.     {
  118.         for (j=0; j<M; j++)
  119.         {
  120.             if (hasAppeared[(int)t[i][j]] != 1)
  121.             {
  122.                 printf("%g\t\t(%g)\t\t", t[i][j], tOcc[i][j]);     
  123.                 for (k=0; k<tOcc[i][j]; k++)
  124.                 {
  125.                     putchar('*');
  126.                 }
  127.                 putchar('\n');
  128.                 hasAppeared[(int)t[i][j]] = 1;
  129.             }
  130.         }
  131.     }/*
  132.     for (i=0; i<10; i++)
  133.     {
  134.         printf("%d (%g)\t", i, hasAppeared[i]);
  135.         for (j=0; j<(int)hasAppeared[i]; j++)
  136.         {
  137.             printf("*");
  138.         }
  139.         putchar('\n');
  140.     }*/
  141.     return;
  142. }
  143.  
  144. int round(float num)
  145. {
  146.     return num < 0 ? num - 0.5 : num + 0.5;
  147. }
  148.  
  149. void denormalize(float t[N][M])
  150. {
  151.     initialize(t);
  152.     return;
  153. }
  154.  
  155. void normalize(float t[N][M])
  156. {
  157.     int i, j, k;
  158.     float min = t[0][0], max = t[0][0], Dx;
  159.     for (i=0; i<N; i++)
  160.     {
  161.         for (j=0; j<M; j++)
  162.         {
  163.             if (t[i][j] < min)
  164.             {
  165.                 min = t[i][j];
  166.             }
  167.         }
  168.     }
  169.     for (i=0; i<N; i++)
  170.     {
  171.         for (j=0; j<M; j++)
  172.         {
  173.             if (t[i][j] > max)
  174.             {
  175.                 max = t[i][j];
  176.             }
  177.         }
  178.     }
  179.     Dx = (max - min) / 10;/*
  180.     printf("Dx = %g\nMax = %g and Min = %g\n\n", Dx, max, min);*/
  181.     /*
  182.     for (k=0; k<10; k++)
  183.     {
  184.         printf("[%g, %g) ==> %dth Area\n", k*Dx + min, (k+1)*Dx + min, k);
  185.     }*/
  186.    
  187.    
  188.     for (i=0; i<N; i++)
  189.     {
  190.         for (j=0; j<M; j++)
  191.         {
  192.             for (k=0; k<10; k++)
  193.             {
  194.                 if (t[i][j] == max)
  195.                 {
  196.                     t[i][j] = 9.0;
  197.                 }
  198.                 if (t[i][j] >= (k*Dx + min) && t[i][j] < ((k + 1)*Dx + min))
  199.                 {
  200.                     t[i][j] = k;
  201.                     break;
  202.                 }
  203.             }
  204.         }
  205.     }
  206.     return;
  207. }
  208.  
  209. void loadTemp(float tTemp[N][M], float* ptrTemp)
  210. {
  211.     int i, j;
  212.     for (i=0; i<N; i++)
  213.     {
  214.         for (j=0; j<M; j++)
  215.         {
  216.             tTemp[i][j] = *(ptrTemp + M*i + j);
  217.         }
  218.     }
  219.     return;
  220. }
  221.  
  222. void calcCorners(float t[N][M])
  223. {
  224.     float ur, ul, dr, dl;
  225.     ur = ( right + up ) / 2;
  226.     dr = ( right + down ) / 2;
  227.     ul = ( left + up ) / 2;
  228.     dl = ( left + down ) / 2;
  229.     t[0][0] = ul;
  230.     t[0][M-1] = ur;
  231.     t[N-1][0] = dl;
  232.     t[N-1][M-1] = dr;
  233.     return;
  234. }
  235.  
  236. void createR(float t[N][M])
  237. {
  238.     int i;
  239.     for (i=0; i<N; i++)
  240.     {
  241.         t[i][M-1] = right;
  242.     }
  243.     return;
  244. }
  245.  
  246. void createL(float t[N][M])
  247. {
  248.     int i;
  249.     for (i=0; i<N; i++)
  250.     {
  251.         t[i][0] = left;
  252.     }
  253.     return;
  254. }
  255.  
  256. void createD(float t[N][M])
  257. {
  258.     int i;
  259.     for (i=0; i<M; i++)
  260.     {
  261.         t[N-1][i] = down;
  262.     }
  263.     return;
  264. }
  265.  
  266. void createU(float t[N][M])
  267. {
  268.     int i;
  269.     for (i=0; i<M; i++)
  270.     {
  271.         t[0][i] = up;
  272.     }
  273.     return;
  274. }
  275.  
  276. void initialize(float t[N][M])
  277. {
  278.     int i, j;
  279.     printf("Time: 0\n");
  280.     for (i=0; i<N; i++)
  281.     {
  282.         for (j=0; j<M; j++)
  283.         {
  284.             t[i][j] = 1.0;
  285.         }
  286.     }
  287.     createL(t);
  288.     createR(t);
  289.     createD(t);
  290.     createU(t);
  291.     calcCorners(t);
  292.     return;
  293. }
  294.  
  295. void printarray(float* pointer)
  296. {
  297.     int i, j;
  298.     for (i=0; i<N; i++)
  299.     {
  300.         for (j=0; j<M; j++)
  301.         {
  302.             printf("%.2f\t", *(pointer + M*i + j));
  303.         }
  304.         putchar('\n');
  305.     }
  306.     printf("\n------------------------------------------------------\n");
  307.     return;
  308. }
  309.  
  310. float next(int i, int j, float t[N][M])
  311. {
  312.     float tNewElement;
  313.     tNewElement = 0.1 * (t[i-1][j-1] + t[i-1][j] + t[i-1][j+1] + t[i][j-1] + 2 * t[i][j] + t[i][j+1] + t[i+1][j-1] + t[i+1][j] + t[i+1][j+1]);
  314.     return tNewElement;
  315. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement