Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Stumped - How do I code this loop...

Stumped - How do I code this loop...

Scheduled Pinned Locked Moved C#
question
16 Posts 6 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • B BillWoodruff

    +5 for this wonderful code "haiku" :)

    “I have diligently numbered the days of pure and genuine happiness which have fallen to my lot: They amount to 14.” Abd-Ar Rahman III, Caliph of Cordoba, circa 950CE.

    C Offline
    C Offline
    CPallini
    wrote on last edited by
    #7

    Thank you.

    THESE PEOPLE REALLY BOTHER ME!! How can they know what you should do without knowing what you want done?!?! -- C++ FQA Lite

    1 Reply Last reply
    0
    • C CPallini

      List<int> positions = new List<int> { 1, 2, 3, 4, 5 };
      for (int n = 1, p1 = 0, p2 = 4; n <= 10; ++n, ++p1, ++p2)
      {
      p1 %= positions.Count;
      p2 %= positions.Count;
      Console.WriteLine("{0,2} {1} {2}", n, positions[p1], positions[p2]);
      }

      THESE PEOPLE REALLY BOTHER ME!! How can they know what you should do without knowing what you want done?!?! -- C++ FQA Lite

      B Offline
      B Offline
      BillWoodruff
      wrote on last edited by
      #8

      I hope this doesn't sound picky-picky, but I kept studying the line of code that writes to the Console: Console.WriteLine("{0,2} {1} {2}", n, positions[p1], positions[p2]); Thinking that {0,2} did some exotic thing I had never seen before, but it appears it actually does nothing, and changing it to {0} produces no change in the output. Or, am I missing something ? Once again, thanks for the great code example: you've expanded my understanding of what a C# 'for loop can do ! thanks, Bill

      “I have diligently numbered the days of pure and genuine happiness which have fallen to my lot: They amount to 14.” Abd-Ar Rahman III, Caliph of Cordoba, circa 950CE.

      P Richard DeemingR 2 Replies Last reply
      0
      • B BillWoodruff

        I hope this doesn't sound picky-picky, but I kept studying the line of code that writes to the Console: Console.WriteLine("{0,2} {1} {2}", n, positions[p1], positions[p2]); Thinking that {0,2} did some exotic thing I had never seen before, but it appears it actually does nothing, and changing it to {0} produces no change in the output. Or, am I missing something ? Once again, thanks for the great code example: you've expanded my understanding of what a C# 'for loop can do ! thanks, Bill

        “I have diligently numbered the days of pure and genuine happiness which have fallen to my lot: They amount to 14.” Abd-Ar Rahman III, Caliph of Cordoba, circa 950CE.

        P Offline
        P Offline
        Pete OHanlon
        wrote on last edited by
        #9

        It's putting the space before the single digit numbers. Try changing it {0,4} to see the effect in greater detail.

        C B 2 Replies Last reply
        0
        • B BillWoodruff

          I hope this doesn't sound picky-picky, but I kept studying the line of code that writes to the Console: Console.WriteLine("{0,2} {1} {2}", n, positions[p1], positions[p2]); Thinking that {0,2} did some exotic thing I had never seen before, but it appears it actually does nothing, and changing it to {0} produces no change in the output. Or, am I missing something ? Once again, thanks for the great code example: you've expanded my understanding of what a C# 'for loop can do ! thanks, Bill

          “I have diligently numbered the days of pure and genuine happiness which have fallen to my lot: They amount to 14.” Abd-Ar Rahman III, Caliph of Cordoba, circa 950CE.

          Richard DeemingR Offline
          Richard DeemingR Offline
          Richard Deeming
          wrote on last edited by
          #10

          The MSDN documentation is reasonably clear (for once!):

          http://msdn.microsoft.com/en-us/library/system.string.format%28v=vs.110%29.aspx#FormatItem[^]

          A format item has this syntax:

          {index[,alignment][:formatString]}

          ... alignment Optional. A signed integer that indicates the total length of the field into which the argument is inserted and whether it is right-aligned (a positive integer) or left-aligned (a negative integer). If you omit alignment, the string representation of the corresponding argument is inserted in a field with no leading or trailing spaces.


          "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

          "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

          C B 2 Replies Last reply
          0
          • P Pete OHanlon

            It's putting the space before the single digit numbers. Try changing it {0,4} to see the effect in greater detail.

            C Offline
            C Offline
            CPallini
            wrote on last edited by
            #11

            Exactly.

            THESE PEOPLE REALLY BOTHER ME!! How can they know what you should do without knowing what you want done?!?! -- C++ FQA Lite

            1 Reply Last reply
            0
            • Richard DeemingR Richard Deeming

              The MSDN documentation is reasonably clear (for once!):

              http://msdn.microsoft.com/en-us/library/system.string.format%28v=vs.110%29.aspx#FormatItem[^]

              A format item has this syntax:

              {index[,alignment][:formatString]}

              ... alignment Optional. A signed integer that indicates the total length of the field into which the argument is inserted and whether it is right-aligned (a positive integer) or left-aligned (a negative integer). If you omit alignment, the string representation of the corresponding argument is inserted in a field with no leading or trailing spaces.


              "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

              C Offline
              C Offline
              CPallini
              wrote on last edited by
              #12

              Richard Deeming wrote:

              he MSDN documentation is reasonably clear (for once!):

              :-)

              THESE PEOPLE REALLY BOTHER ME!! How can they know what you should do without knowing what you want done?!?! -- C++ FQA Lite

              1 Reply Last reply
              0
              • P Pete OHanlon

                It's putting the space before the single digit numbers. Try changing it {0,4} to see the effect in greater detail.

                B Offline
                B Offline
                BillWoodruff
                wrote on last edited by
                #13

                Thanks Pete !

                “I have diligently numbered the days of pure and genuine happiness which have fallen to my lot: They amount to 14.” Abd-Ar Rahman III, Caliph of Cordoba, circa 950CE.

                1 Reply Last reply
                0
                • Richard DeemingR Richard Deeming

                  The MSDN documentation is reasonably clear (for once!):

                  http://msdn.microsoft.com/en-us/library/system.string.format%28v=vs.110%29.aspx#FormatItem[^]

                  A format item has this syntax:

                  {index[,alignment][:formatString]}

                  ... alignment Optional. A signed integer that indicates the total length of the field into which the argument is inserted and whether it is right-aligned (a positive integer) or left-aligned (a negative integer). If you omit alignment, the string representation of the corresponding argument is inserted in a field with no leading or trailing spaces.


                  "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

                  B Offline
                  B Offline
                  BillWoodruff
                  wrote on last edited by
                  #14

                  Thanks, Richard !

                  “I have diligently numbered the days of pure and genuine happiness which have fallen to my lot: They amount to 14.” Abd-Ar Rahman III, Caliph of Cordoba, circa 950CE.

                  1 Reply Last reply
                  0
                  • C CPallini

                    If n starts with 1, then

                    p1 = (n-1) % positions.Count;
                    p2 = (positions.Count + n - 2) % positions.Count;

                    THESE PEOPLE REALLY BOTHER ME!! How can they know what you should do without knowing what you want done?!?! -- C++ FQA Lite

                    A Offline
                    A Offline
                    Andy_L_J
                    wrote on last edited by
                    #15

                    Here what I ended up using:

                    public List GetPositions(int n, int posCount)
                    {
                    int pos1 = n % posCount;
                    int pos2 = pos1 == 0 ? posCount - 1 : pos1 - 1;
                    return new List{pos1, pos2};
                    }
                    ...
                    int n = 0;
                    List positions = new List{0,1,2,3,4};
                    while( n < 10)
                    {
                    List res = GetPositions(n, positions.Count)
                    Console.WriteLine("Count{0}: Pos1 {1} - Pos2 {2}", n, res[0], res[1]);
                    n++;
                    }

                    Thanks to you and G for your help clearing the fog!

                    I don't speak Idiot - please talk slowly and clearly "I have sexdaily. I mean dyslexia. Fcuk!" Driven to the arms of Heineken by the wife

                    C 1 Reply Last reply
                    0
                    • A Andy_L_J

                      Here what I ended up using:

                      public List GetPositions(int n, int posCount)
                      {
                      int pos1 = n % posCount;
                      int pos2 = pos1 == 0 ? posCount - 1 : pos1 - 1;
                      return new List{pos1, pos2};
                      }
                      ...
                      int n = 0;
                      List positions = new List{0,1,2,3,4};
                      while( n < 10)
                      {
                      List res = GetPositions(n, positions.Count)
                      Console.WriteLine("Count{0}: Pos1 {1} - Pos2 {2}", n, res[0], res[1]);
                      n++;
                      }

                      Thanks to you and G for your help clearing the fog!

                      I don't speak Idiot - please talk slowly and clearly "I have sexdaily. I mean dyslexia. Fcuk!" Driven to the arms of Heineken by the wife

                      C Offline
                      C Offline
                      CPallini
                      wrote on last edited by
                      #16

                      You are welcome.

                      THESE PEOPLE REALLY BOTHER ME!! How can they know what you should do without knowing what you want done?!?! -- C++ FQA Lite

                      1 Reply Last reply
                      0
                      Reply
                      • Reply as topic
                      Log in to reply
                      • Oldest to Newest
                      • Newest to Oldest
                      • Most Votes


                      • Login

                      • Don't have an account? Register

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • World
                      • Users
                      • Groups