Advertisement
tsounakis

3.3 TELOS

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