C Programming – HackerRank Solution | 2D ARRAYS- Election of Candidates |

(HackerRank) Write a Modular C Programming code to solve 2D ARRAYS- Election of Candidates, An election is contested by 5 candidates. The candidates are numbered 1 to 5

An election is contested by 5 candidates. The candidates are numbered 1 to 5 and the voting is done by marking the candidates number on the ballot paper. Apply Problem solving framework and Write a modular C program to read the ballots and count the vote cast for each candidate.

image

Input Format

First line indicates number of Candidates.

Second line indicates number of voters.

Further lines indicate matrix of votes casted by voters.

Constraints

Vote can be either 0 or 1 by a voter.

1 indicates a voter casted a vote to one candidate only. For other candidates vote becomes 0.

Output Format

Display candidate wise vote count.

Sample Input 0

5 4
1 1 0 0
0 0 0 0
0 0 1 0
0 0 0 0
0 0 0 1

Sample Output 0

Candidate 1: 2
Candidate 2: 0
Candidate 3: 1
Candidate 4: 0
Candidate 5: 1

Sample Input 1

2 5
0 1 2 3 4
1 2 3 4 0

Sample Output 1

Invalid votes

Sample Input 2

0 -5

Sample Output 2

Invalid input

Refer : C Programming HackerRank all solutions for Loops | Arrays | strings | Data Structures | Linked lists | Stacks | Queues | Binary Trees

 

CODE:

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>

int main() {
    int voters,candidates,i,j;
    scanf("%d",&candidates);
    scanf("%d",&voters);
    if(candidates>0 && voters>0)
    {
    int vote[candidates][voters];
    int vote_sum[candidates];
    for(i=0;i<candidates;i++)
    {
        vote_sum[i]=0;
        for(j=0;j<voters;j++)
        {
            scanf("%d",&vote[i][j]);
            if(vote[i][j]!=0 && vote[i][j]!=1)
            {
                printf("Invalid votes");
                exit(0);
            }
            vote_sum[i]+=vote[i][j];
        }
    }
    for(i=0;i<candidates;i++)
        printf("Candidate %d: %d\n",i+1,vote_sum[i]);
}
    else{
        printf("Invalid input");
    }
    return 0;
}

OUTPUT

Congratulations, you passed the sample test case.

Click the Submit Code button to run your code against all the test cases.

Input (stdin)

5 4
1 1 0 0
0 0 0 0
0 0 1 0
0 0 0 0
0 0 0 1
Your Output (stdout)

Candidate 1: 2
Candidate 2: 0
Candidate 3: 1
Candidate 4: 0
Candidate 5: 1
Expected Output

Candidate 1: 2
Candidate 2: 0
Candidate 3: 1
Candidate 4: 0
Candidate 5: 1

 

Please find some more codes of Loops, Condition Statements, 1D Arrays, 2D Arrays, Strings, Pointers, Data Structures, Files, Linked lists, Stacks, Queues, Binary Trees, MISC, Solved model question papers & Hacker Rank all solutions on the below page:

Top 100+ C Programming codes – KLE Technological University







Leave a Comment

Ads Blocker Image Powered by Code Help Pro

Ads Blocker Detected!!!

Welcome to FactsPrime

Sorry, We have detected that you have activated Ad-Blocker. Please Consider supporting us by disabling your Ad Blocker, It helps us in maintaining this website. To View the content, Please disable adblocker and refresh the page.

Thank You !!!