Simple code, but cannot understand

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Lionheart

    Simple code, but cannot understand

    using System

    namespace ConsoleApplicat ion1

    public class Class

    public static void Main(

    for (int i = 1; i < 10; i++

    for (int j = 1; j < 10; j++

    for (int k = 1; k < 10; k++

    Console.WriteLi ne(i+" "+j+" "+k)








    the running result is
    6 3
    6 3
    6 4
    6 4
    6 4
    6 4
    6 4
    6 4
    6 4
    6 4
    6 4
    6 5
    6 5
    6 5
    6 5
    6 5
    6 5
    6 5
    6 5
    6 5
    6 6
    6 6
    6 6
    6 6
    6 6
    ......

    but i think the result should be like
    1 1
    1 1
    1 1
    .......

    Really confuse about it, thank you so much for help!
  • Morten Wennevik

    #2
    Re: Simple code, but cannot understand

    You got 3 loops, and the innermost (k) one will run every time, the midle
    one (j) will run each time the innermost one is finished, and the outer
    one (i) will run every time the middle one is finished

    so the result will be

    i, j, k
    i, j, k+1
    i, j, k+2
    ....
    i, j+1, k
    i, j+1, k+1
    i, j+1, k+2
    ....
    i, j+2, k
    i, j+2, k+1
    i, j+2, k+2
    ....
    ....
    ....
    ....
    ....
    ....
    i+1, j, k
    i+1, j, k+1
    i+1, j, k+2
    ....
    i+1, j+1, k
    i+1, j+1, k+1
    i+1, j+1, k+2
    ....
    i+1, j+2, k
    i+1, j+2, k+1
    i+1, j+2, k+2
    ....
    ....
    ....
    i+2, j, k
    i+2, j, k+1
    i+2, j, k+2

    and so on.

    The result will indeed be
    1 1 1
    1 1 2
    and so on, but it will write so fast you won't see the first results


    --
    The hotmail account will most likely not be read, so please respond only
    to the news group.

    Comment

    • James

      #3
      RE: Simple code, but cannot understand

      Your correct, the output is
      1 1
      ....
      9 9

      Your problem is your screen buffer size for height is to small. Try setting it to something like 800 to 1000


      ----- Lionheart wrote: ----

      using System

      namespace ConsoleApplicat ion1

      public class Class

      public static void Main(

      for (int i = 1; i < 10; i++

      for (int j = 1; j < 10; j++

      for (int k = 1; k < 10; k++

      Console.WriteLi ne(i+" "+j+" "+k)








      the running result is
      6 3
      6 3
      6 4
      6 4
      6 4
      6 4
      6 4
      6 4
      6 4
      6 4
      6 4
      6 5
      6 5
      6 5
      6 5
      6 5
      6 5
      6 5
      6 5
      6 5
      6 6
      6 6
      6 6
      6 6
      6 6
      .....

      but i think the result should be like
      1 1
      1 1
      1 1
      ......

      Really confuse about it, thank you so much for help!

      Comment

      • Roman S. Golubin

        #4
        Re: Simple code, but cannot understand


        Hello, Lionheart!
        [color=blue]
        > for (int i = 1; i < 10; i++)
        > {[/color]
        ....[color=blue]
        > 6 4 3
        > 6 4 4[/color]
        ....[color=blue]
        >
        > but i think the result should be like:
        > 1 1 1
        > 1 1 2
        > 1 1 3
        > .......
        >
        > Really confuse about it, thank you so much for help![/color]

        The result have more than 900 rows! You must configure your console buffer
        to height near 1000 or more :-)


        --
        Roman S. Golubin
        ICQ UIN 63253392
        golubinr.ANTl_S SPAM@arhcity.ru


        Comment

        Working...