-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path2.c
61 lines (57 loc) · 1.88 KB
/
2.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
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
//function to print the winning possibility
void print(int *P, int p, int *C, int c, int tot)
{
int csum = 0; //sum of votes of coalition parties
for(int i = 0;i < c;i++)
{
csum += P[C[i] - 1];
}
float fract = (float)csum / tot;
if(fract > 0.5) //condition to check percentage is more than 50%
{
for(int i = 0;i < p;i++)
printf("%d: %d ",i+1,P[i]);
printf("\tTotal votes for the coalition: %d\n",csum);
}
}
//A recursive vote distribution function
void vote_dist(int *P, int p, int v,int *C, int c, int ind, int tot)
{
if(v == 0) //base condition which is true when no. of votes remaining is 0
print(P,p,C,c,tot);
else
{
for(int i = ind;i < p;i++)
{
P[i]++;
vote_dist(P,p,v-1,C,c,i,tot); //recursive call to the function where no. of votes remaining is less than 1
P[i]--;
}
}
}
int main(void)
{
int i,p,v,c=0;
printf("#Voters: ");
scanf("%d",&v);
printf("#Parties: ");
scanf("%d",&p);
int *P,*C;
P = (int*)malloc(sizeof(int) * p); //dynamically allocating memory
C = (int*)malloc(sizeof(int) * p);
for(i = 0;i < p;i++)
P[i] = 0; //initializing all elements of array to 0
printf("Coalition: \n");
for(i = 0;i < p+1;i++)
{
scanf("%d",&C[i]);
if(C[i] > p || C[i] < 1) //if value is not between 1 & p break;
break;
}
c = i;
vote_dist(P,p,v,C,c,0,v);
return 0;
}