Advertisement
Wojtekd

LAB4

Mar 17th, 2015
250
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.08 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <time.h>
  4. #include <limits.h>
  5. #include "main_lib.h"
  6.  
  7.  
  8. void wypisz_tablice(int** tab)
  9. {
  10.     int i,j;
  11.     for(i = 0;i < N; i++)
  12.     {
  13.         printf("\n");
  14.         for(j = 0; j < M; j++)
  15.         {
  16.             printf("%3d\t",tab[i][j]);
  17.         }
  18.     }
  19. }
  20. /* zad1 */
  21. int **inicjuj_tablice_wart_losowymi()
  22. {
  23.     int** tab;
  24.     int i,j;
  25.  
  26.     // alokacja pamieci
  27.     tab = (int**)malloc(N*sizeof(int*));
  28.     for(i = 0; i < N; i++)
  29.     {
  30.         tab[i] = (int*)malloc(M*sizeof(int));      
  31.     }  
  32.     // wypelnij losowymi
  33.     for(i = 0; i < N; i++)
  34.     {
  35.         for(j = 0; j < M; j++)
  36.         {
  37.             tab[i][j] = rand()% 201 - 100;
  38.         }      
  39.     }  
  40.     return tab;
  41. }
  42.  
  43. /* zad2 */
  44. void zwolnij_tablice(int **tab)
  45. {
  46.     int i,j;
  47.     for(i = 0; i < N; i++)
  48.     {
  49.         free(tab[i]);
  50.     }
  51.     free(tab);
  52. }
  53.  
  54. /* zad3 */
  55. struct element *inicjuj_lancuch(int elements)
  56. {
  57.     if(elements == 0) return NULL;
  58.    
  59.     struct element* start = (struct element*)malloc(sizeof(struct element));
  60.     start->value = rand()%201 - 100;
  61.     struct element* prev = start;
  62.    
  63.     int i;
  64.     for(i = 1; i < elements; i++)
  65.     {
  66.         struct element *newElement = (struct element*)malloc(sizeof(struct element));
  67.         newElement->value = rand()% 201 - 100;
  68.         newElement->next = NULL;
  69.        
  70.         prev->next = newElement;
  71.         prev = newElement;
  72.     }
  73.     printf("\n");
  74.     return start;
  75. }
  76.  
  77. /* zad4 */
  78. void zwolnij_lancuch(struct element* lancuch)
  79. {
  80.     struct element* current = lancuch;         
  81.     struct element* temp = NULL;
  82.            
  83.     while(current != NULL)
  84.     {
  85.         temp = current;
  86.         current = current->next;
  87.         free(temp);    
  88.     }
  89. }
  90. void wypisz_lancuch(struct element* lancuch)
  91. {
  92.     while(lancuch != NULL)
  93.     {
  94.         printf("%d\t",lancuch->value);
  95.         lancuch = lancuch->next;
  96.     }
  97. }
  98.  
  99. /* zad5 */
  100. int ile_ciagow_arytmetycznych(int **tab)
  101. {
  102.     int result = 0;
  103.     int a0, a1, r;     
  104.    
  105.     int i;
  106.     for(i = 0; i < 3; i++)
  107.     {
  108.         a0 = tab[i][0];
  109.         a1 = tab[i][1];
  110.         r = a1 - a0;
  111.         for(int j = 0; j < 2; j++)
  112.         {
  113.             if(tab[i][j+1] - tab[i][j] != r)
  114.             {
  115.                 break;
  116.             }
  117.         }
  118.         result++;
  119.     }
  120.     return result;
  121. }
  122.  
  123. /* zad6 */
  124. struct point znajdz_sume_minimalna(int **tab)
  125. {
  126.     struct point result;
  127.  
  128.     return result;
  129. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement