Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Starpattern in console app

Tags:

c#

I need to create the following pattern: http://puu.sh/dx3fN/500f5ee468.png

It is homework, this question I failed the first time. I read now that I should have only used "*" one time, but how would this even work in that case? Would appreciate if anyone could give me some insight in how to think.

My code is down below:

using System;
class StarPattern
{
    public static void Main()
    {
        for (int i = 0; i < 5; i++)
        {
            Console.Write("*");
        }
        for (int a = 0; a <= 0; a++)
        {
            Console.WriteLine("");
            Console.Write("*");
        }
        for (int c = 0; c <= 0; c++)
        {
            Console.WriteLine("   *");
        }
        for (int d = 0; d <= 1; d++ )
        {
            Console.Write("*");
            Console.WriteLine("   *");
        }
        for (int e = 0; e < 5; e++ )
        {
            Console.Write("*");
        }
        Console.ReadLine();
    }                 
}
like image 394
kaktusräv Avatar asked Dec 02 '22 15:12

kaktusräv


2 Answers

You can simplify your code by nesting loops so outer (i) = rows and inner (j) = columns. From looking at your sample, you want to write out if you're on the boundary of either so you can add a condition to just write out on min and max.

private static void Main(string[] args)
{
    for (int i = 0; i <= 4; i++)
    {
        for (int j = 0; j <= 4; j++)
        {
            if (i == 0 || i == 4 || j == 0 || j == 4)
            {
                Console.Write("*");
            }
            else
            {
                Console.Write(" ");
            }
        }
        Console.WriteLine();
    }
    Console.ReadKey();
}

I'd probably replace 0 with a constant called MIN and 4 with a constant called MAX to save duplicating them. That way, you could increase the size of the square by just changing the constants.

like image 104
DoctorMick Avatar answered Dec 04 '22 03:12

DoctorMick


Hardly anyone is commenting their code for you. How disappointing!

The trick here is to focus on what is important and define values that you will;

  1. be using many times in the code
  2. only want to change once if requirements need tweeking

These values are height and width - the core components of a rectangle.

The golden rule for the pattern is: If the row and column of each character is on the edge of the square, print a *

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

namespace Stars
{
    class Program
    {
        static int Width = 5; //define the width of the square in characters
        static int Height = 5; //define the height of the square in characters

        static void Main(string[] args)
        {
            for (int row = 0; row <= Height; row++) //Each iteration here is one row.
            {
                //loop representing columns. This is NESTED within the rows so 
                //that each row prints more than one column
                for (int column = 0; column <= Width; column++)
                {
                    if (IsCentreOfSquare(row, column)) //calculate if the current row and column coordinates are the interior of the square
                    {
                        Console.Write(" ");
                    }
                    else
                    {
                        Console.Write("*");
                    }
                }
                Console.WriteLine(); //this row is over. move to the next row
            }
            Console.ReadLine(); //pause so that the user can admire the pretty picture.
        }

        /// <summary>
        /// Calculates if the row and column indexes specified are in the interior of the square pattern
        /// </summary>
        /// <returns></returns>
        private static bool IsCentreOfSquare(int row, int col)
        {
            if (row > 0 && row < Height)
            {
                if (col > 0 && col < Width)
                {
                    return true;
                }
            }

            return false;
        }
    }
}
like image 32
Gusdor Avatar answered Dec 04 '22 03:12

Gusdor