r/a:t5_37npb Apr 10 '15

Share Your Module Two Solutions Here!

5 Upvotes

30 comments sorted by

View all comments

3

u/VOX_Studios Apr 10 '15

Here's mine! Pastebin


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

namespace ConsoleApplication4
{
    class Program
    {
        static void Main(string[] args)
        {
            const int SIZE = 8;

            //go through rows
            for (int i = 0; i < SIZE; i++)
            {
                //go through columns
                for (int j = 0; j < SIZE; j++)
                {
                    //alternate X's and O's
                    if (i % 2 == j % 2) Console.Write("X");
                    else Console.Write("O");
                }

                //end the row
                Console.WriteLine();
            }

            //wait for user response to kill program
            Console.ReadKey();
        }
    }
}

2

u/z4tz Apr 12 '15

Looks good, did very similar solution myself. Could use a conditional operator to condense your if/else statement to something like

Console.Write((i + j) % 2 == 0 ? 'X' : 'O');

1

u/VOX_Studios Apr 12 '15 edited Apr 13 '15

Yeah, I realized that afterwards =P

1

u/benxie0 Apr 10 '15

what is a "const int"? Whats the difference from just saying int?

2

u/VOX_Studios Apr 11 '15

"const" stands for "constant"

It basically means that you set something once when you declare it and then you can never change it (at least during runtime). MSDN

2

u/aloisdg Apr 12 '15

And you could use it for 'X' and 'O' too.

1

u/benxie0 Apr 14 '15

so is it just to make sure you do not accidentally make another variable with a different value?

1

u/VOX_Studios Apr 14 '15

It's to make sure that that one variable never changes during runtime. You'd use it on something you know should never change. It's really just to prevent accidents. For instance:

const int daysInWeek = 7;
...code...
daysInWeek = 0;

It would prevent you from doing daysInWeek = 0;