Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <time.h>
- #define SIZE 100000
- struct students{
- int id[SIZE];
- int isPointedAt[SIZE];
- int toTheLeft[SIZE];
- int toTheRight[SIZE];
- };
- void setId(struct students* students1);
- void getId(struct students* students1);
- void setPointed(struct students* students1);
- void setNeighbors(struct students* students1);
- void cheat(struct students* students1);
- float results(struct students* students1);
- void saveToFile(float res);
- void makeAverage();
- int main() {
- srand(time(0));
- float res=0;
- printf("Random number is: %d",rand());
- struct students students1;
- printf("Hello, World!\n");
- setId(&students1);
- setNeighbors(&students1);
- int i;
- for (i = 0; i < 10000; ++i) {
- setPointed(&students1);
- cheat(&students1);
- res=results(&students1);
- saveToFile(res);
- }
- makeAverage();
- return 0;
- }
- void setId(struct students* students1)
- {
- int i;
- for (i = 0; i < SIZE; ++i) {
- students1->id[i] = i+1;
- }
- }
- void setPointed(struct students* students1)
- {
- int i;
- for (i = 0; i < SIZE; ++i) {
- students1->isPointedAt[i] = 0;
- }
- }
- void setNeighbors(struct students* students1)
- {
- int i;
- for (i = 0; i < SIZE; ++i) {
- if(students1->id==1)
- {
- students1->toTheRight[i]=SIZE;
- students1->toTheLeft[i]=i+1;
- }else if(students1->id==SIZE)
- {
- students1->toTheRight[i]=i-1;
- students1->toTheLeft[i]=1;
- }else
- {
- students1->toTheRight[i]=i-1;
- students1->toTheLeft[i]=i+1;
- }
- }
- }
- void getId(struct students* students1)
- {
- int i;
- for (i = 0; i < SIZE; ++i) {
- printf("student %d Id: %d\n",i,students1->id[i]);
- }
- }
- void cheat(struct students* students1)
- {
- int i;
- for (i = 0; i < SIZE; ++i) {
- if(rand()%2==0) //if 1 then cheats left
- {
- students1->isPointedAt[students1->toTheRight[i]]=1;
- }else
- {
- students1->isPointedAt[students1->toTheLeft[i]]=1;
- }
- }
- }
- float results(struct students* students1)
- {
- int notCheatedfrom=0;
- int i;
- for (i = 0; i < SIZE; ++i) {
- if(students1->isPointedAt[i]==0)
- {
- notCheatedfrom++;
- }
- }
- // printf("%d out of %d is %f\n",notCheatedfrom,SIZE,(float)notCheatedfrom/SIZE);
- return (float)notCheatedfrom/SIZE;
- }
- void saveToFile(float res)
- {
- FILE* file;
- file = fopen("results.txt","a");
- fprintf(file,"%f\n",res);
- fclose(file);
- }
- void makeAverage()
- {
- FILE* file;
- file = fopen("results.txt","r");
- float results[110000];
- char line[10];
- int i=0;
- while (fgets(line, sizeof(line), file)) {
- /* note that fgets don't strip the terminating \n, checking its
- presence would allow to handle lines longer that sizeof(line) */
- results[i]=atof(line);
- i++;
- }
- fclose(file);
- int j;
- float average=0;
- for (j=0;j<i;j++) {
- average+=results[j];
- }
- average=average/j;
- printf("average result from %d results is: %f",j,average);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement