I would highly appreciate it if someone can point me in the right direction. For the past couple of months I have been learning C# using Murach's C# 2013 book, it is a good book, however at time, it lacks certain details. I have been working with Arrays and finally got going with multidimensional arrays, I have written a simple logic where a multidimensional array is declared and populated as (4x4) multiplication table using nested "For Loop" which work as expected. The issue is that I am now trying to search for a given int value within the 2d array by using a nested "For Loop", and I would like to find the int value by looping through all the rows and columns and getting its location using array indexes. I have been at for a couple of days, I have searching online but wasn't able to find a solid direction.
Objective: once the multiplication table is populated, now I would like to located "9" in all the columns and rows.
It would be fantastic if someone can get me going with it. Here is my code.
//CONSTANT ARRAY LENGTH
const int multiTable = 4;
//ARRAY
int [ , ] multiplicationTableArr =
new int[multiTable, multiTable]; // 4 x 4 table
//MULTIPLICATION METHOD
private void MultiplicationTable
{
int r; //ROW
int c; //COLUMN
int result;
for (r = 0; r < multiplicationTableArr.GetUpperBound(0); r++)
{
//NESTED FOR LOOP
for (c = 0; c < multiplicationTableArr.GetUpperBound(0); c++)
{
result = (r + 1) * (c + 1);
multiplicationTableArr[r, c] = result;
break;
}//NESTED FOR LOOP ENDS
}
}
// SEACHFORVALUE METHOD
private void seachForValue()
{
int r; //ROW
int c; //COLUMN
int intSearchNumber;
txtTable.Clear(); //clear the text box
intSearchNumber = int.Parse(txtSearchNumber.Text);
for (r = 0; r < multiplicationTableArr.GetLength(0); r++)
{
for (c = 0; c < multiplicationTableArr.GetLength(1); c++)
{
if (intSearchNumber == multiplicationTableArr[r,c])
{
txtTable.AppendText(r + ", " + c.ToString());
}
}//NESTED FOR LOOP ENDS
}
}
Thank you.