Hyluss

Macierz rzadka

Jul 2nd, 2016
516
0
Never
1
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.77 KB | None | 0 0
  1. //znalezione w sieci
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4.  
  5. #define SIZE_X 4
  6. #define SIZE_Y 3
  7.  
  8.  
  9. struct Point
  10. {
  11.     double value;
  12.     int x;
  13.     int y;
  14.     struct Point *next;
  15. };
  16.  
  17.  
  18. void addElement(struct Point **headPointer, double matrixValue, int matrixX, int matrixY)
  19. {
  20.     struct Point *pointer;
  21.     pointer = *headPointer;
  22.  
  23.     if (matrixValue != 0)
  24.     {
  25.         if(pointer == NULL)
  26.         {
  27.             pointer = (struct Point*)malloc(sizeof(struct Point));
  28.             pointer->value = matrixValue;
  29.             pointer->x = matrixX;
  30.             pointer->y = matrixY;
  31.             pointer->next = NULL;
  32.             *headPointer = pointer;
  33.         }
  34.         else
  35.         {
  36.             while (pointer->next != NULL)
  37.             {
  38.                 pointer = pointer->next;
  39.             }
  40.             pointer->next = (struct Point*)malloc(sizeof(struct Point));
  41.             pointer = pointer->next;
  42.             pointer->value = matrixValue;
  43.             pointer->x = matrixX;
  44.             pointer->y = matrixY;
  45.             pointer->next = NULL;
  46.         }
  47.     }
  48.  
  49.  
  50. }
  51.  
  52.  
  53. void showAll(struct Point *head)
  54. {
  55.     if (head != NULL)
  56.     {
  57.         printf("%lf (%d,%d)\n", head->value, head->x, head->y);
  58.  
  59.         while(head->next != NULL)
  60.         {
  61.             head = head->next;
  62.             printf("%lf (%d,%d)\n", head->value, head->x, head->y);
  63.         }
  64.     }
  65. }
  66.  
  67.  
  68. void findMaxMin(struct Point *head, struct Point **max, struct Point **min)
  69. {
  70.     if (head != NULL)
  71.     {
  72.         *max = head;
  73.         *min = head;
  74.  
  75.         while (head->next != NULL)
  76.         {
  77.             head = head->next;
  78.             if (head->value > (*max)->value)
  79.             {
  80.                 *max = head;
  81.             }
  82.             if (head->value < (*min)->value)
  83.             {
  84.                 *min = head;
  85.             }
  86.         }
  87.     }
  88. }
  89.  
  90.  
  91. int main()
  92. {
  93.     int i, j;
  94.     double matrix[SIZE_X][SIZE_Y];
  95.     struct Point *head, *maxElement, *minElement;
  96.  
  97.     head = NULL;
  98.     maxElement = NULL;
  99.     minElement = NULL;
  100.  
  101.     // inicjalizacja macierzy
  102.     for(i = 0; i < SIZE_X; i++)
  103.     {
  104.         printf("Wprowadz elementy %d wiersza (%d):", (i+1), SIZE_Y);
  105.         for(j = 0; j < SIZE_Y; j++)
  106.         {
  107.             scanf("%lf", &matrix[i][j]);
  108.         }
  109.     }
  110.  
  111.     // dodawanie elemntow macierzy rzadkiej
  112.     for(i = 0; i < SIZE_X; i++)
  113.     {
  114.         for(j = 0; j < SIZE_Y; j++)
  115.         {
  116.             addElement(&head, matrix[i][j], (i+1), (j+1));
  117.         }
  118.     }
  119.  
  120.     showAll(head);
  121.  
  122.     findMaxMin(head, &maxElement, &minElement);
  123.     printf("\n\nMin: %lf (%d,%d)\n", minElement->value, minElement->x, minElement->y);
  124.     printf("Max: %lf (%d,%d)\n", maxElement->value, maxElement->x, maxElement->y);
  125.  
  126.  
  127.     return 0;
  128. }
Advertisement
Comments
  • User was banned
Add Comment
Please, Sign In to add comment