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. I need help with C# loops

I need help with C# loops

Scheduled Pinned Locked Moved C#
csharphelpquestion
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.
  • A Offline
    A Offline
    Alex501
    wrote on last edited by
    #1

    I need to create this program and needs to have if, else statements. Could you please help me? I want to create a program that will look like that? Welcome to HollowRectanglePrinter! How many columns wide should the rectangle be? 6 How many rows tall should the rectangle be? 4 Here you go: ****** * * * * ******

    P P G G 4 Replies Last reply
    0
    • A Alex501

      I need to create this program and needs to have if, else statements. Could you please help me? I want to create a program that will look like that? Welcome to HollowRectanglePrinter! How many columns wide should the rectangle be? 6 How many rows tall should the rectangle be? 4 Here you go: ****** * * * * ******

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

      OK - for a starter if/else statements aren't loops. They're conditional statements. Next, we won't do your homework for you - and this is so obviously a homework assignment. Finally, if you can't solve a basic problem like this quickly then you really shouldn't be aiming at a career in programming.

      Deja View - the feeling that you've seen this post before.

      My blog | My articles

      1 Reply Last reply
      0
      • A Alex501

        I need to create this program and needs to have if, else statements. Could you please help me? I want to create a program that will look like that? Welcome to HollowRectanglePrinter! How many columns wide should the rectangle be? 6 How many rows tall should the rectangle be? 4 Here you go: ****** * * * * ******

        P Offline
        P Offline
        PIEBALDconsult
        wrote on last edited by
        #3

        (I agree that it looks like homework.) Loops? Plural? I can do that without loops. P.S. Without conditionals as well. P.P.S. And no recursion either for that matter.

        C 2 Replies Last reply
        0
        • A Alex501

          I need to create this program and needs to have if, else statements. Could you please help me? I want to create a program that will look like that? Welcome to HollowRectanglePrinter! How many columns wide should the rectangle be? 6 How many rows tall should the rectangle be? 4 Here you go: ****** * * * * ******

          G Offline
          G Offline
          Gareth H
          wrote on last edited by
          #4

          http://msdn2.microsoft.com/en-us/library/zdf6yhx5.aspx[^] If you still cant work it out... Regards, Gareth.

          1 Reply Last reply
          0
          • A Alex501

            I need to create this program and needs to have if, else statements. Could you please help me? I want to create a program that will look like that? Welcome to HollowRectanglePrinter! How many columns wide should the rectangle be? 6 How many rows tall should the rectangle be? 4 Here you go: ****** * * * * ******

            G Offline
            G Offline
            Guffa
            wrote on last edited by
            #5

            It's fun to obfuscate homework. ;)

            int w=6,h=4;h*=w;for(int i=h;i>0;Console.Write("*\r\n ".Substring(--i%w==0|w==i%w+1|w>i|h<i+w?0:3,i%w==0?3:1)));

            Experience is the sum of all the mistakes you have done.

            P 1 Reply Last reply
            0
            • P PIEBALDconsult

              (I agree that it looks like homework.) Loops? Plural? I can do that without loops. P.S. Without conditionals as well. P.P.S. And no recursion either for that matter.

              C Offline
              C Offline
              Colin Angus Mackay
              wrote on last edited by
              #6

              Some fancy pants string formatting?

              Upcoming FREE developer events: * Developer Day Scotland Recent blog posts: * Follow up on hiring a software developer * The Value of Smaller Methods My website | blog

              P 1 Reply Last reply
              0
              • P PIEBALDconsult

                (I agree that it looks like homework.) Loops? Plural? I can do that without loops. P.S. Without conditionals as well. P.P.S. And no recursion either for that matter.

                C Offline
                C Offline
                Colin Angus Mackay
                wrote on last edited by
                #7

                Okay - I've got a working solution without a single loop or conditional statement, nor does it use recursion. Probably not the most elegant solution, not as compact as Guffa's solution, but interesting none the less.

                Upcoming FREE developer events: * Developer Day Scotland Recent blog posts: * Follow up on hiring a software developer * The Value of Smaller Methods My website | blog

                P 1 Reply Last reply
                0
                • C Colin Angus Mackay

                  Some fancy pants string formatting?

                  Upcoming FREE developer events: * Developer Day Scotland Recent blog posts: * Follow up on hiring a software developer * The Value of Smaller Methods My website | blog

                  P Offline
                  P Offline
                  PIEBALDconsult
                  wrote on last edited by
                  #8

                  Mostly PadRight, with a smattering of Replace thrown in. And to think I wrote a version last year using four for loops; oh the shame! :doh:

                  1 Reply Last reply
                  0
                  • C Colin Angus Mackay

                    Okay - I've got a working solution without a single loop or conditional statement, nor does it use recursion. Probably not the most elegant solution, not as compact as Guffa's solution, but interesting none the less.

                    Upcoming FREE developer events: * Developer Day Scotland Recent blog posts: * Follow up on hiring a software developer * The Value of Smaller Methods My website | blog

                    P Offline
                    P Offline
                    PIEBALDconsult
                    wrote on last edited by
                    #9

                    I'll show you mine if you show me yours.... In the interest of being the smart-ass brown-noser in the class I used line drawing characters rather than asterisks. Anybody can use asterisks. :-D

                    System.Console.WriteLine
                    (
                    "\u250C".PadRight ( Width - 1 , '\u2500' ) + "\u2510\n" +

                    "".PadRight ( Height - 2 , '@' ).Replace
                    (
                        "@"
                    ,
                        "\\u2502".PadRight ( Width - 1 ) + "\\u2502\\n"
                    ) +
                    
                    "\\u2514".PadRight ( Width - 1 , '\\u2500' ) + "\\u2518"
                    

                    ) ;

                    C G 2 Replies Last reply
                    0
                    • G Guffa

                      It's fun to obfuscate homework. ;)

                      int w=6,h=4;h*=w;for(int i=h;i>0;Console.Write("*\r\n ".Substring(--i%w==0|w==i%w+1|w>i|h<i+w?0:3,i%w==0?3:1)));

                      Experience is the sum of all the mistakes you have done.

                      P Offline
                      P Offline
                      PIEBALDconsult
                      wrote on last edited by
                      #10

                      Oh no! You didn't use Environment.Newline! The world is going to end! :-D

                      G 1 Reply Last reply
                      0
                      • P PIEBALDconsult

                        I'll show you mine if you show me yours.... In the interest of being the smart-ass brown-noser in the class I used line drawing characters rather than asterisks. Anybody can use asterisks. :-D

                        System.Console.WriteLine
                        (
                        "\u250C".PadRight ( Width - 1 , '\u2500' ) + "\u2510\n" +

                        "".PadRight ( Height - 2 , '@' ).Replace
                        (
                            "@"
                        ,
                            "\\u2502".PadRight ( Width - 1 ) + "\\u2502\\n"
                        ) +
                        
                        "\\u2514".PadRight ( Width - 1 , '\\u2500' ) + "\\u2518"
                        

                        ) ;

                        C Offline
                        C Offline
                        Colin Angus Mackay
                        wrote on last edited by
                        #11

                        I suspect your solution is printing one character too wide. My solution is not so compact:

                            private static void Main(string\[\] args)
                            {
                                Console.Write("Width:");
                                int width = Convert.ToInt32(Console.ReadLine());
                                Console.Write("Height:");
                                int height = Convert.ToInt32(Console.ReadLine());
                        
                                string end = new string('\*', width);
                                string middle = string.Concat("\*", new string(' ', width - 2), "\*", Environment.NewLine);
                                string fill = new string('-', height - 2);
                                fill = fill.Replace("-", middle);
                                Console.WriteLine(end);
                                Console.Write(fill);
                                Console.WriteLine(end);
                        
                                Console.ReadLine();
                            }
                        

                        Upcoming FREE developer events: * Developer Day Scotland Recent blog posts: * Follow up on hiring a software developer * The Value of Smaller Methods My website | blog

                        P 1 Reply Last reply
                        0
                        • P PIEBALDconsult

                          Oh no! You didn't use Environment.Newline! The world is going to end! :-D

                          G Offline
                          G Offline
                          Guffa
                          wrote on last edited by
                          #12

                          PIEBALDconsult wrote:

                          The world is going to end!

                          The world is going to \r\n! ;)

                          Experience is the sum of all the mistakes you have done.

                          1 Reply Last reply
                          0
                          • P PIEBALDconsult

                            I'll show you mine if you show me yours.... In the interest of being the smart-ass brown-noser in the class I used line drawing characters rather than asterisks. Anybody can use asterisks. :-D

                            System.Console.WriteLine
                            (
                            "\u250C".PadRight ( Width - 1 , '\u2500' ) + "\u2510\n" +

                            "".PadRight ( Height - 2 , '@' ).Replace
                            (
                                "@"
                            ,
                                "\\u2502".PadRight ( Width - 1 ) + "\\u2502\\n"
                            ) +
                            
                            "\\u2514".PadRight ( Width - 1 , '\\u2500' ) + "\\u2518"
                            

                            ) ;

                            G Offline
                            G Offline
                            Guffa
                            wrote on last edited by
                            #13

                            Letting the replace do the looping is an interresting idea. The string constructor can do some looping too. :) int w=6,h=4;Console.Write(("h"+new string('w',h-2)+"h").Replace("h",new string('*',w)+"n").Replace("w","*"+new string(' ',w-2)+"*n").Replace("n", Environment.NewLine));

                            Experience is the sum of all the mistakes you have done.

                            P 1 Reply Last reply
                            0
                            • C Colin Angus Mackay

                              I suspect your solution is printing one character too wide. My solution is not so compact:

                                  private static void Main(string\[\] args)
                                  {
                                      Console.Write("Width:");
                                      int width = Convert.ToInt32(Console.ReadLine());
                                      Console.Write("Height:");
                                      int height = Convert.ToInt32(Console.ReadLine());
                              
                                      string end = new string('\*', width);
                                      string middle = string.Concat("\*", new string(' ', width - 2), "\*", Environment.NewLine);
                                      string fill = new string('-', height - 2);
                                      fill = fill.Replace("-", middle);
                                      Console.WriteLine(end);
                                      Console.Write(fill);
                                      Console.WriteLine(end);
                              
                                      Console.ReadLine();
                                  }
                              

                              Upcoming FREE developer events: * Developer Day Scotland Recent blog posts: * Follow up on hiring a software developer * The Value of Smaller Methods My website | blog

                              P Offline
                              P Offline
                              PIEBALDconsult
                              wrote on last edited by
                              #14

                              In what I posted I use width-1 so it's not a character too wide. (Pad only adds enough characters to satisfy the requested length.) I've never looked at constructors for string so I didn't think of that, but "".PadRight ( width , '*' ) results in the same thing as new string('*', width) I suspect the constructor is more efficient. Another tool for the toolbox, thanks.

                              1 Reply Last reply
                              0
                              • G Guffa

                                Letting the replace do the looping is an interresting idea. The string constructor can do some looping too. :) int w=6,h=4;Console.Write(("h"+new string('w',h-2)+"h").Replace("h",new string('*',w)+"n").Replace("w","*"+new string(' ',w-2)+"*n").Replace("n", Environment.NewLine));

                                Experience is the sum of all the mistakes you have done.

                                P Offline
                                P Offline
                                PIEBALDconsult
                                wrote on last edited by
                                #15

                                StringBuilder.Append will do that as well, leading me to suspect that the string constructor uses a StringBuilder. Stepping into the source code should confirm it. :-D

                                G 1 Reply Last reply
                                0
                                • P PIEBALDconsult

                                  StringBuilder.Append will do that as well, leading me to suspect that the string constructor uses a StringBuilder. Stepping into the source code should confirm it. :-D

                                  G Offline
                                  G Offline
                                  Guffa
                                  wrote on last edited by
                                  #16

                                  I doubt that. I believe that the string constructor does something much less complicated than using a StringBuilder. It probably just allocates the memory needed for the string and fills it with the character. Also, the StringBuilder returns a finished string object, so it would be complicated to merge the string object being created with the string object returned from the StringBuilder. :)

                                  Experience is the sum of all the mistakes you have done.

                                  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