hi, I originally made a 1 dimensional array but then modified it into 2 dim. Problem is, I used percentage to determine who has the most number of votes but variable newPresident only output Estrada J. which belong to array candidate[0]. When I tried printing the elements in percentage[], only percentage[0] has a stored value, the rest is 0.0.
Can anyone tell me what's wrong with my code? I want to store all the percentage values to percentage[] or else my if() else statement won't work...

    public static void main(String[] args) {

        //Array Declaration
        String[] candidate = {"Estrada J.", "Drilon F.", "Enrile J.", "Lacson P."};
        int[][] votes = {{5, 5, 10, 11, 20, 30, 4, 15, 10, 30, 40, 50, 60},
                         {10, 20, 30, 35, 25, 10, 10, 20, 30, 10, 20, 15, 30},
                         {20, 15, 15, 20, 20, 30, 50, 50, 20, 10, 15, 15, 15},
                         {50, 50, 10, 10, 10, 10, 10, 20 ,15, 30, 40, 40, 50}};
       float[] percentage = new float[4];

        //Variable Declaration
        String newPresident;
        int row, col;
        float overAll = 0;
        System.out.println("************************************************"
                + "***********************************************************"
                + "*********");
        System.out.println("\t\t\t\t\tPRESIDENTIAL ELECTION 2016");
        System.out.println("************************************************"
                + "***********************************************************"
                + "*********");
        System.out.println("Candidates\t1\t2\t3\t4\t5\t6\t7\t8\t9\t10\t11\t12"
                + "\tNCR");
        System.out.println("-----------------------------------------------"
                + "----------------------------------------------------------"
                + "-----------");

        for(row=0; row<votes.length; row++)
        {    
        int totalVotes=0;
            System.out.printf("%10s", candidate[row]);
            for(col=0; col<votes[row].length; col++) {
                System.out.printf("%8d",votes[row][col]); 
                totalVotes = totalVotes + votes[row][col];}
            overAll = overAll + totalVotes;
            System.out.print("\n");          
        }
         System.out.println("************************************************"
                + "***********************************************************"
                + "*********");   
         System.out.println("CANDIDATE \t TOTAL VOTE \tPERCENTAGE");
         for(row=0; row<votes.length; row++)
         {   
            int totalVotes=0, ctr=0;


            System.out.printf("%10s\t", candidate[row]);
             for(col=0; col<votes[row].length; col++) {
                 totalVotes= totalVotes + votes[row][col];}
            percentage[ctr] = (totalVotes / overAll) * 100;
            System.out.printf("%7d\t\t%7.2f", totalVotes,  percentage[ctr]);
            System.out.print("%\n");
            ctr++;
        }

        if((percentage[0] > percentage[1]) && (percentage[0] > percentage[2]) && (percentage[0] > percentage[3]))
            newPresident = candidate[0];
        else if((percentage[1] > percentage[0]) && (percentage[1] > percentage[2]) && (percentage[1] > percentage[3]))
            newPresident = candidate[1];
        else if((percentage[2] > percentage[0]) && (percentage[2] > percentage[1]) && (percentage[0] > percentage[3]))
            newPresident = candidate[2];
        else
            newPresident = candidate[3];

        System.out.println();
        System.out.println("The Candidate with the highest vote is: " + newPresident);
        System.out.println("************************************************"
                + "***********************************************************"
                + "*********");  

In your loop (starting line 43) you use ctr to index the % values, increasing it at the end of the loop, but you set it back to zero at the start of every pass thru the loop (line 44), so you only ever use index 0. The newPresident is wrong because all the other candidtaes have 0% beause of the above bug.
ps: Can't see why you use ctr at all, why not just row

Can't see why you use ctr at all, why not just row

Honestly, I also don't know why I used it haha! gah >.< I think it's because I was trying a lot of diff. for loops and I got confused.. anyway, thank you for pointing it out :)

Edited 4 Years Ago by Ashenvale

This question has already been answered. Start a new discussion instead.