Random number, character array and bundle sort of shaping array

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.