Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

You work for a company that is implementing an electronic voting system. The sys

ID: 3691997 • Letter: Y

Question

You work for a company that is implementing an electronic voting system. The system allows voting by e-mail, and works as follows. Every registered voter sends an encrypted e-mail message containing the numeric ID of the candidate for which the vote is cast (IDs are 9 digit positive integers). E-mail messages are decrypted and the votes are appended to a list in the order in which they are received. Your job is to write a program that takes the combined list and determines if any candidate has received a majority (strictly more than half) of the votes cast. If there is such a candidate, he/she will be declared the winner of the election, otherwise the election is undecided.

Please provide code in JAVA. You may use hashing or randomized quick select algorithm.  (hint: if there is a candidate who wins a strict majority of the votes, then his/her ID must be the median of the set of votes)

Program Input

The program must read from the standard input a first line containing the number n of votes cast, followed by n lines with one vote per line.

Program Output

The program should print to the standard output a new-line terminated line in the format shown in the samples below.

Sample Input 1

Output for Sample Input 1

Sample Input 2

Output for Sample Input 2

Sample Input 3

Output for Sample Input 3

Explanation / Answer

import java.util.Scanner;
public class HelloWorld{

public static void main(String []args)
{
int i,j,n;
int votes[] = new int[100];
int candidate[] = new int[100];
int vote[] = new int[100];
System.out.print("Please number of votes to be cast: ");
Scanner in = new Scanner(System.in);
n = in.nextInt();
for(i=0;i<n;i++)
votes[i] = in.nextInt();
for(i=0;i<n;i++)
{
int status=0;
for(j=0;candidate[j] != 0;j++)
{
if(candidate[j] == votes[i])
{
vote[j]++;
status=1;
break;
}
}
if(status == 0)
{
candidate[j] = votes[i];
vote[j]++;
}
}
//find number of candidate in array candidate
int can_no=0;
for(j=0;candidate[j] != 0;j++)
can_no++;
//Sort the array vote to find the median
for (int c = 0; c < ( can_no - 1 ); c++)
{
for (int d = 0; d < can_no - c - 1; d++)
{
if (vote[d] < vote[d+1]) /* For descending order use < */
{
int swap = vote[d];
int swap1 = candidate[d];
vote[d] = vote[d+1];
candidate[d] = candidate [d+1];
vote[d+1] = swap;
candidate[d+1] =swap1;
}
}
}
if(vote[0] > n/2)
System.out.println(candidate[0] + " wins with "+ vote[0] + " votes out of " + n);
else
System.out.println("No candidate received a majority of the votes");
}
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote