Guest User

Untitled

a guest
Feb 18th, 2018
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.01 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <math.h>
  4. #include <string.h>
  5.  
  6. #define WITHHEXDUMP 0
  7.  
  8. // MIN A
  9. double *allocate_double(long len);
  10. void free_double(double *mem);
  11. void print(double *mem, long len);
  12.  
  13. // MIN B
  14. double *double_size(double *zahl, long old_size);
  15. long sub(double *a, long size1, double *b, long size2,
  16. double *c, long size3);
  17.  
  18. void clearstdin();
  19.  
  20.  
  21. void clearstdin()
  22. {
  23. while(getchar()!='\n');
  24. }
  25.  
  26. double *allocate_double(long len)
  27. {
  28. long size_bytes=len*sizeof(double);
  29. double *adrptr=0;
  30. printf("*** ALLOCATE_DOUBLE ***\n");
  31. printf("ALLOCATION OF %ld BYTES FOR %ld DOUBLES:\n",
  32. size_bytes, len);
  33. if(!(adrptr=malloc(size_bytes)))
  34. {
  35. printf("MALLOC ERROR OR OUT OF MEMORY.\n");
  36. printf("BUY DISCOUNTED RAM CHEAP: 1-800-HYNIX.\n");
  37. exit(-1);
  38. }
  39. printf("ALLOCATION DONE AT %p\n",adrptr);
  40. return adrptr;
  41. }
  42.  
  43. void free_double(double *mem)
  44. {
  45. printf("*** FREE_DOUBLE ***\n");
  46. if(!mem)
  47. {
  48. printf("ATTEMPT TO FREE A NULL POINTER.\n");
  49. exit(-1);
  50. }
  51. free(mem);
  52. printf("FREED MEMORY AT %p\n",mem);
  53. }
  54.  
  55.  
  56.  
  57. void print(double *mem, long len)
  58. {
  59. int i;
  60. unsigned char *byteptr=(unsigned char*) mem;
  61. printf("*** PRINT ***\n");
  62. printf("ACCESSING MEMORY AT %p\n",mem);
  63. for(i=0;i<len;i++)
  64. printf("%20lf%c", mem[i], (i+1)%3?' ':'\n');
  65. putchar('\n');
  66.  
  67. #if WITHHEXDUMP
  68.  
  69. // "welche die darin enthaltenen Zeichen ausgibt" ?!!?!
  70. // OK, hier kommt der HEX-Dump:
  71. // Ich glaube nicht, dass es ernst gemeint ist sondern
  72. // ein Fehler in der Angabe... Aber ein HEX-Dump ist
  73. // nuetzlich um zu sehen, wie es im Speicher aussieht
  74. // und die Adressen.
  75. // Diesen Teil kann man also ziemlich sicher WEGLASSEN:
  76.  
  77. for(i=0;i<len*sizeof(double);i++)
  78. {
  79. if(!(i%16))
  80. printf("\n%p:",byteptr+i);
  81. printf("%02X ", byteptr[i]);
  82. }
  83. putchar('\n');
  84. #endif
  85.  
  86. }
  87.  
  88.  
  89.  
  90. double *double_size(double *zahl, long old_size)
  91. {
  92. long size_bytes=old_size*2*sizeof(double);
  93. double *adrptr=0;
  94. printf("*** DOUBLE_SIZE ***\n");
  95. printf("REALLOCATION OF %ld BYTES FOR %ld DOUBLES\n",size_bytes,old_size*2);
  96. if(!(adrptr=realloc(zahl, size_bytes)))
  97. printf("REALLOCATION FAILED.\n");
  98. else
  99. printf("NEW ADRESS %p\n", adrptr);
  100. return adrptr;
  101. //Warum wird hier nicht explizit return 0 gemacht? Weil realloc ja bereits
  102. //0 zurueckliefert und sich um das sowieso im Beispiel die aufrufende
  103. //Fkt kuemmert, daher kommt nur eine Fehlermeldung.
  104. }
  105.  
  106. // Ergibt mit double's irgendwie keinen Sinn, sie "numerisch" (Ziffer fuer Ziffer) zu subtrahieren?
  107. long sub(double *a, long size1, double *b, long size2, double *c, long size3)
  108. {
  109. printf("*** SUB ***\n");
  110. printf("ACCESSING A AT %p\n",a);
  111. printf("ACCESSING B AT %p\n",b);
  112. printf("ACCESSING C AT %p\n",c);
  113. if(size2>size1)
  114. {
  115. printf("CANNOT SUBTRACT LARGER FROM SMALLER NUMBER\n");
  116. return 0;
  117. }
  118. if(size3<size1)
  119. {
  120. printf("NOT ENAUGH ROOM FOR RESULT.\n");
  121. return 0;
  122. }
  123. if(!a || !b || !c)
  124. {
  125. printf("MEMORY BUFFER(S) NOT ALLOCATED.\n");
  126. return(0);
  127. }
  128. memset(c, 0, size3*sizeof(double));
  129. int i, carry=0;
  130.  
  131. //Anm: Eigentlich heisst es korrekt "borrow" und nicht "carry" beim Subtr.
  132. for(i=1;i<=size1;i++)
  133. {
  134. c[size3-i]=a[size1-i]-carry; //Ein ev. Carry aus dem vorherigen Schritt abziehn
  135. if(!(i>size2)) //Solange es Zahlen zum subtrahieren gibt
  136. c[size3-i]-=b[size2-i]; //diese auch abziehen
  137. carry=0; //Carry null setzen
  138. if(c[size3-i] < 0) //Wenn etwas negatives rauskommt:
  139. {
  140. c[size3-i]=10+c[size3-i]; // Wrap-Around um 10
  141. carry=1; //Carry setzen
  142. }
  143. }
  144. return 1;
  145. }
  146.  
  147.  
  148. int main()
  149. {
  150. int i;
  151. int num;
  152. int size1, size2, size3;
  153. double *new;
  154. double *buffer=allocate_double(20);
  155. double *a, *b, *c;
  156. sranddev();
  157. for(i=0;i<20;i++)
  158. buffer[i]=sin(rand())*rand();
  159. print(buffer,20);
  160. printf("PRESS RETURN\n");
  161. clearstdin(); // WAIT KEY
  162. new=double_size(buffer,20);
  163. if(!new)
  164. {
  165. printf("COULDN'T DOUBLE BUFFER SIZE...\n"); //Realloc ging nicht
  166. free_double(buffer);
  167. buffer=0;
  168. }
  169. else
  170. {
  171. buffer=new; //Realloc ging, dann noch paar konstante Werte als Beweis
  172. printf("ADDING SOME CONSTANT VALUES...\n"); //reinschreiben und diese ausgeben.
  173. for(i=20;i<40;i++)
  174. buffer[i]=1234;
  175. print(buffer,40);
  176. free_double(buffer);
  177. buffer = new = 0;
  178. }
  179. printf("PRESS RETURN\n");
  180. clearstdin(); // WAIT KEY
  181.  
  182. // Groessen fuer 3 Arrays abfragen und diese dann allocen
  183. // Diese Arrays werden nachher zum subtrahieren verwendet
  184. // Deswegen sollte B kleiner als A sein und C mindestens
  185. // so gross wie A (z.b. A=10, B=5, C=10)
  186. printf("ENTER SIZE (ELEMENTS) OF 3 DYNAMIC ARRAYS:\n");
  187. printf("1: ");
  188. scanf("%d",&size1);
  189. clearstdin();
  190. printf("2: ");
  191. scanf("%d",&size2);
  192. clearstdin();
  193. printf("3: ");
  194. scanf("%d",&size3);
  195. clearstdin();
  196. a=allocate_double(size1);
  197. b=allocate_double(size2);
  198. c=allocate_double(size3);
  199. printf("PRESS RETURN\n");
  200. clearstdin(); // WAIT KEY
  201.  
  202. i=0;
  203. do
  204. {
  205. printf("RANDOM FILL? Y/N: ");
  206. switch(getchar())
  207. {
  208. case 'y': //Mit ZUfallszahlen ausfuellen
  209. case 'Y':
  210. for(i=0;i<size1;i++)
  211. a[i]=rand()%10; //Modulo 10 dient dazu, damit nur 1 Ziffer drinsteht
  212. for(i=0;i<size2;i++)
  213. b[i]=rand()%10;
  214. break;
  215. case 'n':
  216. case 'N': //Ziffern manuell eingeben (Achtung, nur 1 Ziffer eingeben,
  217. Angabe ist hier sinnlos wegen Doubles, also nicht 0.5 eingb)
  218. printf("VALUES FOR A:\n");
  219. for(i=0;i<size1;i++)
  220. {
  221. printf("%u: ",i);
  222. scanf("%lf",&a[i]);
  223. }
  224. printf("VALUES FOR B:\n");
  225. for(i=0;i<size2;i++)
  226. {
  227. printf("%u: ",i);
  228. scanf("%lf",&b[i]);
  229. }
  230. break;
  231. default:
  232. clearstdin();
  233. continue;
  234. }
  235. } while(!i);
  236. print(a,size1);
  237. printf("PRESS RETURN\n");
  238. clearstdin(); //WAIT KEY
  239. print(b,size2);
  240. printf("PRESS RETURN\n");
  241. clearstdin(); //WAIT KEY
  242. printf("SUBTRACTING...\n");
  243. sub(a,size1,b,size2,c,size3);
  244. print(c,size3);
  245. printf("PRESS RETURN\n");
  246. clearstdin(); //WAIT KEY
  247. if(a)
  248. free_double(a);
  249. if(b)
  250. free_double(b);
  251. if(c)
  252. free_double(c);
  253. printf("That's all folks...\n");
  254. return 0;
  255. }
Add Comment
Please, Sign In to add comment