How to print ASCII patterns in C# but using Java syntax?

748 Views Asked by At

patterns

Code below is for pattern a, feel like once I get a I could get the others.

I like the array[int].length syntax in Java and was helpful to get the pattern to print as shown in the picture. But I do not think such a thing exists in C#.

class Main {
    public static void main(String[] args)
    {
        char[][] arr = new char[10][10];
        int starCount = 10;
        for(int i = 0; i < arr.length; i++)
        {
            for(int j = 0; j < starCount; j++)
            {
                arr[i][j] = '*';
            }
            for(int k = starCount; k < arr[i].length; k++)
            {
                arr[i][k] = '-';
            }
            starCount--;
        }
        for(int a = 0; a < arr.length; a++)
        {
            for(int b = 0; b < arr[a].length; b++)
            {
                System.out.print(arr[a][b]);
            }
            System.out.println();
        }
    }
}

This code prints the * in a decreasing fashion but I am struggling with how to replace the empty elements of the array with the - character as shown in the image.

class MainClass {
    public static void Main (string[] args)
    {
        char[ , ] arr = new char[10,10];
        int starCount = 10;
        for(int i = 0; i < arr.Length; i++)
        {
            for (int j = 0; j < starCount; j++)
            {
                arr[i , j] = '*';
            }
            for (int k = 0; ) //IDK WHAT TO DO TO ASSIGN ARR[I , K] = '-';
                starCount--;
        }
        for (int i = 0; i < 10; i++)
        {
            for (int j = 0; j < 10; j++)
            {
                Console.Write(arr[i , j]);
            }
            Console.WriteLine();
        }
    }
}
4

There are 4 best solutions below

0
On BEST ANSWER

You can use two nested for loops and one if else statement to print all of these patterns. This is Java code, but I think it can be easily converted to C#:

int n = 5;
for (int i = -n; i <= n; i++) {
    for (int j = -n; j <= n; j++) {
        // a) if (i + j <= 0)
        // b) if (i + j >= 0)
        // c) if (i <= j)
        // d) if (Math.abs(i) + Math.abs(j) <= n)
        if (i + j <= 0)
            System.out.print("*");
        else
            System.out.print("-");
    }
    System.out.println();
}

Output (combined):

a)           b)           c)           d)
***********  ----------*  ***********  -----*-----
**********-  ---------**  -**********  ----***----
*********--  --------***  --*********  ---*****---
********---  -------****  ---********  --*******--
*******----  ------*****  ----*******  -*********-
******-----  -----******  -----******  ***********
*****------  ----*******  ------*****  -*********-
****-------  ---********  -------****  --*******--
***--------  --*********  --------***  ---*****---
**---------  -**********  ---------**  ----***----
*----------  ***********  ----------*  -----*-----

See also: Output an ASCII diamond shape using loops

0
On

The easiest way would be to look at the C# documentation for the Array class. There you would find that the Array class has a GetLength() method, that returns what the length property of a Java array returns.

Using that method you can change your code to

class MainClass {
    public static void Main (string[] args)
    {
        char[ , ] arr = new char[10,10];
        int starCount = 10;
        for(int i = 0; i < arr.GetLength(0); i++)
        {
            for (int j = 0; j < starCount; j++)
            {
                arr[i , j] = '*';
            }
            for (int k = starCount; k < arr.GetLength(1); k++)
            {
                arr[i , k] = '*';
            }
            starCount--;

        }
        for (int i = 0; i < arr.GetLength(0); i++)
        {
            for (int j = 0; j < arr.GetLength(1); j++)
            {
                Console.Write(arr[i , j]);
            }
            Console.WriteLine();
        }
    }
}
0
On

Here's a different approach to the original problem, which might be easier for you to convert.

Build a string of 10 stars and 9 dashes, e.g. hard-coded that would be:

String line = "**********---------";

Now print a 10 rows with substrings of that string:

for (int i = 0; i < 10; i++)
    System.out.println(line.substring(i, i + 10));

Output

**********
*********-
********--
*******---
******----
*****-----
****------
***-------
**--------
*---------

If the size is dynamic, based on an int value in variable starCount, then in Java 11+ you can use the repeat() method:

String line = "*".repeat(starCount) + "-".repeat(starCount - 1);
for (int i = 0; i < starCount; i++)
    System.out.println(line.substring(i, i + starCount));

That one should be easy to do in C#. See: Best way to repeat a character in C#.

In versions of Java below 11, you can build a char[]:

char[] line = new char[2 * starCount - 1];
Arrays.fill(line, 0, starCount, '*');
Arrays.fill(line, starCount, line.length, '-');
for (int i = 0; i < starCount; i++)
    System.out.println(new String(line, i, starCount));
0
On

For some reason when you switched from java to C# you went from using jagged arrays:

//java 
char[][] arr = new char[10][10];

To rectangular arrays:

//c#
char[ , ] arr = new char[10,10];

This undoubtedly makes your life more hard work because it means a lot more has to change. C# supports jagged arrays in exactly the same way Java does, and in fact if I hadn't written "//java" above you wouldn't have been able to tell whether it was C# or java because they're the same

I like the array[int].length syntax in Java ... But I do not think such a thing exists in C#.

It absolutely does, and you need to change just one single character to get it: properties in C# are named in Pascal case, so you want Length, not length

In fact, the logic of that entire block of java you have will work just fine in C# - just paste it in and change the following minor changes:

length -> Length
System.out.print -> Console.Write
System.out.println -> Console.WriteLine