-
Notifications
You must be signed in to change notification settings - Fork 0
/
dataFile.c
74 lines (57 loc) · 1.31 KB
/
dataFile.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <string.h>
void generateData()
{
FILE *fptr;
int pref, temp;
int numOfCandidates, numOfVoters;
int c ;
int randomVoters[numOfCandidates];
int preference[numOfVoters][numOfCandidates];
fptr = fopen("data.txt", "w+");
if (fptr == NULL)
{
printf("Error!! opening file");
exit(1);
}
printf("Enter the number of candidates: ");
scanf("%d", &numOfCandidates);
fprintf(fptr,"%d ", numOfCandidates);
printf("Enter the number of voters: ");
scanf("%d", &numOfVoters);
fprintf(fptr,"%d", numOfVoters);
fprintf(fptr,"\n");
srand(time(NULL));
for (int i = 0; i < numOfCandidates + 1; i++)
{
randomVoters[i] = i +1;
}
for (int i = 0; i < numOfCandidates; i++)
{
pref = rand()%numOfCandidates + 1;
temp = randomVoters[i];
randomVoters[i] = randomVoters[pref];
randomVoters[pref] = temp;
}
for (int i = 0; i < numOfVoters; ++i)
{
for (int j = 0; j < numOfCandidates; ++j)
{
preference[i][j] = randomVoters[i];
fprintf(fptr,"%d ", preference[i][j]);
}
fprintf(fptr,"\n");
}
fclose(fptr);
}
void readFile()
{
FILE *f = fopen("data.txt", "r");
}
int main()
{
generateData();
return 0;
}