Advertisement
Guest User

Untitled

a guest
Feb 23rd, 2020
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.41 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <assert.h>
  4.  
  5. enum TYPE {S, I, R};
  6.  
  7. //TODO: Implement idx
  8. //idx returns an integer to be used for hashing
  9. //this integer should be unique for every x, y pair in your grid
  10. int idx(int x, int y, int k)
  11. {
  12. return ((2 * k + 1)*(2 * k + 1))+(2 * k + 1) * x + y; //someone on discord said this
  13. }
  14.  
  15. typedef struct Host
  16. {
  17. int id;
  18. int x, y;
  19. int t;
  20. enum TYPE type;
  21. } THost;
  22.  
  23.  
  24. typedef struct node_tag {
  25. THost host;
  26. struct node_tag * next;
  27. } node;
  28.  
  29. //create a node whose value is a specific host
  30. //return a pointer to the created node
  31. node * create_node(THost host)
  32. {
  33. node *newnode = malloc(sizeof(node));
  34. newnode->host = host;
  35. newnode->next = NULL;
  36. return newnode;
  37. // free(newnode);
  38. }
  39.  
  40. //add_first() should add to the beginning of a linked list
  41. //note the type: 'node **head'
  42. //note that it does not return a value
  43. void add_first(node **head, node *newnode)
  44. {
  45. newnode->next = *head;
  46. *head = newnode;
  47.  
  48. }
  49.  
  50.  
  51. //remove the first node from the list
  52. //note the type: 'node **head'
  53. //return a pointer to the removed content
  54. node * remove_first(node **head)
  55. {
  56. node * temp = *head;
  57. *head = temp->next; //rest of list
  58. return temp;
  59. // free(temp);
  60. }
  61.  
  62. //remove all the nodes in the list
  63. //and free all the allocated memory
  64. void remove_all(node **head)
  65. { // node * iter = NULL;
  66. node * temp = NULL;
  67. while(*head != NULL){
  68. temp = *head;
  69. *head = temp->next;
  70. free(temp);
  71. }
  72. //free(iter);
  73. //free(temp);
  74. }
  75.  
  76. //location_match checks whether a linked list contains
  77. //one or more hosts in the same location as 'host'
  78. //return 1 if there is a match, 0 if not
  79. int location_match(node *head, THost host)
  80. { node * n = head;
  81. while(n != NULL){
  82. if((n->host.x==host.x)&&(n->host.y==host.y)){
  83. return 1;
  84. // free(n);
  85. }
  86. n=n->next;} //check next value in linked node
  87. return 0;
  88. // free(n);
  89. }
  90.  
  91.  
  92. //hash function included for your convenience :)
  93. unsigned hash(unsigned a)
  94. {
  95. a = (a ^ 61) ^ (a >> 16);
  96. a = a + (a << 3);
  97. a = a ^ (a >> 4);
  98. a = a * 0x27d4eb2d;
  99. a = a ^ (a >> 15);
  100. return a;
  101. }
  102. //summary prints out the proportions of different host types.
  103. //It returns 1 if the number of infected hosts is not 0.
  104. int summary(THost hosts[], int m)
  105. {
  106. int S_n, I_n, R_n;
  107.  
  108. S_n = I_n = R_n = 0;
  109. for(int i = 0; i < m; i++)
  110. {
  111. S_n += (hosts[i].type == S);
  112. I_n += (hosts[i].type == I);
  113. R_n += (hosts[i].type == R);
  114. }
  115. if(I_n == 0)
  116. {
  117. printf(" S I R\n");
  118. printf("%lf %lf %lf\n", (double)S_n/(S_n + I_n + R_n),
  119. (double)I_n/(S_n + I_n + R_n), (double)R_n/(S_n + I_n + R_n));
  120. }
  121. return I_n > 0;
  122. }
  123.  
  124. // one_round
  125. int one_round(THost *hosts, int m, node *p_arr[], int n_arr, int k, int T)
  126. {
  127. //S -> I and I -> R
  128. for(int i = 0; i < m; i++)
  129. {
  130. if(hosts[i].type == S) //Note the use of enumerator S
  131. {
  132. //finish the following line of code
  133. int index = hash(idx(hosts[i].x, hosts[i].y, k)) % n_arr;
  134. if(location_match(p_arr[index], hosts[i]))
  135. {hosts[i].type = I; //you have ebola now!
  136. //TODO: fill in what should happen here (not long)
  137. }
  138. }
  139. else if(hosts[i].type == I)
  140. {hosts[i].t += 1;
  141. if(hosts[i].t==T){
  142. hosts[i].type = R; //you have survived ebola
  143. }
  144. //TODO: fill in what should happen here (not long)
  145. }
  146. }
  147.  
  148. //TODO: fill in code below
  149. //reset all linked lists
  150. for(int i=0; i<n_arr; i++){
  151. remove_all(&(p_arr[i]));
  152. }
  153.  
  154.  
  155. for(int i = 0; i < m; i++)
  156. {
  157. int r = rand() % 4;
  158. //finish the follow code
  159. //0: up, 1: right, 2: down, 3: left
  160. //TODO: update locations for all hosts
  161. switch(r)
  162. {
  163. case 0: hosts[i].y = (((hosts[i].y + 1) + k)%(2 * k + 1) - k); break; //because some dude on piazza said so
  164. case 1: hosts[i].x = (((hosts[i].x + 1) + k)%(2 * k + 1) - k); break;
  165. case 2: hosts[i].y = (((hosts[i].y - 1) + k)%(2 * k + 1) - k); break;
  166. case 3: hosts[i].x = (((hosts[i].x - 1) + k)%(2 * k + 1) - k); break;
  167. }
  168.  
  169. //buid linked list for I hosts
  170. if(hosts[i].type == I)
  171. {
  172. node *r = create_node(hosts[i]);
  173. int index = hash(idx(hosts[i].x, hosts[i].y, k)) % n_arr;
  174. add_first(&(p_arr[index]), r);
  175. }
  176. }
  177.  
  178. return summary(hosts, m);
  179. }
  180.  
  181. int main(int argc, char *argv[])
  182. {
  183.  
  184. if(argc != 5)
  185. {
  186. printf("Usage: %s k m T N\n", argv[0]);
  187. return 0;
  188. }
  189.  
  190. int k = atoi(argv[1]);
  191. int m = atoi(argv[2]);
  192. int T = atoi(argv[3]);
  193. int N = atoi(argv[4]);
  194.  
  195. assert(k >= 0 && k <= 1000);
  196. assert(m >= 1 && m <= 100000);
  197. assert(T >= 1);
  198. assert(N > 0 && N <= 100000);
  199. srand(12345);
  200.  
  201. //initialize hosts
  202. THost hosts[m];
  203.  
  204. hosts[0].id = 0;
  205. hosts[0].x = 0;
  206. hosts[0].y = 0;
  207. hosts[0].t = 0;
  208. hosts[0].type = I;
  209.  
  210. for(int i = 1; i < m; i ++)
  211. {
  212. hosts[i].id = i;
  213. hosts[i].x = rand() % (2*k + 1) - k;
  214. hosts[i].y = rand() % (2*k + 1) - k;
  215. hosts[i].t = 0;
  216. hosts[i].type = S;
  217. }
  218.  
  219. //initialize linked lists
  220. node *p_arr[N];
  221.  
  222. for(int i = 0; i < N; i++)
  223. {
  224. p_arr[i] = NULL;
  225. }
  226. node *r = create_node(hosts[0]);
  227. int index = hash(idx(hosts[0].x, hosts[0].y, k)) % N;
  228. add_first(&(p_arr[index]), r);
  229.  
  230. //simulation
  231. while(one_round(hosts, m, p_arr, N, k, T));
  232.  
  233. return 0;
  234. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement