Results 1 to 2 of 2
  1. #1
    spsu_girl is offline Member
    Join Date
    Feb 2013
    Posts
    1
    Rep Power
    0

    Red face Adding the sum of the columns and rows in a 2d array

    So I have the basic part of the array written. We had to read in information from a file (attached) and then post it into a table using columns and rows. Now all I need is to get the sum of the columns and rows. Here are the directions:

    •Modify the program so that the total votes cast per state are shown (i.e., add a column to each row summing the total votes cast for all candidates per state)
    •Modify the program so that the total votes per candidate are shown (i.e., add a last row showing total votes received for all three columns of votes.


    Java Code:
        public static void main(String[] args) throws IOException
        {
            // TODO code application logic here
            File election = new File("voting_2008.txt");
            Scanner sc = new Scanner(election);
            
            String[] states = new String[51];
            int[][] votes = new int [51][3];
                         
            for(int s=0; s < 51; s++)
            {
                states[s] = sc.nextLine();
            }
            
            for(int c=0; c < 3; c++)
            {
                for(int s=0; s < 51; s++)
                {
                    votes[s][c] = sc.nextInt();
                    
                }
                
            }
                    
            Formatter fmt = new Formatter();
            fmt.format("%20s%12s%12s%12s", "State", "Obama", "McCain", "Other");
            System.out.println(fmt);
            for(int s=0; s<51; s++)
            {
                fmt = new Formatter();
                fmt.format("%20s", states[s]);
                System.out.print(fmt);
                for(int c=0; c < 3; c++)
                {
                    fmt = new Formatter();
                    fmt.format("%12d", votes[s][c]);
                    System.out.print(fmt);
                }
                System.out.println();
            }          
        }
    }
    Attached Files Attached Files

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,453
    Rep Power
    20

    Default Re: Adding the sum of the columns and rows in a 2d array

    Moved from Java2D

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

Similar Threads

  1. Replies: 0
    Last Post: 02-27-2012, 10:13 PM
  2. Replies: 8
    Last Post: 05-16-2011, 07:35 AM
  3. JSlider change JPanel[][] rows and columns
    By Charlie161 in forum AWT / Swing
    Replies: 3
    Last Post: 02-28-2011, 06:44 PM
  4. Need a loop for rows and columns
    By Ceasar in forum New To Java
    Replies: 4
    Last Post: 10-09-2009, 04:03 PM
  5. Two diM aRRay and add rows and columns....
    By filly444 in forum New To Java
    Replies: 2
    Last Post: 08-30-2008, 06:24 PM

Posting Permissions

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