settingsAccountsettings
Menusettings

Q: Sunlight (C# Drawing Task)

+4 votes

Sandy is a little girl who spends her free time playing with her friends. Unfortunately, Sandy broke her wristwatch a week ago. Now she is trying a new trick using the sunlight to guess the time. But this trick is useful only when the sky isn’t cloudy. Your task is to help Sandy by writing a program which shows how bright the sun is at the moment.

You are given an integer number N (always odd), corresponding to the width and height of the sun and the length of the horizontal and vertical sunbeams. The diagonal sunbeams have length equal to N – 1.

Input:

The input data should be read from the console.

  • On the only input line you will be given an integer N - the size of the sun.

The input data will always be valid and in the format described. There is no need to check it explicitly.

Output:

The output should be printed on the console. Use the “*” (asterisk) to mark the sun and the sunbeams and “.” (dot) for the rest. Follow the examples below.

Constraints:

  • N will always be a positive odd number in the range [333].
  • Allowed working time for your program: 0.1 seconds. Allowed memory: 16 MB.

Examples:

drawing task in C#

asked in C# category by user sam
edited by user golearnweb

1 Answer

+2 votes
 
Best answer

You need to use new string() inside the Console.WriteLine - you can read more here: http://www.dotnetperls.com/string-constructor

Also, here is how I divide the task - into 7 parts - and then solve (draw) it piece by piece. You can read the comments in the code - it is explained there.

See the pic - I made for you :-):

drawing the solution of the task

Here is my answer to your question (the code):

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


class Sunlight
{
    static void Main()
    {
        int n = int.Parse(Console.ReadLine());

        Console.WriteLine("{0}*{0}",
            new string('.', (((n * 3) - 1) / 2)));//PART 1 (STATIC)

        for (int i = 0; i < (n - 1); i++)//PART 2 (DYNAMIC)
        {
            Console.WriteLine("{0}*{1}*{1}*{0}",
                new string('.', i + 1),
                new string('.', (((n * 3) / 2) - 2) - i));
        }

        for (int i = 0; i < (n / 2); i++)//PART 3 (DYNAMIC)
        {
            Console.WriteLine("{0}{1}{0}",
                new string('.', n),
                new string('*', n));
        }

        Console.WriteLine("{0}",
            new string('*', n * 3));//PART 4 (STATIC)

        for (int i = 0; i < (n / 2); i++)//PART 5 (DYNAMIC) - THE SAME AS PART 3
        {
            Console.WriteLine("{0}{1}{0}",
                new string('.', n),
                new string('*', n));
        }

        for (int i = 0; i < (n - 1); i++)//PART 6 (DYNAMIC)
        {
            Console.WriteLine("{0}*{1}*{1}*{0}",
                new string('.', (n - 1) - i),
                new string('.', (n / 2) + i));
        }

        Console.WriteLine("{0}*{0}",
            new string('.', (((n * 3) - 1) / 2)));//PART 7 (STATIC) - THE SAME AS PART 1
    }
}
answered by user matthew44
edited by user golearnweb
...