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

Create a java program that reads an input of n lines with the first line being t

ID: 3554926 • Letter: C

Question

Create a java program that reads an input of n lines with the first line being the number of people in the contest and the remaining lines being a numeric 9 digit code for each person that gets a vote. For example the code 222222222 can be one person and code 567891012 can be another person. To win the contest your code needs to be inputted the most times. Then output the person or code that gets the most votes as the winner. Output no winner if theres no majority.

input:

3
222222222
567891012
222222222

(222222222 wins the contest because it is inputted the most times)

output:

222222222 wins 2 out of 3

Explanation / Answer

package Chegg;

/**
*
* @author User
*/
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;
public class Contest
{

/**
* @param args the command line arguments
*/
public static void main(String[] args)throws IOException
{
// TODO code application logic here
int i=0,j=0,z=0,k=0,max=0,pos=0,c=0;
boolean flag=true;
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter the input values:");
int ch=Integer.parseInt(br.readLine());
int array[]=new int[ch];
int freq[]=new int[ch];
for(i=0;i<ch;i++)
{
array[i]=Integer.parseInt(br.readLine());
}
for(i=0;i<ch;i++)
{
z=1;
flag=true;
for(k=0;k<i;k++)
{
if(array[i]==array[k])
flag=false;
}
if(flag==true)
{
for(j=i+1;j<ch;j++)
{
if(array[i]==array[j])
z++;
}
freq[i]=z;
}
}
for(i=0;i<ch;i++)
{
if(i==0)
{
max=freq[i];
pos=i;
}
else if(freq[i]>max)
{
max=freq[i];
pos=i;
}
}

for(i=0;i<ch;i++)
{
if(max==freq[i])
c++;
}

if(c==1)
System.out.println("The number is:" + array[pos]);

else

System.out.println("No majority");
}
}

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