Note that there are some explanatory texts on larger screens.

plurals
  1. POBug with 2D Array Viewer and Editor. C# WPF
    text
    copied!<p>First time poster so sorry if this is too long, but I've hit a wall and don't know what else to try. </p> <p>I'm making a Matrices Calculator and doing so with C# and WPF. </p> <p>I've been spending the last hour trying to figure out what's wrong with my Array Iterator to view and edit the values in a 2D NxN array. </p> <p>To get a better idea my project so far requires the user to input a size n to make the array. It'll generate a 2D int array of size NxN of 0's and start at the default position of (0, 0). From there the user can edit the value going from Left to Right, Top to Bottom editing the whole array.</p> <p>I have 2 private global ints, one that keeps track of the row position and one that keeps track of the column position and manipulating these numbers to edit that part of the array.</p> <p>The iterations starts off fine going (0, 0) > (0, 1); however instead of going from (0,1) > (0,2) like it should it jumps to (1, 1). I've gone countless times over my if logic and can't find where I'm doing it wrong though. </p> <p>I have yet to test the traversing back right to left, bottom to top yet but since it's logic is nearly identical I'm assuming it'll have the same problems I'm currently having. </p> <p>Many thanks if someone could point me where in my logic it's flawed so I can move onto coding up the other parts. </p> <p>My code is as follows:</p> <pre><code>private int[,] matrix; //Matrix currently being edited private int row; //keeps track of current row position and set to 0 when matrix is made private int col; //Keeps track of current column position and set to 0 when matrix is made private void previousPos_Click(object sender, RoutedEventArgs e) { Console.WriteLine("row " + row + "\r\n col " + col); if (row &lt; 0 &amp;&amp; col &lt; 0) { textBlock1.Text = "No previous values to edit"; row = 0; col = 0; positionDisplay.Text = "" + col + ", " + row; } else if (row &lt; 0 &amp;&amp; col &lt; 3) { setValue(row, col, valueDisplay.Text); row = 2; col -= 1; displayMatrix(matrix); positionDisplay.Text = "" + col + ", " + row; } else { setValue(row, col, valueDisplay.Text); row -= 1; displayMatrix(matrix); positionDisplay.Text = "" + col + ", " + row; } } private void nextPos_Click(object sender, RoutedEventArgs e) { Console.WriteLine("row "+row+"\r\ncol "+col); if (row &gt;= matrix.GetLength(0) &amp;&amp; col &gt;= matrix.GetLength(1)) { textBlock1.Text = "No more values to edit"; row = matrix.GetLength(0) - 1; col = matrix.GetLength(1) - 1; positionDisplay.Text = "" + col + ", " + row; } else if (row &gt;= matrix.GetLength(0) &amp;&amp; col &lt; matrix.GetLength(1)) { setValue(row, col, valueDisplay.Text); col += 1; row = 0; displayMatrix(matrix); positionDisplay.Text = "" + col + ", " + row; } else { setValue(row, col, valueDisplay.Text); row += 1; displayMatrix(matrix); positionDisplay.Text = "" + col + ", " + row; } } public void setValue(int curRow, int curCol, string value) { col = curRow; row = curCol; try { matrix[row, col] = int.Parse(value); } catch(Exception) { string messageBoxText = "Please input a valid number"; string caption = "Warning"; MessageBoxButton button = MessageBoxButton.OK; MessageBoxImage icon = MessageBoxImage.Warning; MessageBox.Show(messageBoxText, caption, button, icon); } } </code></pre> <p>Edit: Fixed a typo I saw and willing to post more/rest of code on request.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload