Click here to Skip to main content
15,881,248 members
Please Sign up or sign in to vote.
1.31/5 (4 votes)
See more:
Hello Friends,

I am practicing my programing assignment with the help of c# language.
I found that this program unable to print the desired output what i want.

Please help me out in finding the error.

Thank you.

Code.
............................
C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace logical_star_outer_pyramid
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Enter your Number");
            int i = int.Parse(Console.ReadLine());
            for (int j = 0; j <= i; j++)
            {
                for (int k = 1; k <= (i - j); k++)
                    Console.Write(" ");
                for (int l = 1; l <= j; l++)
                {
                    if (l == 1 || l == j || i == j)
                        Console.Write("*" + "");
                    else
                        Console.Write(" ");
                }
                Console.WriteLine();
            }
            Console.ReadLine();
        }
    }
}


output should be like:-
Enter the Number:-
4;I.E; Outer Pyramid (inner pyramid or stars should not present)

    *
  *   *
 *     *
* * * * *
Posted
Updated 16-Apr-15 1:07am
v2
Comments
Tomas Takac 16-Apr-15 2:41am    
Is this a homework? My fist advice would be: use meaningful variable names. The code would be much more readable if named i -> number, j -> row, k -> column etc.
Sergey Alexandrovich Kryukov 16-Apr-15 3:32am    
Right. Should be correctly spelled English words, such as "index"...
My other advice: use the debugger.
—SA
Mirza Mustafa Ali Baig 16-Apr-15 5:12am    
Thanks for your reply and your advice sir.

1 solution

Why do you use 3 loops?
C#
for (int j = 0; j <= i; j++)
{
    for (int k = 1; k <= (i - j); k++)
        Console.Write(" ");
    for (int l = 1; l <= j; l++)


You need to use only 2 loops! Imagine, you need to fill array...
In pseudo-code:
C#
string stars = string.Empty;
for (int i = 1; i<=NumberEnteredByUser; i++)
{
    for (j = 1; j<=i; j++)
    {
        //concat *
        stars = string.Concat(stars, "*");
    }
    //print
    Console.WriteLine(stars);
    //clear string
    stars = string.Empty;
}


Got it?

Note that string is immutable. What it means? Read here: Why strings are immutable and what are the implications of it?[^]
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 16-Apr-15 3:31am    
My 4. I strongly recommend using System.Text.StringBuilder instead of string. Strings are immutable, so repeated concatenation is utterly inefficient. Besides, it's strange to use string.Concat, as '+' is more readable, maintainable and unambiguous. Besides, follow good advice by Tomas Takac in his comment to the question: don't use such names.
—SA
Maciej Los 16-Apr-15 5:13am    
Thank you, Sergey. Three things: 1) i wrote that string is immutable and added link to the article where author explains why string is immutable and shows a way to workaround it; 2) string.Concat is used, because it's pseudo-code (see line above the code). I wanted to exaplain what this piece of code (in this specific line) does; 3) variable naming convention depends on developer's tastes. In Poland we say (it would be hard to understand this statement, but i strongly believe you'll get it): "Two schools are very known: Falenicka School and Otwocka School, both somwhere around Warsaw.". This meant that even both Schools are close eachother, the way they teach students can differ.

Anyway, thank you for your valuable comment.
Tomas Takac 16-Apr-15 5:30am    
Re the variable names. I think using i,j,... for loop indexes is ok. People are generally used to it and can understand it without problems. The problem with OP's code was that he used these one-letter names all over the place like in "if (l == 1 || l == j || i == j)" which total kills the readability. You do no such thing in your code. Just wanted to make myself clear. +5
Maciej Los 16-Apr-15 5:33am    
You're right, Tomas. Thank you for your 5 ;)
Mirza Mustafa Ali Baig 16-Apr-15 6:27am    
Thank you for your reply.
I got all new way to write the code using your article and code.
I admit that i haven't wrote the Code properly i had executed the code with two loops i got the output but not the desired out put what i have needed.
Actually the output what is want is the outer pyramid structure i.e only boundaries of pyramid should have stars but not the entire pyramid.
I hope u got my question right this time.

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