-3

I am new here and also new to C# in general. I have as a task to output the letter O. Furthermore, the program should read a number that means the corresponding height.

The output should look like the given photo illustrates.

Photo of Illustration

If the number is now larger, the object should be adjusted in width accordingly. And this is now my problem. I already have a code block, which outputs this above constellation, but is not customizable. I have already tried using WriteLine and if statements to scale a width with spaces, but that doesn't work the way I want it to. In addition, the convert is still missing at the beginning, which introduces the entered number.

So what I want:

  1. you should enter a number that defines the height.
  2. the output should be adjusted to the height and scaled logically
  3. no more lines should be added that contain the #, it should only be the total of 8 "#" (stretched, you could say)

My code so far is:

class Program
{
    static void Main(string[] args)
    {
        for (int i = 0; i <= 7; i++)                               
            if (i == 0)
                Console.WriteLine(" #");
        for (int j = 1; j <= 7; j++)
            if (j == 1)
                Console.WriteLine(" # #");
        for (int k = 2; k <= 7; k++)
            if (k == 2)
                Console.WriteLine("# #");      
        for (int m = 3; m <= 7; m++)
            if (m == 3)
                Console.WriteLine(" # #");
        for (int m = 4; m <= 7; m++)
            if (m == 4)
                Console.WriteLine(" #");
        Console.ReadKey();
    }
}
NVRM
  • 1
  • 2
    That Image is called "ASCII Art". That is the term you should google for. – Christopher Aug 16 '21 at 11:41
  • You need to use a fixed-size (monospaced) font like Courier for ASCII Art and that is the case for a Console standard settings. What is the problem and teh question? I see that the spaces are misplaced. –  Aug 16 '21 at 11:43
  • 1
    Does your code not work in some way? Now is probably a good time to start familiarizing yourself with the [use of a debugger](https://stackoverflow.com/q/25385173/328193). When you do this, which specific operation in your code first produces an unexpected result? What were the runtime values when that operation was invoked? What was the observed result? What was the expected result? Why? For tips and information on how to ask a question, please see [ask] and its linked resources. – David Aug 16 '21 at 11:44
  • 3
    [How do I ask and answer homework questions on StackOverflow](https://meta.stackoverflow.com/questions/334822/how-do-i-ask-and-answer-homework-questions) – Jamiec Aug 16 '21 at 11:44
  • Does this answer your question? [Producing ascii art via C#](https://stackoverflow.com/questions/3436132/producing-ascii-art-via-c-sharp) and [Print ASCII line art characters in C# console application](https://stackoverflow.com/questions/4511098/print-ascii-line-art-characters-in-c-sharp-console-application) –  Aug 16 '21 at 11:46
  • What's the point of the for loops in your code? They essentially do nothing. Your code boils down to 5 Console.WriteLines. – Slugsie Aug 16 '21 at 12:00
  • 1
    If you understand the code you have already written (and I'm not sure you exactly do, since those loops are redundant), then what you need to do is to simply ask, "if I wanted to increase the size by X, what would that mean for each row?" In other words, the difference is only in the placement and number of spaces so how can your code increase the number of spaces depending on the width? Draw out a few example on squared paper and it is basic math. – Luke Briner Aug 16 '21 at 12:06

1 Answers1

0

Have a close look at the diamond (the letter O).

The first and last line contain only one #. On these lines you need a loop to print the variable number of spaces before.

___#

Between these two lines you have a variable number of lines. You need a loop to produce these lines. Or two loops. One to produce the ^ shape

__#_#
_#___#
#_____#

and another to produce the v shape.

_#___#
__#_#

Nested inside these loops you need two more loops. One which prints the spaces before and one which prints the spaces between the first # and the second #.

Note that if the total number of lines is odd, the middle line is the longest and the ^ and v parts do not have the same size. If the number is even, then you will have two longest lines and two parts having the same height.

and the last

___#

With Console.Write you can write characters on the same line. With Console.WriteLine you end the line, and the next write will write on the next line.

Well, and then you need some basic arithmetic to calculate the number of spaces depending on the total height and the current line number.

Note that the greatest effort goes into dissecting this shape into its basic components and analyzing its geometry. Once you have this, the coding itself is rather simple.

Since this looks like homework, I'll let you figure out the details and do the coding part.

Olivier Jacot-Descombes
  • 104,806
  • 13
  • 138
  • 188