7 Replies - 937 Views - Last Post: 09 October 2015 - 06:45 AM Rate Topic: -----

#1 The_Fatness   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 13-September 15

For Loops with Nested Loops

Posted 08 October 2015 - 05:46 PM

Hello all. I have been trying to read ahead in my programming class today and am working on the For and Nested Loops assignments and have accomplished one of the assignments in Visual Logic, and we have to do all of our assignments in both Visual Logic and Visual Studio. This assignment is to create a diamond in the console using just the letter "o".

This and this are the solution and visualization from Visual Logic.

Here is where I am getting stuck in Visual Studio.


for (int lineCount = 1; lineCount <= 19; lineCount++)
            {

                for (int spaceCount = 18; spaceCount >= lineCount; spaceCount--)
                {
                    Console.Write(" ");
                    spaceCount--;
                }
                for (int circleCount = 1; circleCount <= lineCount; circleCount++)
                {
                    Console.Write("o");

                }
                Console.WriteLine();
                lineCount++;
            }

            int count = 1;

            for (int lineCount = 1; lineCount <= 19; lineCount++)
            {
                for (int spaceCount = 1; spaceCount <= lineCount; spaceCount++)
                {
                    spaceCount++;
                    Console.Write(" ");
                }
                for (int circleCount = count; count <= 17; count++)
                {
                    Console.Write("o");
                }
                lineCount++;
                Console.WriteLine();
                count++;
            }


Are any of you fine folks able to throw me a bone as to what I am missing?

Is This A Good Question/Topic? 0
  • +

Replies To: For Loops with Nested Loops

#2 Damage   User is offline

  • Lord of Schwing
  • member icon

Reputation: 300
  • View blog
  • Posts: 1,989
  • Joined: 05-June 08

Re: For Loops with Nested Loops

Posted 08 October 2015 - 06:01 PM

Your second for block, the one where you're drawing the bottom of the diamond. You're not counting down on anything. Something like

 int circlecount = 10;
            for (int i = circlecount - 1; i >= 1; i--)
            {
                for (int j = 0; j < (circlecount - i); j++)
                    Console.Write(" ");
                for (int j = 1; j <= i; j++)
                    Console.Write("o");
                for (int k = 1; k < i; k++)
                    Console.Write("o");
                Console.WriteLine();
            }


This post has been edited by Damage: 08 October 2015 - 06:04 PM

Was This Post Helpful? 0
  • +
  • -

#3 The_Fatness   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 13-September 15

Re: For Loops with Nested Loops

Posted 08 October 2015 - 06:21 PM

Last night I was extremely confident with programming and the direction I am headed in. Now I am back to thinking I am not smart enough for this.
Was This Post Helpful? 0
  • +
  • -

#4 Damage   User is offline

  • Lord of Schwing
  • member icon

Reputation: 300
  • View blog
  • Posts: 1,989
  • Joined: 05-June 08

Re: For Loops with Nested Loops

Posted 08 October 2015 - 06:42 PM

hahahaha, dude, i think the same thing on a daily basis. If you enjoy it, keep going. Somethings take longer than others to wrap your head around but you just have to keep chipping away it
Was This Post Helpful? 0
  • +
  • -

#5 Skydiver   User is offline

  • Code herder
  • member icon

Reputation: 7915
  • View blog
  • Posts: 26,430
  • Joined: 05-May 12

Re: For Loops with Nested Loops

Posted 08 October 2015 - 07:26 PM

Also compare the logic you have in Visual Logic vs. C#. In Visual Logic, you are incrementing and decrementing by 2, but in your C# code, you are only incrementing and decrementing by 1 in your for loop declaration, and then you have an extra increment/decrement within your loop body. Although you get the intended effect, it would probably make things clearer if you kept the two consistent.
Was This Post Helpful? 0
  • +
  • -

#6 The_Fatness   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 13-September 15

Re: For Loops with Nested Loops

Posted 09 October 2015 - 05:46 AM

View PostDamage, on 08 October 2015 - 06:42 PM, said:

hahahaha, dude, i think the same thing on a daily basis. If you enjoy it, keep going. Somethings take longer than others to wrap your head around but you just have to keep chipping away it


I initially chose Networking as my "major" in school, and discovered that I am not cut out for it and frankly just do not enjoy it. After figuring out some of my programming assignments on my own, I really feel like I enjoy it a lot more than I expected. However these "For Loops and Nested Loops" have really knocked me back down a peg. I haven't struggled with any of it quite so much as I did this. I actually did really well in Visual Logic with the For and Nested Loops, but in C# it was a totally different animal.


View PostSkydiver, on 08 October 2015 - 07:26 PM, said:

Also compare the logic you have in Visual Logic vs. C#. In Visual Logic, you are incrementing and decrementing by 2, but in your C# code, you are only incrementing and decrementing by 1 in your for loop declaration, and then you have an extra increment/decrement within your loop body. Although you get the intended effect, it would probably make things clearer if you kept the two consistent.


Am I not incrementing by 2 by denoting the count as = 1 and then doing count++ inside the nested loop and at the end of the for loop?
Was This Post Helpful? 0
  • +
  • -

#7 Skydiver   User is offline

  • Code herder
  • member icon

Reputation: 7915
  • View blog
  • Posts: 26,430
  • Joined: 05-May 12

Re: For Loops with Nested Loops

Posted 09 October 2015 - 05:54 AM

View PostThe_Fatness, on 09 October 2015 - 08:46 AM, said:

Am I not incrementing by 2 by denoting the count as = 1 and then doing count++ inside the nested loop and at the end of the for loop?


Yes, you are, but instead of writing your code as:
for (int lineCount = 1; lineCount <= 19; lineCount++)
{
    :
    lineCount++;
}



You could have written it as:
for (int lineCount = 1; lineCount <= 19; lineCount += 2)
{
    :
}



Quote

“Programs must be written for people to read, and only incidentally for machines to execute.”

― Harold Abelson, Structure and Interpretation of Computer Programs


Please pass on my kudos to your teacher who is making you write your program twice: once in Visual Logic and the second time in C#.

I learned programming in a similar environment. I had to draw out a flowchart first before I could start implementing it in BASIC. I remember hating it back then because it discouraged experimentation which was one of the things that BASIC tried to encourage. It did teach me how to try to plan code structure better.
Was This Post Helpful? 0
  • +
  • -

#8 The_Fatness   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 13-September 15

Re: For Loops with Nested Loops

Posted 09 October 2015 - 06:44 AM

@Skydiver, ok thanks I wasn't sure how to increment by more than 1. I will pass on the kudos for sure. We only code this way for the first semester, and even then we are close to not using Visual Logic anymore, I think all we have left is 2 chapters, 1 of which is Arrays and the other is something with drawing pictures by rotating shapes.

@Skydiver, ok thanks I wasn't sure how to increment by more than 1. I will pass on the kudos for sure. We only code this way for the first semester, and even then we are close to not using Visual Logic anymore, I think all we have left is 2 chapters, 1 of which is Arrays and the other is something with drawing pictures by rotating shapes.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1