11 Replies - 484 Views - Last Post: 25 September 2013 - 01:58 PM Rate Topic: -----

#1 bkruep  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 24
  • Joined: 26-July 13

Checkerboard

Posted 23 September 2013 - 10:46 AM

I am trying to write a checkerboard and I can only use the following commands for output...

Console.Write("* ");
Console.Write(" ";
Console.WriteLine();

I got the code to output the correct amount of lines but the spacing is off. This is what my code looks like...

* * * * * * * *
* * * * * * * *
* * * * * * * *
* * * * * * * *
* * * * * * * *
* * * * * * * *
* * * * * * * *
* * * * * * * *

Here is how I need it to look
* * * * * * * *
_ * * * * * * * *
* * * * * * * *
_ * * * * * * * *
* * * * * * * *
_ * * * * * * * *
* * * * * * * *
_ * * * * * * * *
(the underscores are spaces)

Here is my code and any help would be much appreciated...


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Checkerboard
{
    class Program
    {
        static void Main(string[] args)
        {
            int lineNumber = 1;
            int asterick_amount = 1;

            while (lineNumber <= 8)
            {
 
                while (asterick_amount <= 8)
                {
                    Console.Write("* ");
                    asterick_amount += 1;           
                }
                
                asterick_amount = 1;
                Console.WriteLine();
                lineNumber += 1;
            }
        }
    }
}




This post has been edited by andrewsw: 23 September 2013 - 11:07 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Checkerboard

#2 andrewsw  Icon User is online

  • Fire giant boob nipple gun!
  • member icon

Reputation: 3215
  • View blog
  • Posts: 10,781
  • Joined: 12-December 12

Re: Checkerboard

Posted 23 September 2013 - 10:50 AM

Within your outer while-loop you need to check if lineNumber is even; if it is, start by printing an extra space.

So you need to use an if-statement and you can use the % operator to determine if a number is even (or odd).

% Operator :MSDN

dotnetperls:odd

This post has been edited by andrewsw: 23 September 2013 - 10:54 AM

Was This Post Helpful? 0
  • +
  • -

#3 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2241
  • View blog
  • Posts: 9,412
  • Joined: 29-May 08

Re: Checkerboard

Posted 23 September 2013 - 10:59 AM

Can be done in single loop if you're prepare to have think.
Was This Post Helpful? 0
  • +
  • -

#4 Curtis Rutland  Icon User is online

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 4433
  • View blog
  • Posts: 7,702
  • Joined: 08-June 10

Re: Checkerboard

Posted 23 September 2013 - 01:05 PM

Are you sure that's how its supposed to look? Shouldn't it look more like this:

  *   *   *   *
*   *   *   *
  *   *   *   *
*   *   *   *
  *   *   *   *
*   *   *   *
  *   *   *   *
*   *   *   *


Because that's what a checkerboard looks like, alternating black (space) and red (star).

And Adam is correct, but it's no more efficient (you're going to run 64 iterations either way), so either way you do it is "right".
Was This Post Helpful? 0
  • +
  • -

#5 Witchking  Icon User is offline

  • D.I.C Head

Reputation: 68
  • View blog
  • Posts: 188
  • Joined: 17-February 13

Re: Checkerboard

Posted 23 September 2013 - 01:57 PM

View PostCurtis Rutland, on 23 September 2013 - 10:05 PM, said:

And Adam is correct, but it's no more efficient (you're going to run 64 iterations either way), so either way you do it is "right".

It may not be more efficient, but it is more fun. ;)

This post has been edited by Witchking: 23 September 2013 - 01:57 PM

Was This Post Helpful? 0
  • +
  • -

#6 Curtis Rutland  Icon User is online

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 4433
  • View blog
  • Posts: 7,702
  • Joined: 08-June 10

Re: Checkerboard

Posted 23 September 2013 - 02:08 PM

Agreed. I solved it with one loop just for the heck of it. It's not really any harder if you understand how to use the modulus operator (%).
Was This Post Helpful? 0
  • +
  • -

#7 Momerath  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1010
  • View blog
  • Posts: 2,444
  • Joined: 04-October 09

Re: Checkerboard

Posted 24 September 2013 - 01:24 PM

I did it with no loops!
Was This Post Helpful? 0
  • +
  • -

#8 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3467
  • View blog
  • Posts: 10,687
  • Joined: 05-May 12

Re: Checkerboard

Posted 24 September 2013 - 06:14 PM

I can see doing no loops if you know the checkerboard dimensions at compile time, but do do you do it at runtime with no loops?
Was This Post Helpful? 0
  • +
  • -

#9 andrewsw  Icon User is online

  • Fire giant boob nipple gun!
  • member icon

Reputation: 3215
  • View blog
  • Posts: 10,781
  • Joined: 12-December 12

Re: Checkerboard

Posted 24 September 2013 - 07:12 PM

View PostSkydiver, on 25 September 2013 - 01:14 AM, said:

I can see doing no loops if you know the checkerboard dimensions at compile time, but do do you do it at runtime with no loops?

A checkerboard has a known, fixed, size, so this is an occasion where loop-unwinding can be considered.

This post has been edited by andrewsw: 24 September 2013 - 07:13 PM

Was This Post Helpful? 0
  • +
  • -

#10 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3467
  • View blog
  • Posts: 10,687
  • Joined: 05-May 12

Re: Checkerboard

Posted 24 September 2013 - 07:50 PM

Ah, I see. It's not a tiling problem. I was more interested in the general problem solution for tiling some arbitrary size. But you are right, for the normal 8x8 problem space, unrolling the loop works. But at that point, why unroll the loop instead of just declaring an array of constant strings? The unrolled loop still needs to be JIT'ed, but the array of constant strings will live as a block of memory that just needs to be referenced.
Was This Post Helpful? 0
  • +
  • -

#11 Momerath  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1010
  • View blog
  • Posts: 2,444
  • Joined: 04-October 09

Re: Checkerboard

Posted 24 September 2013 - 09:25 PM

Because of the constraint placed by the original poster, you only have three output statements you can use.

And no, this isn't a good use for loop unrolling, the runtime isn't significant anyways, removing the looping code doesn't really enhance performance. Just got caught up in the "I can do that in ONE loop.", "Well, I can do it with ZERO loops!" :)

This post has been edited by Momerath: 24 September 2013 - 09:27 PM

Was This Post Helpful? 0
  • +
  • -

#12 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2241
  • View blog
  • Posts: 9,412
  • Joined: 29-May 08

Re: Checkerboard

Posted 25 September 2013 - 01:58 PM

Without any explicit written loops.
Spoiler

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1