locked
Using for-loop to create a multiplication table (richtextbox)

    Question

  • Hello all,

     

    I am at my wit end here, have been wrestling with this problem for 4 hours but unable to squeeze out a proper line of code to excecute the design result. Please help me

     

    Exercise:

     

    Generate a multiplication table (sample below) up to 15 x 15, using two for-loop (one inside the other) to generate the products.

     

    - If the product is divisible by 6 display an asterisk (*) instead of the number (use the % operator and compare result with zero)

     

    - initially get one for-loop working to calculate a single row, then wrap it inside another.

    The header row "1 2 3 4 5..." can be a fixed string - don't have to created using for loop

    MUST use TWO for-loops, one inside the other to calculate the product

     

    MULTIPLICATION TABLE
    1 2 3 4 5 6
    1 1 2 3 4 5 6
    2 2 4 6 8 10 12
    3 3 6 9 12 15 18
    4 4 8 12 16 20 24

     

    I only manage to do the following so far:

     

            private void button1_Click(object sender, EventArgs e)

            {

                for (int row = 1; row <= 15; row++)

     

                {

                    richTextBox1.AppendText(Convert.ToString(row) + "\t");       

     

    Which produced the 1 line of row, from 1 to 15

     

    Please help, I know I have to declare 2 or more of the following (int table; or int x, ySmile then assign some value but I couldn't figure out how

     

    Thanks a lot in advance

    Thursday, April 17, 2008 5:53 AM

Answers

  • Whirlwind,

    you are on the right track. The first step is to add another for loop nested within the first one.

    Something like this:

     

    for (int row = 1; row <= 15; row++) {

      for (int col = 1; col <= 15; col++) {

        int product = row * col;

        richTextBox1.AppendText (string.Format ("{0}\t", product));

      }

      richTextBox1.AppendText ("\n");

    }

     

    From here you will have to check the divisibility, add the row and colum header etc, but this should be a leg-up

     

    Good luck

    --mc

    Thursday, April 17, 2008 10:39 PM
  • In reverse order:

     

    string is the same as String (actually, System.String). It is not a different type, it's just an alias provided by the C# compiler for cosmetic reasons. The same holds for int (System.Int32), float (System.Single), double (System.Double) and several others.

    For this reason, string.Format() is exactly the same as String.Format() (or System.String.Format()); which of the two you should use is largely a matter of personal taste: I prefer to use the alias for consistency, but that's really up to you.

     

    string.Format, as other methods (Console.WriteLine, StringBuilder.AppendFormat and a few others) requires that you provide a format string and a variable number of values. Your format string must contain placeholders for the values which will be automatically converted to string and will be numbered starting from "0".

    For instance:

     

    string result = string.Format ("Hello {0}, today it's {1}", "Whirlwind", "Friday");

    would produce the string "Hello Whirlwind, today it's Friday"

     

    Each placeholder can also contain the same formatting you can provide with the ToString method, so you can write:

     

    string result = string.Format ("This is year {0:yyyy}", DateTime.Now);

    would produce the string "This is year 2008".

     

    There's a lot more about formatting, but it would be too much to write here... you will find plenty of information in the documentation of string.Format() and its links.

     

    HTH

    --mc

    Friday, April 18, 2008 5:37 PM

All replies

  • Whirlwind,

    you are on the right track. The first step is to add another for loop nested within the first one.

    Something like this:

     

    for (int row = 1; row <= 15; row++) {

      for (int col = 1; col <= 15; col++) {

        int product = row * col;

        richTextBox1.AppendText (string.Format ("{0}\t", product));

      }

      richTextBox1.AppendText ("\n");

    }

     

    From here you will have to check the divisibility, add the row and colum header etc, but this should be a leg-up

     

    Good luck

    --mc

    Thursday, April 17, 2008 10:39 PM
  • Thank you Mario for your help.

     

    That clear a few things for me. But I still have a few questions to ask: 

     

    Would you please explain what the {0} meant?

     

    Is

    string.Format

     

    the same as

    Convert.ToString

     

    ?

     

     

    Friday, April 18, 2008 4:58 AM
  • also, what is the different between

     

    string.Format

     

    and

     

    String.Format

     

     

    Or only one of the above combination applied to this case?

     

    Friday, April 18, 2008 5:21 AM
  • In reverse order:

     

    string is the same as String (actually, System.String). It is not a different type, it's just an alias provided by the C# compiler for cosmetic reasons. The same holds for int (System.Int32), float (System.Single), double (System.Double) and several others.

    For this reason, string.Format() is exactly the same as String.Format() (or System.String.Format()); which of the two you should use is largely a matter of personal taste: I prefer to use the alias for consistency, but that's really up to you.

     

    string.Format, as other methods (Console.WriteLine, StringBuilder.AppendFormat and a few others) requires that you provide a format string and a variable number of values. Your format string must contain placeholders for the values which will be automatically converted to string and will be numbered starting from "0".

    For instance:

     

    string result = string.Format ("Hello {0}, today it's {1}", "Whirlwind", "Friday");

    would produce the string "Hello Whirlwind, today it's Friday"

     

    Each placeholder can also contain the same formatting you can provide with the ToString method, so you can write:

     

    string result = string.Format ("This is year {0:yyyy}", DateTime.Now);

    would produce the string "This is year 2008".

     

    There's a lot more about formatting, but it would be too much to write here... you will find plenty of information in the documentation of string.Format() and its links.

     

    HTH

    --mc

    Friday, April 18, 2008 5:37 PM