0

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.

2
  • And what is not working? Does it show some results in the textbox? Commented Nov 12, 2013 at 8:33
  • I realize 4x4 is not big table so I increased it to 12x12 now. I am able to find any value under 10 and my current logic shows the value in the table once. So lets say if I search for 12, I know 12 appears more than just once yet my logic only shows it as if it only appears once in the rows and columns. Thank you. Commented Nov 12, 2013 at 8:50

2 Answers 2

2

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();
    }
}
Sign up to request clarification or add additional context in comments.

3 Comments

This is more a comment than an answer. You don't suggest any fix but only provide review comments
It is a goodpoint could use GetUpperBound(1) for the second dimension, the columns loop.
Thank you everyone for all your input. @Noctis, I thank you very much for pointing me in right direction. It all now makes and sense and works as expected.
1

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));
        }
    }
}

5 Comments

Running your code on his 12x12 and looking for 12 returns this array: (0, 11) (1, 5) (2, 3) (3, 2) (5, 1) (11, 0)
That seems correct! Those cells would correspond to 1 x 12, 2 x 6, 3 x 4, 4 x 3, 6 x 2 and 12 x 1. All of which equal 12.
that is correct :). I just added the output since he updated his question from 4 to 12. I changed it a bit (variable names), and added to my answer (with a big IT'S NOT ORIGINAL bold header ...) hope that's ok with you ...
@Noctis: Of course! :) Not sure you meant to paste in the card-shuffling code too? ;)
Argh ... yep yep... was from a different answer ... removed it ... my bad :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.