6 Replies - 3750 Views - Last Post: 14 October 2011 - 06:54 AM Rate Topic: -----

#1 Aramyth  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 51
  • Joined: 14-January 10

Storing User Input in a 2D Array

Posted 10 October 2011 - 08:55 PM

I have a form, where the user can input 6 numbers (matrix), and I want to store them in a 2D array so that later I can perform different matrix math with this array.

I'm confused exactly though as to how I would assign the input to the right elements in the array.

 int[,] matrixInput = new int[2,3];

matrixInput[0, 0] = Int32.Parse(this.inputX1.Text);
matrixInput[0, 1] = Int32.Parse(this.inputY1.Text);
matrixInput[0, 3] = Int32.Parse(this.equalsOne.Text); ;

matrixInput[1, 0] = Int32.Parse(this.inputX2.Text);
matrixInput[1, 1] = Int32.Parse(this.inputY2.Text);
matrixInput[1, 2] = Int32.Parse(this.equalsTwo.Text);


Also what's the difference between int[,] and int[][]? I understand that int[,] is jagged but that's about it.

Thanks in advance.

This post has been edited by Aramyth: 10 October 2011 - 09:00 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Storing User Input in a 2D Array

#2 Robin19  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 272
  • View blog
  • Posts: 552
  • Joined: 07-July 10

Re: Storing User Input in a 2D Array

Posted 11 October 2011 - 06:16 AM

A jagged array (int[][]) is an array of arrays. A multidimensional array (int[,]) is an array that has more than one direction. You had them backwards.
int[][] matrixInput = new int[2][]; // make an array of 2 arrays
// this line fails!!!
matrixInput[0][0] = Int32.Parse(this.inputX1.Text);
// it fails because matrixInput[0] is still a null array.
// you need to initialize the arrays first
for(int i = 0; i < matrixInput.GetLength(0); i++)
{
   matrixInput[i] = new int[3];
}

As you can see, a jagged array takes more effort to initialize. You can use either, but I only use a jagged array when I need the different array sizes.

Edit: You can also look into the Matrix class to do the math for you.

This post has been edited by Robin19: 11 October 2011 - 06:19 AM

Was This Post Helpful? 1
  • +
  • -

#3 assiance  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 7
  • Joined: 23-October 08

Re: Storing User Input in a 2D Array

Posted 11 October 2011 - 08:24 AM

matrixInput[0, 3] = Int32.Parse(this.equalsOne.Text);

should be


matrixInput[0, 2] = Int32.Parse(this.equalsOne.Text);
Was This Post Helpful? 1
  • +
  • -

#4 brep  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 23
  • View blog
  • Posts: 169
  • Joined: 26-August 11

Re: Storing User Input in a 2D Array

Posted 11 October 2011 - 11:16 AM

Also, two nested for loops might be useful (except if it matters where you place the data in the array, then you would have to do it manually instead of loops).
Example:
for(int i=0; i<=2; i++) {
    for(int j=0; j<=3; j++) {
    //array is assigned values here. (your_array[i, j] = 67, or something similar)

}
}


Something like that might suffice for two-dimensional arrays.
Hope this helps
preb

This post has been edited by preb: 11 October 2011 - 11:19 AM

Was This Post Helpful? 1
  • +
  • -

#5 Aramyth  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 51
  • Joined: 14-January 10

Re: Storing User Input in a 2D Array

Posted 12 October 2011 - 08:48 AM

Yes it matters where the data is stored in the array.

I'll be back to working at this in a few hours, so we'll see how it goes. Thanks for all the help.
Was This Post Helpful? 0
  • +
  • -

#6 Aramyth  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 51
  • Joined: 14-January 10

Re: Storing User Input in a 2D Array

Posted 12 October 2011 - 06:33 PM

I wondered if I should start a new thread, but it's basically a part two to this question.

I'm trying to write an algorithm for reduced row echelon form and I'm drawing blanks on how to start it. I'm wonderibg how I'll look through the rows and do the math properly... :/ Any tips? And sorry if this sounds lame, but I just learned reduced row echelon form.
Was This Post Helpful? 0
  • +
  • -

#7 Robin19  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 272
  • View blog
  • Posts: 552
  • Joined: 07-July 10

Re: Storing User Input in a 2D Array

Posted 14 October 2011 - 06:54 AM

There are two ways I would do it. My basic assumption is that one is dealing with rows of numbers at a time.

Use a balanced jagged array. You can access each row by itself (myMatrix[i] = new int[5]) and do checks (what is the first column that contains a non-zero?).

The other option would be to make a Row class. The class can have properties (IsNonZero, FirstNonZeroColumn, etc). Then just make an array of Rows and ask each class to use in sorting.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1