Advertisement
tomur

Cheaters problem

Jan 6th, 2018
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.26 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <time.h>
  4.  
  5. #define SIZE 100000
  6.  
  7. struct students{
  8.     int id[SIZE];
  9.     int isPointedAt[SIZE];
  10.     int toTheLeft[SIZE];
  11.     int toTheRight[SIZE];
  12.  
  13. };
  14.  
  15. void setId(struct students* students1);
  16. void getId(struct students* students1);
  17. void setPointed(struct students* students1);
  18. void setNeighbors(struct students* students1);
  19. void cheat(struct students* students1);
  20. float results(struct students* students1);
  21. void saveToFile(float res);
  22. void makeAverage();
  23.  
  24. int main() {
  25.     srand(time(0));
  26.     float res=0;
  27.     printf("Random number is: %d",rand());
  28.     struct students students1;
  29.     printf("Hello, World!\n");
  30.     setId(&students1);
  31.     setNeighbors(&students1);
  32.  
  33.     int i;
  34.     for (i = 0; i < 10000; ++i) {
  35.         setPointed(&students1);
  36.         cheat(&students1);
  37.         res=results(&students1);
  38.         saveToFile(res);
  39.     }
  40.  
  41.     makeAverage();
  42.     return 0;
  43. }
  44.  
  45. void setId(struct students* students1)
  46. {
  47.     int i;
  48.     for (i = 0; i < SIZE; ++i) {
  49.         students1->id[i] = i+1;
  50.     }
  51. }
  52.  
  53. void setPointed(struct students* students1)
  54. {
  55.     int i;
  56.     for (i = 0; i < SIZE; ++i) {
  57.         students1->isPointedAt[i] = 0;
  58.     }
  59. }
  60.  
  61. void setNeighbors(struct students* students1)
  62. {
  63.     int i;
  64.     for (i = 0; i < SIZE; ++i) {
  65.         if(students1->id==1)
  66.         {
  67.             students1->toTheRight[i]=SIZE;
  68.             students1->toTheLeft[i]=i+1;
  69.  
  70.         }else if(students1->id==SIZE)
  71.         {
  72.             students1->toTheRight[i]=i-1;
  73.             students1->toTheLeft[i]=1;
  74.         }else
  75.         {
  76.             students1->toTheRight[i]=i-1;
  77.             students1->toTheLeft[i]=i+1;
  78.         }
  79.     }
  80. }
  81.  
  82. void getId(struct students* students1)
  83. {
  84.     int i;
  85.     for (i = 0; i < SIZE; ++i) {
  86.         printf("student %d Id: %d\n",i,students1->id[i]);
  87.     }
  88. }
  89.  
  90. void cheat(struct students* students1)
  91. {
  92.     int i;
  93.     for (i = 0; i < SIZE; ++i) {
  94.         if(rand()%2==0)     //if 1 then cheats left
  95.         {
  96.             students1->isPointedAt[students1->toTheRight[i]]=1;
  97.         }else
  98.         {
  99.             students1->isPointedAt[students1->toTheLeft[i]]=1;
  100.         }
  101.     }
  102. }
  103.  
  104. float results(struct students* students1)
  105. {
  106.     int notCheatedfrom=0;
  107.         int i;
  108.         for (i = 0; i < SIZE; ++i) {
  109.             if(students1->isPointedAt[i]==0)
  110.             {
  111.                 notCheatedfrom++;
  112.             }
  113.         }
  114.    // printf("%d out of %d is %f\n",notCheatedfrom,SIZE,(float)notCheatedfrom/SIZE);
  115.     return (float)notCheatedfrom/SIZE;
  116. }
  117. void saveToFile(float res)
  118. {
  119.     FILE* file;
  120.     file = fopen("results.txt","a");
  121.     fprintf(file,"%f\n",res);
  122.     fclose(file);
  123.  
  124. }
  125.  
  126. void makeAverage()
  127. {
  128.     FILE* file;
  129.     file = fopen("results.txt","r");
  130.     float results[110000];
  131.     char  line[10];
  132.     int i=0;
  133.     while (fgets(line, sizeof(line), file)) {
  134.         /* note that fgets don't strip the terminating \n, checking its
  135.            presence would allow to handle lines longer that sizeof(line) */
  136.         results[i]=atof(line);
  137.         i++;
  138.     }
  139.     fclose(file);
  140.     int j;
  141.     float average=0;
  142.     for (j=0;j<i;j++) {
  143.  
  144.         average+=results[j];
  145.     }
  146.     average=average/j;
  147.     printf("average result from %d results is: %f",j,average);
  148. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement