+ 1

Drawing a square

Guys, how can I draw a square in a console window using a nested For loop and the (*) symbol? Your help will be highly appreciated.

28th Oct 2017, 8:27 PM
Tshego M
Tshego M - avatar
36 Antworten
+ 2
For readability it's better to do it with 'if-else' construction. if (age <= 22) { Console.Write("You're younger than {0}.", age); } else { Console.Write("You're older than {0}.", age); } With ternary will look like that: Console.Write((age <= 22) ? "You're younger than {0}." : "You're older than {0}.", age);
30th Oct 2017, 9:07 AM
Boris Batinkov
Boris Batinkov - avatar
+ 7
// in java a very simple one looks like this not sure if you can relate to any of it. //initalization of i outside for loop you dont have to do this you can initalize it inside the for loop int i=0; // first loop comparison.. for(;i < 5;++i){ //initalization of variable inside nested loop.. for(int j=0;j < 10;++j){ //prints the stars on the same line.. System.out.print("*");} //println method creates a new line after the nested loop has compleated.. System.out.println(); } //repeats untill the first for loop returns false
28th Oct 2017, 9:21 PM
D_Stark
D_Stark - avatar
+ 2
Assume this is the square: * * * * * * * * * * * * You need 2 nested 'for' loops. The outer will make new lines (rows). And the inner should draw the columns. Since this is a square, it's obvious that the top and bottom sides are the same. So you can print the top once before the loops and once after them, or to put logic inside the outer loop to print the symbols and spaces in the right moment. There are many ways to do it. But you should try it by yourself. What's the point if I solve it for you, right?
28th Oct 2017, 9:17 PM
Boris Batinkov
Boris Batinkov - avatar
29th Oct 2017, 7:38 AM
Boris Batinkov
Boris Batinkov - avatar
+ 2
if the statement evaluates to true the first statement is executed, if it evaluates to false the second statement is executed
29th Oct 2017, 7:38 AM
Adam
Adam - avatar
+ 2
where are the lines
29th Oct 2017, 9:34 AM
Adam
Adam - avatar
+ 2
can you publish the code in playground or paste it here? so I'll be able to play around with it and find a solution
29th Oct 2017, 9:31 PM
Adam
Adam - avatar
+ 1
Here's my C++ version. I've compensated for the space between lines and out in some input validation as well. If you really want I can make A C# version as well. Depends how many upvotes I get... :p https://code.sololearn.com/cEye8mi55i0i/?ref=app
28th Oct 2017, 10:31 PM
Adam
Adam - avatar
+ 1
Thanks, David. I think I see the error I made. My code was: for(I=0; I<=10; I++) { //Print the top of the square Console.Write("*"); break; //Print the sides for(j=0; j<1; j++) { Console.Write(* *); break; } } //Output was: * * * * * * * * * This is not what I want it to do.
29th Oct 2017, 5:38 AM
Tshego M
Tshego M - avatar
+ 1
you mean the ternary operator
29th Oct 2017, 7:32 AM
Adam
Adam - avatar
+ 1
a>b ? Console.Write ("a is larger") : Console.Write ("a is not larger");
29th Oct 2017, 7:36 AM
Adam
Adam - avatar
+ 1
I've putted some comments. If you wonder why I'm using 'size - 1', it's to avoid the unnecessary spaces at the end of the lines.
29th Oct 2017, 7:51 AM
Boris Batinkov
Boris Batinkov - avatar
+ 1
you don't need the "if"
29th Oct 2017, 8:26 AM
Adam
Adam - avatar
+ 1
just for the record you don't put parentheses on the first part of the ternary operator, or any part for that matter your code should look exactly like this: Age <= 21 ? Console.WriteLine ("Not eligible for college.") : Console.WriteLine ("Eligible for college."); only the writeline() method should get () and not the "Age <= 21" could that be your mistake?
29th Oct 2017, 9:38 PM
Adam
Adam - avatar
+ 1
k np good night and don't let the c# bugs bite 😄
29th Oct 2017, 10:10 PM
Adam
Adam - avatar
+ 1
lol!!!! you're crazy. So I tried it again today and let me just do try it in the playground then I'll post the code here. Please wait......
30th Oct 2017, 6:40 AM
Tshego M
Tshego M - avatar
+ 1
Ternary operator is helpful, when you need to print a short string, or assign value to variable, where 'if- else' can't do better than taking unnecessary space. // Example 1 int num = 5; // using if-else if (num % 2 == 0) { Console.Write("even"); } else { Console.Write("odd"); } // using ternary Console.Write((num % 2 == 0) ? "even" : "odd"); // Example 2 - ternary string answer = (num % 2 == 0) ? "even" : "odd"; Console.Write("The number is {0}.", answer); // Example 3 - ternary int yourAge = 30; int age = 25; string ageStat = (yourAge <= age) ? "younger" : "older"; Console.Write("You're {0} than {1}.", ageStat, age);
30th Oct 2017, 2:33 PM
Boris Batinkov
Boris Batinkov - avatar
+ 1
You can start over, without reset. And yes. It's good to take the course again. But note that SL is just a little from here - a little from there thing. To understand the material you'll need to solve a lot of code problems. For example, you can practice everything in www.hackerrank.com.
31st Oct 2017, 11:28 AM
Boris Batinkov
Boris Batinkov - avatar
+ 1
I'm coding since 2013, but started more serious, and learned 80% of all I know, in 2016-2017 with Java as main language. Now I'm on OOP and currently I'm practicing with recursion. For example to simulate the behavior of n count of nested for-loops.
31st Oct 2017, 3:32 PM
Boris Batinkov
Boris Batinkov - avatar
0
Boris, I can do it but it would result in long lines of code e.g. Console.write line("*********"); etc etc. I tried doing it yesterday before deciding to post the question here because there's clearly something that I'm not doing right.
29th Oct 2017, 5:28 AM
Tshego M
Tshego M - avatar