-2

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();
    }
  }
}
maccettura
  • 9,653
  • 3
  • 20
  • 28
  • 1
    *Tip 1:* Always format the code for human readability, i.e. indent the code appropriately. – Andreas Dec 11 '20 at 20:29
  • sorry its an awkward copy and paste from repl.it and first time putting code into a question on here. – Mikail Miller Dec 11 '20 at 20:30
  • Still not really clear what your question is, what syntaax in your java code can you not replicate in your c# code? – NotZack Dec 11 '20 at 20:31
  • in the java code I use the array[int].length in the nested for loop to assign the '-' char into the array but in C# that does not exists and I cannot figure out how to replicate the pattern without it. – Mikail Miller Dec 11 '20 at 20:34

2 Answers2

0

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));
Andreas
  • 138,167
  • 8
  • 112
  • 195
  • Thank you for the answer and I will attempt to recreate it that way. This is a practice exercise for using nested loops and multi-dimensional arrays so may need to try the jagged array suggestion that was mentioned in a comment. – Mikail Miller Dec 11 '20 at 20:50
0

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();
    }
  }
}
Thomas Kläger
  • 10,196
  • 2
  • 16
  • 27
  • Thank you so much for this answer! Now I will try to apply this syntax to the other patterns for practice. Really appreciate your help!! – Mikail Miller Dec 16 '20 at 14:58