Creating an array (matrix) that is using user inputs

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Luffy0114
    New Member
    • Feb 2016
    • 1

    Creating an array (matrix) that is using user inputs

    Alright, so I just start self-learning for computer science over Internet, and there is a question that I don't know how to do it. Please help by giving me a hint or something.

    Question:
    Create an array (matrix) by asking the users for a number of rows and columns. Then ask the users to input each value for the matrix. Keep all the numbers as Integer.

    Solve:
    So I start by asking the users a number of rows and columns (let's say 4x4), but then I don't know how to ask user to input each value for the matrix. Thank you
  • chaarmann
    Recognized Expert Contributor
    • Nov 2007
    • 785

    #2
    You just use a double for-loop. The inner iterates over the columns, the outer over the rows.
    Inside your double loop, you read the user input in a variable, the same way you do it when you ask for number of rows.
    Then you copy the variable into the array:
    Code:
    int rowCount = ... // read number of rows from user
    int columnCount = ... // read number of columns from user
    int[][] array = new int[rowCount][columnCount];
    for(int row = 0; ...)
    {
       for(int column = 0; ...)
       {
        ... // tell user which row/column to input
        int userInput = ... // read in value from user
        array[row][column] = userInput;
       }
    }

    Comment

    Working...