locked
Creating Multiplication table using two-dimensional array.

    Question

  • i need a program of multiplication table using two-dimensional array.

    thanks.

    Jes

    Monday, July 30, 2012 12:06 PM

Answers

  • Hi

    filling the table  - more or less based on your own code ;-)
                const int extent = 10; //or 100, or 1000 or ...
                int[,] multiplicationTable = new int[extent, extent];
                for (int x = 1; x < extent + 1; x++)
                    for (int y = 1; y < extent + 1; y++)
                        multiplicationTable[x - 1, y - 1] = x * y;

    Result:

      1   2   3   4   5   6   7   8   9  10
      2   4   6   8  10  12  14  16  18  20
      3   6   9  12  15  18  21  24  27  30
      4   8  12  16  20  24  28  32  36  40
      5  10  15  20  25  30  35  40  45  50
      6  12  18  24  30  36  42  48  54  60
      7  14  21  28  35  42  49  56  63  70
      8  16  24  32  40  48  56  64  72  80
      9  18  27  36  45  54  63  72  81  90
     10  20  30  40  50  60  70  80  90 100

    Chris






    • Edited by Christos Basedov Monday, July 30, 2012 4:39 PM
    • Marked as answer by jes88 Tuesday, July 31, 2012 8:13 AM
    Monday, July 30, 2012 3:53 PM

All replies

  • class Program
        { 
            static void Main(string[] args)
            {
                int[][] numbers = new int[9][];
                for (int line = 1; line <=9; line++)
                {
                    numbers[line-1]=new int[line];
     
                    for (int col = 1; col <=line; col++)
                    {
                        numbers[line - 1][col - 1] = line * col;
                    }
                }
     
                for (int line = 1; line <= 9; line++)
                {
                    for (int col = 1; col <= line; col++)
                    {
                        Console.Write((col + "*" + line + "=" + numbers[line - 1][col - 1]).PadRight(8));
                    }
                    Console.WriteLine();
                }
            }
        }

    下载MSDN桌面工具(Vista,Win7)
    我的博客园
    慈善点击,点击此处

    • Proposed as answer by Norkk Monday, July 30, 2012 12:37 PM
    Monday, July 30, 2012 12:34 PM
  •  

           

    its like this output, but i need the program created in two-dimensional array form

           

            static void Main(string[] args)

            {

               int prod = 0;

                for (int row = 1; row <= 10; row++)
                {
                    for (int col = 1; col <= 10; col++)
                    {
                        prod = row * col;
                        Console.Write(prod + "\t");
                    }
                    Console.WriteLine();

                }
                Console.ReadLine();

                    
    Monday, July 30, 2012 1:22 PM
  • Hi,

    I didnt really understand you. Do you want to have numbers in 2d array, like:

     int[,] array2Da = new int[10, 2] { { 1, 2 }, { 3, 4 }, { 5, 6 }, { 7, 8 }, { 9, 10 }, { 11, 12 }, { 13, 14 }, { 15, 16 }, { 17, 18 }, { 19, 20 } };
                for (int i = 0; i < array2Da.GetLength(1); i++)
                {
                    for (int j = 0; j < array2Da.GetLength(0); j++)
                    {
                        Console.WriteLine(string.Format("{0}\t", i * j));
                    }
                }
    


    Mitja

    Monday, July 30, 2012 1:31 PM
  • Hi

    filling the table  - more or less based on your own code ;-)
                const int extent = 10; //or 100, or 1000 or ...
                int[,] multiplicationTable = new int[extent, extent];
                for (int x = 1; x < extent + 1; x++)
                    for (int y = 1; y < extent + 1; y++)
                        multiplicationTable[x - 1, y - 1] = x * y;

    Result:

      1   2   3   4   5   6   7   8   9  10
      2   4   6   8  10  12  14  16  18  20
      3   6   9  12  15  18  21  24  27  30
      4   8  12  16  20  24  28  32  36  40
      5  10  15  20  25  30  35  40  45  50
      6  12  18  24  30  36  42  48  54  60
      7  14  21  28  35  42  49  56  63  70
      8  16  24  32  40  48  56  64  72  80
      9  18  27  36  45  54  63  72  81  90
     10  20  30  40  50  60  70  80  90 100

    Chris






    • Edited by Christos Basedov Monday, July 30, 2012 4:39 PM
    • Marked as answer by jes88 Tuesday, July 31, 2012 8:13 AM
    Monday, July 30, 2012 3:53 PM
  • Hi,

    Looks like homework. I always suggest to try this by yourself and ask about a particular point where you are stuck so that you learn something in the process instead of copying/pasting some code.

    Have you tried to just declare a two dimensional array ? Where are you stuck ? Else see other responses for a ready to consume solution...


    Please always mark whatever response solved your issue so that the thread is properly marked as "Answered".

    Monday, July 30, 2012 4:07 PM
  • Hi ies88:)

    Mine is working properly……And you mean two demision instead of two loops.

    Plz run my program and it will generate the multipliation table.


    下载MSDN桌面工具(Vista,Win7)
    我的博客园
    慈善点击,点击此处

    Tuesday, July 31, 2012 2:05 AM
  • It's a jagged array which is similar to but not exactly a multidimensional array.

    Tuesday, July 31, 2012 2:56 AM