Advertisement
tsounakis

mama mama thelw kaka

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