13, (* * *) simulation n individual participation in the election process, and output election results: Assuming four candidates, respectively, a, B, C, D, the election of a candidate, the direct input of their number (number is randomly generated by the computer),
If the input is not a, B, C, D is considered invalid, after the end of the election, the number of votes from high to low output candidate number and the number of votes obtained.
Request: Take the random number, through the random number results to determine the results of the ballot and stored in the array, and then through the following table the shape array and corresponding character array sorted, output results.
int main ()
{
int people_num;
printf ("Number of people entering the crowd:");
scanf ("%d", &people_num); Mass number input
int candidate = 0; Candidate number
int tickets[4]={0,0,0,0}; The number of votes a[0], the number of votes on behalf of a, the number of votes b[0], the number of votes on behalf of B, the number of votes c[0], the number of votes on behalf of C, the number of votes d[0], the number of votes on behalf of D
Char people[4]={' A ', ' B ', ' C ', ' D '};
printf ("The resulting random ticket is:");
for (int i = 0; i < people_num; i++)
{
Candidate=arc4random ()% (4-0+1); Generated with the ticket, 0 on behalf of a,1 representative b,2 on behalf of c,3 representative d,4
printf ("%d\t", candidate);
if (Candidate > 3) {
Continue
}else{
tickets[candidate]++;
}
}
for (int i = 0; I <4-1; i++) {//by the way the ballot is bubbled
for (int j = 0; j< 4-i-1; j + +) {
if (Tickets[j] < tickets[j+1]) {
int temp = 0; Ballot sorting
temp = Tickets[j];
TICKETS[J] = tickets[j+1];
TICKETS[J+1] = temp;
Char temp2 = ' 0 '; Sort names at the same time
Temp2 = People[j];
PEOPLE[J] = people[j+1];
PEOPLE[J+1] = Temp2;
}
}
}
printf ("\ n");
for (int i = 0; i< 4; i++) {
printf ("%c%d\n", People[i], tickets[i]);
}
return 0;
}
Random number, character array and bundle sort of shaping array