Results 1 to 4 of 4
  1. #1
    Chromatism is offline Member
    Join Date
    Apr 2012
    Posts
    2
    Rep Power
    0

    Default Help desperately needed - synchronized swaps

    Hi everyone,

    I've got a problem that I'm hoping someone can find an answer to in fairly short order (college work issue)
    I've been given a task to read in votes for 5 candidates, and then display the votes and candidates names in descending order

    The question in full reads:
    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:


    • 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 - done
    • you will need to modify the input method from the lectures so that it inputs votes until -1 is entered - done
    • you will need to modify the sort method from the lectures so that it sorts into descending order, rather than ascending - done
    • you will need to modify the output method from the lectures so that it outputs the candidate and the number of votes they received - Not there yet
    • 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 - Help needed!


    My issue is that this means I need a int array for votes and a string array for the names, and I need to keep them synchronized while I sort them for output. - this aspect has not been taught, and there are no references for how to do this anywhere on in my course notes. I'll post my current workings below, but please be aware I've been fudging around with a stringSwap method with no success.

    We're using very basic language so far, so what you see in my code below is pretty much what I've been taught.


    Java Code:
    //output candidates from third place to first by votes
    import java.util.*;
    public class Test
    {
    	public static void main (String [] args)
    	{
    		int [] arr = new int[5];
    		input(arr);
    		sort(arr);
    		output(arr); //for testing
    	}
    
    	public static void input(int[] arr)
    	{
    		Scanner kybd = new Scanner(System.in);
    		System.out.println("Enter Candidate Numbers. -1 ends: ");
    		int input = kybd.nextInt();
    		while(input > -1)
    		{
    			if(input > 0 && input < 6)
    			{
    				arr[input - 1]++;
    			}
    			input = kybd.nextInt();
    		}
    	}
    	
    	public static void sort(int[] arr)
    	{
        		for (int pass = 1; pass < arr.length; pass++)
         		{
              		int largestPos = findLargest(arr, arr.length - pass);
              		if (largestPos != arr.length - pass)
              		{
    						toString(largestPos, arr.length - pass);
                   			swap(arr, largestPos, arr.length - pass);
              		}
              		output(arr); //this is for testing purposes only
         		}
    	}
    
    	public static int findLargest(int [] arr, int num)
    	{
    		int largestPos = 0;
    		for (int i = 1; i <= num; i++)
    		{
    			if (arr[i] <= arr[largestPos])
    			{
    				largestPos = i;
    			}
    		}
    		return largestPos;
    	}
    	
    	public static void swap(int [] arr, int first, int second)
    	{
         		int temp = arr[first];
         		arr[first] = arr[second];
         		arr[second] = temp;
    	}
    	
    	public static void output(int [] arr)
    	{
    		for (int i = 0; i < arr.length; i++)
    		{
    			System.out.print(arr[i] + "  ");
    		}
    		System.out.println();
    	}
    	
    	public static String toString(int num, int numTwo)
    	{
    		String [] text = {"Cliff", "Dave", "Eric", "Bob", "Andy"};
    		stringSwap(text, num, numTwo);
    		return text[num];
    	}
    	
    	public static void stringSwap(int [] text, int num, int numTwo)
    	{
    			int temp = text[num];
    			text[num] = text[num2];
    			text[num2] = temp;
    	}
    }


    Any help would be much appreciated. Thanks!
    Last edited by Chromatism; 04-29-2012 at 08:34 PM.

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,188
    Rep Power
    19

    Default Re: Help desperately needed - synchronized swaps

    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  3. #3
    Chromatism is offline Member
    Join Date
    Apr 2012
    Posts
    2
    Rep Power
    0

    Default Re: Help desperately needed - synchronized swaps

    Quote Originally Posted by DarrylBurke View Post
    Yeah, that's my girlfriend who's having the same problem.

    A reply in either thread would be very much appreciated.

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

Similar Threads

  1. I desperately need help
    By dc2acgsr99 in forum Java Applets
    Replies: 5
    Last Post: 01-11-2014, 05:36 PM
  2. Replies: 15
    Last Post: 04-21-2012, 01:38 AM
  3. Replies: 1
    Last Post: 09-08-2010, 04:08 PM
  4. Declaring a Variable for RGB swaps? Dr Java
    By brandnew956 in forum New To Java
    Replies: 5
    Last Post: 03-03-2010, 01:44 AM
  5. Desperately need help
    By nel636 in forum New To Java
    Replies: 0
    Last Post: 12-03-2008, 12:43 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •