Click here to Skip to main content
15,893,814 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
See more:
Write a program in C# to display the diamond pattern shown below. The user provides the number of the row that is the middle of the diamond. Note that the number of stars in the rows are are 1, 3, 5, etc respectively and then downwards ….5, 3, 1.
   *   
  *** 
 ***** 
******* 
 ***** 
  *** 
   *

Please note all stars are centered.

What I have tried:

C#
if (radioButton2.Checked) //full diamond
            {
                int i, j, k, n;
                //Convert Text Box TOINT32
                n = Convert.ToInt32(textBox1.Text);  
                string line = ""; // for individual output

                for (i = 1; i <= n; i++)//Upper Diamond
                {
                    
                    for (k = 1; k <= n-i; k++) 
                    {
                        line = "";
                    }
                    for (j = 1; j <= i; j++)
                    {
                        line +=  "* " ;
                    }
                    richTextBox1.SelectionAlignment = HorizontalAlignment.Center;
                    richTextBox1.Text += line + "\n";
                }
                for (i = 1; i <= n - 1; i++) //Bottom Diamond
                {
                    for (k = 1; k <= i; k++)
                    {
                        line = "";
                    }
                    for (j = n - 1; j >= i; j--)
                    {
                        line += "* ";
                    }
                    richTextBox1.SelectionAlignment = HorizontalAlignment.Center;
                    richTextBox1.Text += line + "\n";
                }

            } 


My results is
      *  
     * * 
    * * * 
* * * * * * * 
    * * * 
     * * 
      *

Please note all stars are centered

my diamond comes out wrong in a 1 2 3 7 and 3 2 1 formart. please help
Posted
Updated 15-May-17 7:39am
v2

Hi ,
There is a mistake in this line:
for (i = 1; i <= n; i++)//Upper Diamond
It should be:
for (i = 1; i < n; i+=2)//Upper Diamond - change in comparison and increment
Same applies to the bottom diamond. There needs to be a couple of adjustments for the bottom diamond; I leave that up to you as I can only guide you to the solution, but you need to do the homework.

Plus line cleaning involved in the for loop is unnecessary.
for (k = 1; k <= n-i; k++) 
{
   line = "";
}
This can simply be a single line; the FOR loop is unnecessarily punishing the compiler to do the same thing for a couple of times.
line = "";
 
Share this answer
 

I would start off by analysing what is going on. As I see it, there are a number of fixed-length lines. The length of each line is equal to the total number of lines. The number of lines needs to be an odd number and >1 so that you have a middle line to give the diamond effect. Initially there is a central ‘*’ character and then a new * is added to the left and right of the existing stars as the line number increases. This carries on until the middle line has been printed, then the process is reversed and the most recently added * to the left and the right is then replaced by a space.

Although a string is char array, it is read only. You can, however, use a char[], manipulate that and then convert it to a string before printing it. Something like

C#
var currentLine = new string(' ', lines); //fill line with spaces
char[] characters = currentLine.ToCharArray();

You can then add ‘*’ to the characters array by indexing into it. You will need a leftposition and rightposition variable along with an increment variable. The increment is set to -1 after the middle line is printed.
C#
characters[lpos] = charToPaste;
characters[rpos] = charToPaste;
 Console.WriteLine(new string(characters));
lpos -= increment;
rpos += increment;

You need only a single loop to do this.

 
Share this answer
 
Check carefully where you clear the line variable.

When you don't understand what your code is doing or why it does what it does, the answer is debugger.
Use the debugger to see what your code is doing. Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute, it is an incredible learning tool.

Debugger - Wikipedia, the free encyclopedia[^]

Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
Basic Debugging with Visual Studio 2010 - YouTube[^]
The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't find bugs, it just help you to. When the code don't do what is expected, you are close to a bug.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900