Results 1 to 8 of 8
  1. #1
    gable.able is offline Member
    Join Date
    Mar 2012
    Posts
    10
    Rep Power
    0

    Post Help with Arrays!!

    How do you write a program that randomly inputs 20 numbers from 1 to 10 into a 2-D array of 5 rows and 4 columns. I want it to output the 2-D array, the sum of each row storing numbers in a parallel array and sum up each column storing number in a parallel array.

    Im having trouble with parallel arrays!

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,874
    Rep Power
    8

    Default Re: Help with Arrays!!

    Which part of this are you having trouble with? Break your problem up into separate pieces, and then focus on each individual problem by itself. Create an SSCCE that demonstrates one problem at a time, ask a specific technical question, and we'll go from there.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    gable.able is offline Member
    Join Date
    Mar 2012
    Posts
    10
    Rep Power
    0

    Default Re: Help with Arrays!!

    I actually need to add a method into this.... i need this exactly how it is but with the use of a method

    import java.io.*;
    import java.util.Random;

    public class arrays
    {

    public static void main (String[] args)
    {
    System.out.println("Project 3");

    int array[][] = new int[5][4];
    Random randomizer = new Random();
    for (int i = 0; i < array.length; i++){
    for (int j = 0; j < array[i].length; j++){
    array[i][j] = randomizer.nextInt(11);
    }
    }
    System.out.print("2D Array elements: ");
    for (int i = 0; i < array.length; i++){
    for (int j = 0; j < array[i].length; j++){
    System.out.print(" " +array[i][j]+ ","); //Output 2D Array
    }
    }

    int[] sumOfRows = new int[array.length];
    int[] sumOfColumns = new int[array[0].length];

    int sumR = 0;
    int sumC = 0;
    for(int row = 0; row < array.length; row++){
    for(int col = 0; col < array[0].length; col++){ //Sum of rows
    sumR += array[row][col];
    }
    sumOfRows[row] = sumR;
    }
    System.out.println(" ");
    System.out.println("Sum of Rows: " + sumR);

    for (int col = 0; col < array.length; col++){
    for (int row = 0; row < array[row].length; row++){
    sumC += array[col][row];
    }
    sumOfColumns[col] = sumC;
    }
    System.out.println("Sum of Columns:" + sumC);

    }
    }

  4. #4
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,874
    Rep Power
    8

    Default Re: Help with Arrays!!

    When posting code, please use the code tags. Unformatted code is very hard to read.

    I still don't understand what your actual question is.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  5. #5
    gable.able is offline Member
    Join Date
    Mar 2012
    Posts
    10
    Rep Power
    0

    Default Re: Help with Arrays!!

    import java.io.*;
    import java.util.Random;

    Java Code:
     
    public class arrays
    {
    
      public static void main (String[] args)
    {
        System.out.println("Project 3");
    
         int array[][] = new int[5][4];
         Random randomizer = new Random();
         for (int i = 0; i < array.length; i++){
            for (int j = 0; j < array[i].length; j++){
                array[i][j] = randomizer.nextInt(11);
            }
         }
        System.out.print("2D Array elements: ");
         for (int i = 0; i < array.length; i++){
            for (int j = 0; j < array[i].length; j++){
                System.out.print(" " +array[i][j]+ ",");    //Output 2D Array
            }
         }
    
        int[] sumOfRows = new int[array.length];
        int[] sumOfColumns = new int[array[0].length];
    
        int sumR = 0;
        int sumC = 0;
        for(int row = 0; row < array.length; row++){
            for(int col = 0; col < array[0].length; col++){     //Sum of rows
            sumR += array[row][col];
            }
        sumOfRows[row] = sumR;
        }
        System.out.println(" ");
        System.out.println("Sum of Rows: " + sumR);
    
    for (int col = 0; col < array.length; col++){
            for (int row = 0; row < array[row].length; row++){
                sumC += array[col][row];
                }
        sumOfColumns[col] = sumC;
        }
        System.out.println("Sum of Columns:" + sumC);
    
     }
    }
    Sorry, idk if that worked?

  6. #6
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,874
    Rep Power
    8

    Default Re: Help with Arrays!!

    Okay, and which one of your problems are you having trouble with?
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  7. #7
    gable.able is offline Member
    Join Date
    Mar 2012
    Posts
    10
    Rep Power
    0

    Default Re: Help with Arrays!!

    Just incorporating a method into it. Like what method should I use to have the same outcome of the code when I run it?

  8. #8
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,874
    Rep Power
    8

Similar Threads

  1. Casting Enum Type arrays to object type arrays
    By nmvictor in forum Advanced Java
    Replies: 4
    Last Post: 02-17-2012, 12:49 PM
  2. Need help with arrays
    By gretaks in forum Advanced Java
    Replies: 24
    Last Post: 09-14-2011, 06:16 AM
  3. arrays and multidimensional arrays
    By belfast09 in forum New To Java
    Replies: 5
    Last Post: 06-14-2011, 01:28 PM
  4. store array of arrays in array of arrays
    By joost_m in forum New To Java
    Replies: 4
    Last Post: 04-19-2010, 10:32 AM
  5. Arrays.sort... why sorting all arrays in class?
    By innspiron in forum New To Java
    Replies: 6
    Last Post: 03-23-2010, 01:40 AM

Tags for this Thread

Posting Permissions

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