Combustion007
Combustion007

Reputation: 474

Finding an int value within multidimensional array?

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.

Upvotes: 0

Views: 1263

Answers (2)

Noctis
Noctis

Reputation: 11763

One thing that I'm unhappy with is the use of GetUpperBound(0) for both rows and columns, since you're just lucky they are both 4. If you would have different sizes for them, your code would fail.

Having said that, use row and column instead of r & c.

The code seems like it should work ... Put a breakpoint in the txtTable.AppendText... to see if your issue is with the output, or with the logic.

Edit: This is really the above answer, with my variables names and output changes
Do not upvote it for being the right answer, since you should upvote the above answer.

Edited for ease of read / better use of variable names, and clearer output results:

internal class Program
{
    public static void Main(string[] args)
    {
        var test = new ConsoleTest();
        var v = test.seachForValue(12);
        Console.WriteLine(v);


        Console.ReadLine();
    }
}

public class ConsoleTest
{
    public ConsoleTest()
    {
        MultiplicationTable();
    }

    //CONSTANT ARRAY LENGTH
    public const int TableSize = 12;
    //ARRAY
    public int[,] multiplicationTableArr = new int[TableSize, TableSize];  


    //MULTIPLICATION METHOD
    // this will intialize your array to your multiplication table 
    private void MultiplicationTable()
    {
        for (int row = 0; row < TableSize; row++)
        {
            //NESTED FOR LOOP
            for (int column = 0; column < TableSize; column++)
            {
                multiplicationTableArr[row, column] = (row + 1) * (column + 1);
            }//NESTED FOR LOOP ENDS
        }
    }

    // SEACHFORVALUE METHOD
    public string seachForValue(int intSearchNumber)
    {
        var result = new StringBuilder();

        for (int row = 0; row < TableSize; row++)
        {
            for (int col = 0; col < TableSize; col++)
            {
                if (intSearchNumber == multiplicationTableArr[row, col])
                {
                    result.AppendLine("(" + row + ", " + col + ") -> " + (row + 1) + "*" + (col + 1 )+ "=" + intSearchNumber);
                }
            }//NESTED FOR LOOP ENDS
        }

        return result.ToString();
    }
}

Upvotes: 2

Baldrick
Baldrick

Reputation: 11840

There are few issues with the code:

  1. MultiplicationTable needs to be a method - it won't compile without a signature
  2. You need to get rid of the 'break' in the inner loop - this will prevent it from running properly - it will break out after the first time through.
  3. In your searchForValue method, you can use multiTable as your table dimension, rather than multiplicationTableArr.GetLength, as it's already defined. If you do use multiplicationTableArr.GetLength, make sure you add 1, or it'll stop short of the last row / column.
  4. You actually need to call MultiplicationTable to populate the table. You can do this from the class constructor.

Here's a console app version of your code, with the corrections above applied to it.

Searching for 9, the output is:

2, 2

The code is:

using System;

namespace MultiplyTest
{
    public class ConsoleTest
    {
        //CONSTANT ARRAY LENGTH
        public const int multiTable = 4;
        //ARRAY
        public int[,] multiplicationTableArr = new int[multiTable, multiTable];  // 4 x 4 table 

        public ConsoleTest()
        {
            MultiplicationTable();
        }

        //MULTIPLICATION METHOD
        private void MultiplicationTable()
        {
            for (int r = 0; r < multiTable; r++)
            {
                //NESTED FOR LOOP
                for (int c = 0; c < multiTable; c++)
                {
                    multiplicationTableArr[r, c] = (r + 1) * (c + 1);
                }//NESTED FOR LOOP ENDS
            }
        }

        // SEACHFORVALUE METHOD
        public string seachForValue(int intSearchNumber)
        {
            var result = string.Empty;

            for (int r = 0; r < multiTable; r++)
            {
                for (int c = 0; c < multiTable; c++)
                {
                    if (intSearchNumber == multiplicationTableArr[r, c])
                    {
                        result = result + r + ", " + c;
                    }
                }//NESTED FOR LOOP ENDS
            }

            return result;
        }
    }

    internal class Program
    {
        public static void Main(string[] args)
        {
            var test = new ConsoleTest();
            Console.WriteLine(test.seachForValue(9));
        }
    }
}

Upvotes: 1

Related Questions