Sorting the array

Ads
 

Sorting the array

Implement a program to process votes for 5 candidates in a talent contest. The program should use a String array to hold the names of the 5 candidates and an integer array to record the number of votes for each contestant.

It should prompt the user to enter the number of the candidate they wish to vote for (in the range 0 ? 4), until -1 is entered, which signifies the end of voting. An error message should be output if the candidate selected is not in the required range.

At the end of voting, the program should sort the votes into descending order and output them, before outputting messages showing who was in 3rd, 2nd and 1st place:

Hints: ? you will need to declare 2 arrays: one for integer votes to be input by the user and one for the names which you should initialise in the program ? you will need to modify the input method from the lectures so that it inputs votes until -1 is entered ? you will need to modify the sort method from the lectures so that it sorts into descending order, rather than ascending ? you will need to modify the output method from the lectures so that it outputs the candidate and the number of votes they received ? you will need 2 swap methods: one to swap names and one to swap votes so that the names and their associated votes remain synchronised

View Answers

December 10, 2011 at 3:52 PM

So far : //exam result processing - using selection sort

import java.util.*;

public class VoteCount {

public static void main(String[] args) {
    //create empty array
    int[] votes = new int[5];

    //input data
    input(votes);

}

public static void input(int[] votes) 
{
    Scanner kybd = new Scanner(System.in);
    System.out.println("Enter vote number of the candidate results: ");
    int votecount = kybd.nextInt();

    while (votecount !=-1) 
    {
        votes[votecount]++;  
        System.out.println("Candidate" + votes +"Has" +votecount + "votes");

    }



}

}

Wrong coding tho.

Ads









Related Tutorials/Questions & Answers:
Sorting the array
Sorting the array  Implement a program to process votes for 5 candidates in a talent contest. The program should use a String array to hold the names of the 5 candidates and an integer array to record the number of votes for each
sorting array in java
sorting array in java  How to sort array in Java or JavaScript?   JavaScript Sorting array tutorial   Java Sort array of strings...[] args) { String array[]=new String[5]; Scanner input = new Scanner
Advertisements
java array sorting manual way
java array sorting manual way  Hi, Anyone has any sample code on how to sort a array manually without using the inbuild sorting algorithm? Thanks
To find first two maximum numbers in an array,using single loop without sorting array.
To find first two maximum numbers in an array,using single loop without sorting array.  Java program to find first two maximum numbers in an array,using single loop without sorting array
To find first two maximum numbers in an array,using single loop without sorting array.
To find first two maximum numbers in an array,using single loop without sorting array.  Java program to find first two maximum numbers in an array,using single loop without sorting array
sorting
sorting   write a program to arrange sorting in rows,column and diagonal
Sorting
Sorting  can any help me know which sorting algorithm java uses for sorting collection and arrays
sorting
sorting  write to a program to arrange the sorting in rows, column and digonal
sorting
sorting  how to do sorting without using bubble sort,selection sort
sorting
) { //implement a search function for a sorted rotated array. //duplicates... sample"); public int maxSumSubArray(int[] array, int len, int start, int end) { //given an array with integers(both positive and negative) //in any random order
Sorting in Java
Sorting in Java  Sorting in Java
Sorting Program
Sorting Program  To sort 10items in alphabetical order   Arrays.sort(name of ur array)   import java.util.*; class ArrayExample{ public static void main(String[] args) { String array[]=new
linear sorting
linear sorting  what is linear sorting? can any send me an example...("After Sorting: "); for(int i=0;i<arr.length;i++){ System.out.println...) { Scanner input=new Scanner(System.in); System.out.println("Enter Array
C array sort example
C array sort example       In this section, you will learn how to sort an array in C. For sorting an array, we have used the qsort function. This function provides
group by sorting
group by sorting  group by sorting of data in SQL
in_array
in_array  in_array in php
is _array()
is _array()  is_array() in php   Hi Friend, This function is of Boolean type.It checks whether a variable is an array or not. Here is an example: <?php $yes = array('Hello', 'World'); echo is_array($yes) ? 'Array
is _array()
is _array()  is _array()   Hi Friend, This function is of Boolean type.It checks whether a variable is an array or not. Here is an example: <?php $yes = array('Hello', 'World'); echo is_array($yes) ? 'Array
Java array
Java array  Java program to find first two maximum numbers in an array,using single loop without sorting array
Array
Array  how can i use elements of an array in a circular form? that is if the searching of the element reach the last elements of the array, then it will start serching from the begining of the array
Array
Array  is it possible to define array like this..? int[] intArray = new int[] {4,5,6,7,8}; Explain...?   Yes, you can. Java Initialize Array
array
array  write and test a function named mirror that is passed an array of n floats and returns a newly created array that contains those n floats... the array {10.1,11.2,8.3,7.5,22} into{22,7.5,8.3,11.2,10.1
array
array  write and test a function named mirror that is passed an array of n floats and returns a newly created array that contains those n floats... the array {10.1,11.2,8.3,7.5,22} into{22,7.5,8.3,11.2,10.1
array
array  write and test a function named mirror that is passed an array of n floats and returns a newly created array that contains those n floats... the array {10.1,11.2,8.3,7.5,22} into{22,7.5,8.3,11.2,10.1
Array
Array  How do i insert elements into an array up to a limit from...("Enter Range: "); int size=input.nextInt(); int array[]=new int[size]; System.out.println("Enter Array Elements: "); for(int i=0;i<
array
array  WAP in java to store 6 element in array P and 4 element in array Q. Produce the third arra y R containing all element from p & q
array
array  array memory allocation is dynamic or static in java   Java Arrays have dynamic memory allocation
array
array  create an array in which no element is duplicate. if duplicate then tell to user duplicate. duplicacy is tell to user during when he enter the element
array
array  create an array in which no element is duplicate. if duplicate then tell to user duplicate. duplicacy is tell to user during when he enter the element
array
array  take a 2d array and display all its elements in a matrix fome using only one for loop and ple explain the program in below
array
accepts a pointer to integer which represents an array of integer.After that this method prints the entire of the array numbers to the monitor. include using std::cout; using std::endl; void printArray(int *array, int count

Ads