Advertisement
lancernik

AISD2

Apr 23rd, 2019
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.01 KB | None | 0 0
  1. FUNCKJEEE
  2.  
  3.  
  4. #include "header.h"
  5.  
  6.  
  7. /*Tworzymy tablice*/
  8.  
  9. int *create()
  10. {
  11. int *myarray;
  12. myarray = malloc(SIZE *sizeof(int));
  13. if (myarray == NULL){
  14. perror("malloc");
  15. exit(1);
  16. }
  17. return myarray;
  18. }
  19.  
  20.  
  21.  
  22. void init(int* myarray, int n, int size) /* inicjalizowanie kazdego elementu tablicy na podana wartosc*/
  23. {
  24. int i;
  25. for(i=0; i< size; i++)
  26. {
  27. myarray[i] = n;
  28. }
  29.  
  30. }
  31.  
  32.  
  33.  
  34. int* enlargee(int* myarray)
  35. {
  36. int* wsk;
  37. wsk = malloc((SIZE + M ) * sizeof(int)); /* rezerwacja miejsca w pamieci dla powiekszonej tablicy*/
  38. if (wsk == NULL){
  39. perror("malloc");
  40. exit(0);
  41. }
  42. int i;
  43. for(i=0; i< (SIZE +M); i++)
  44. {
  45. if( i<SIZE)
  46. {
  47. wsk[i]= myarray[i]; /*zachowanie wartosci tablicy o rozmiarze SIZE*/
  48. }
  49. else
  50. {
  51. wsk[i]=0; /*komorki dodatkowe zdefiniowane przez M wypelnij zerem*/
  52. }
  53. }
  54. return wsk;
  55. }
  56.  
  57.  
  58.  
  59. void setvalue(int* myarray, int n, int index, int value)/*n-romziar tablicy*/
  60. {
  61. if(index < n)
  62. {
  63. myarray[index]=value;
  64. }
  65.  
  66. else
  67. {
  68. printf("Indeks wiekszy niz rozmiar tablicy");
  69. }
  70. }
  71.  
  72.  
  73. int readvalue(int* myarray , int n, int index)
  74. {
  75. if(index< n)
  76. {
  77. return myarray[index];
  78. }
  79. else
  80. {
  81. printf("Indeks wiekszy niz rozmiar tablicy");
  82. return -1;
  83. }
  84. }
  85.  
  86.  
  87. void removee(int* myarray)
  88. {
  89. free(myarray);
  90.  
  91. }
  92.  
  93.  
  94.  
  95.  
  96. void show(int* myarray, int n)
  97. {
  98. int i;
  99. for(i=0; i< n; i++)
  100. {
  101. printf("myarray[%d]=% d\n", i,myarray[i]);
  102. }
  103. }
  104.  
  105.  
  106.  
  107.  
  108.  
  109.  
  110.  
  111.  
  112.  
  113.  
  114.  
  115.  
  116.  
  117. HEADERRRRRRRR
  118.  
  119. #include<stdio.h>
  120. #include<stdlib.h>
  121. #include <time.h>
  122. #define SIZE 5
  123. #define M 2
  124.  
  125. int* create();
  126. void init(int* , int , int);
  127. int* enlargee(int* );
  128. void setvalue(int* , int , int , int);
  129. int readvalue(int* ,int , int );
  130. void removee(int* );
  131. void show(int* , int n);
  132.  
  133.  
  134.  
  135.  
  136. MAINNNNNN
  137.  
  138.  
  139.  
  140. #include "header.h"
  141. #include "function.c"
  142. int main()
  143. {
  144. int* myarray;
  145. myarray = create();
  146. printf("Adres poczatku tablicy wynosi %p \n",create());
  147.  
  148.  
  149. /* wypelnianie utworzonej tablicy wartoscia 2*/
  150. init(myarray, 2, SIZE);
  151. show(myarray, SIZE);
  152.  
  153. setvalue(myarray,SIZE, 2,5); /* ustawienie 3 elementu tablicy SIZE na wartosc 5*/
  154. show(myarray, SIZE);
  155.  
  156. printf("%d element tablicy wynosi %d\n", 2+1, readvalue(myarray, (SIZE+M), 2));
  157.  
  158. /*powiekszanie tablicy myarray*/
  159. myarray = enlargee(myarray);
  160. printf("Adres poczatku powiekszonej tablicy wynosi %p\n", enlargee(myarray));
  161.  
  162.  
  163. setvalue(myarray, (SIZE+M), 3, 10);
  164. show(myarray, (SIZE+M));
  165.  
  166.  
  167. init(myarray, 3, (SIZE+M)); /* ustawienie powiekszonej tablicy na wartosc 3*/
  168. show(myarray, (SIZE+M));
  169.  
  170. setvalue(myarray, (SIZE+M), 4, 9); /* ustawia 5 element na wartosc 9*/
  171. printf("%d element tablicy wynosi %d\n", 4 +1 , readvalue(myarray, (SIZE+M), 4)); /* czyta 5 element z tablicy*/
  172.  
  173. removee(myarray);
  174. return 0;
  175. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement