I need help with C# loops
-
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: ****** * * * * ******
-
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: ****** * * * * ******
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.
-
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: ****** * * * * ******
(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.
-
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: ****** * * * * ******
-
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: ****** * * * * ******
-
(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.
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
-
(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.
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
-
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
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: -
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
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"
) ;
-
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.
Oh no! You didn't use Environment.Newline! The world is going to end! :-D
-
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"
) ;
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
-
Oh no! You didn't use Environment.Newline! The world is going to end! :-D
-
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"
) ;
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.
-
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
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 asnew string('*', width)
I suspect the constructor is more efficient. Another tool for the toolbox, thanks. -
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.
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
-
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
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.