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 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.









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
Array Sorting - Development process
Array Sorting  Hello sir, Now i m doing various Array sorting(Bubble,insertion,Quick,merge etc ) I want to display Array Sort in steps by step like... is the code to display array sorting with moving graphics(color)in steps
What are the different functions in sorting an array?
What are the different functions in sorting an array?  What are the different functions in sorting an array
Array sorting - Java Beginners
Array sorting   Hello All. I need to sort one array based on the arrangement of another array. I fetch two arrays from somewhere... need to sort the "name" array alphabetically. I can do that easily using
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
sorting an array of string with duplicate values - Java Beginners
sorting an array of string  Example to sort array string
sorting an array of string with duplicate values - Java Beginners
sorting an array of string with duplicate values  I have a sort method which sorts an array of strings. But if there are duplicates in the array it would not sort properly
Insertion, sorting and searching in array
; } Insertion, sorting and searching in array   ... to perform searching and sorting in array. In the java code given below we have...("\nArray elements after sorting", array); // Search for element
PHP Array Random Sorting
PHP Array Random Sort PHP provides another kind of sorting which is called Random Sorting,  in PHP shuffle() is used to sort in this manner. This function shuffles an array. Small description of shuffle function is as follows:ADS
PHP Array Reverse Sorting
] => jaguar [5] => land rover ) After sorting values become:Array ( [0] =>...($car); ?> Output:ADS_TO_REPLACE_12 Before sorting the values are:Array...; jaguar ) After sorting values become:Array ( [v] => vista [s] => safari [n
PHP Array Sorting
_TO_REPLACE_4 Output: Before sorting the values are:Array ( [0] => manza... rover ) After sorting values become:Array ( [0] => jaguar [1] => land...; Output:ADS_TO_REPLACE_12 Before sorting the values are:Array ( [m] => manza [s
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 an array of string with duplicate values - Java Beginners
String of Array  What is mean by string of array? And how one can add, delete the records from database in string format
sorting
sorting   write a program to arrange sorting in rows,column and diagonal
Read text file to 2D array and sorting the second column
Read text file to 2D array and sorting the second column  we found that the student names with marks in a text file the marks decreases significantly. when exams conducted fast 4 times.The first column is the student name
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
PHP Key Sorting
;); echo"<b>Before sorting the keys of the array is:</b><br/>...;<b>Before sorting the keys of the array is:</b><br/>"; print_r($array);ADS_TO_REPLACE_5 ?> Output: Before sorting the keys
PHP MultiArray Sorting
PHP Array Sort Multidimensional PHP provides array_multisort() function to sort more than one array at a time or multi-dimensional array. String keys... of array_multisort() is as follows: General Format bool array
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
sorting an array of string with duplicate values - Java Beginners
JavaScript array sort numerically
JavaScript array sort numerically       As in our previous example of sorting array elements... in this example of sorting array elements ( numeric elements ) we have created
group by sorting
group by sorting  group by sorting of data in SQL
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
PHP Value Sorting
; Output:ADS_TO_REPLACE_7 Before sorting the values are: Array ( [0] => manza [1... rover ) After sorting values become: Array ( [0] => jaguar [1] => land...; vista [j] => jaguar [l] => land rover ) After sorting values become:Array
merge sorting in arrays - Java Beginners
merge sorting in arrays  Write a program to insert string or characters to an array and apply merge sorting on this array  Hi Friend, Please visit the following link: http://www.roseindia.net/java/beginners
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
sorting and storing data
sorting and storing data   sorting and storing data in UITableView
sorting - Java Beginners
; Easy Sorting: For all the values in the array A, find the largest and store... readIntegers() that o reads the user inputs o stores them in an array, and o returns the array   ? The program includes easySort() that o sorts
Array
Array  What if i will not declare the limit index of an array, how will I declare an array and store values with it using loop?   Hi Friend... Scanner(System.in); int array[]=new int[5]; System.out.println("Enter
sorting numbers
sorting numbers  How to sort the numbers in ascending order   import java.util.*; class SortNumbers{ public static void main(String[] args) { Scanner input=new Scanner(System.in
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
Sorting Array List in Java
Sorting Array List in Java In this example you will learn about sorting array list in java using an example. By default array list are sorted as the element... the element increased. This example will explain sorting of ArrayList using Comparator
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
Java sorting
sorting mechanism. say about Collections.Sort() & Arrays.Sort() that uses
Array
Array  can we create an array of size 1 lakh in java programming

Ads