# Printing a diamond of numbers

If you input the number 3 from the keyboard, the program will show this:

      0
0 1 0
0 1 2 1 0
0 1 2 3 2 1 0
0 1 2 1 0
0 1 0
0


Here is my code:

#include <iostream>

using namespace std;

int main()
{
unsigned i,k=0,n;
cout<<"n= "; cin>>n;

while(k<=n)
{

for (i=0;i<=k;i++)
cout<<i;
for (i=k;i>0;i--)
cout<<i-1;
cout<<endl;
k++;
}

k=n;

while (k)
{
k--;
for (i=0;i<=k;i++)
cout<<i;
for (i=k;i>0;i--)
cout<<i-1;
cout<<endl;

}
}


I have two questions:

1. Is there a better/easier/clever way to do this?

2. How to deal with spaces? How to show them? (because my code is working but it doesn't show spaces to make this diamond look)

• using namespace std is not preferred, although not that bad for small programs.

• Each variable should be declared/initialized on separate lines. This improves readability and also makes it easier to add any necessary comments.

unsigned i;
unsigned k = 0;
unsigned n;


Same with this:

cout << "n= ";
cin >> n;

• Prefer "\n" to std::endl here (the latter flushes the buffer, which takes longer). It's still okay to use the latter where both flushing and newlining are needed.

• Always use descriptive names for variables. Single-characters are best for loop counters (such as i). This will vastly improve readability as you won't need comments to describe them.

• It looks like you could use recursion instead of all these loops. It may take longer (if you input a large number), but it should at least simplify the logic.

• You're absolutely right. Professors from high school where I learn are a little overwhelmed by these practices. I used to work in their way unfortunately. Thank you so much. – theDDM Oct 22 '13 at 20:00
• @user2105306: Heck, my college professors still use using namespace std just because it's used in the books... – Jamal Oct 22 '13 at 20:02
• Out of interest: why is using namespace std frowned upon? – Kent Boogaart Oct 23 '13 at 2:46
• @KentBoogaart: That link explains most of it. Personally, I prefer to keep the STL separate from other code, since there's a lot of info in that namespace. Beyond that, it looks like many just dislike using namespace X in general. Although it's okay to have it local, I'm used to not using it at all. I was first told about this in my first question here. – Jamal Oct 23 '13 at 2:55
• Sorry, didn't notice your link. I find the argument unconvincing, especially for std. It's like saying don't do using System; in .NET and instead write System.Bla or global::System.Bla everywhere. If I later update a dependency that also includes, say, a Tuple<T> type, then I can disambiguate as necessary at that point. – Kent Boogaart Oct 23 '13 at 8:45

Spacing could be accomplished using std::setw().

The diamond-generating code should at least be in its own function — I suggest a function like void diamond(std::ostream &out, int size). Keep main() simple: just read the size and call diamond(std::cout, n). Better yet, you could define a Diamond class — see my answer to a similar question.

From the linked example, also pick up some ideas about the loop structure and descriptive variable naming. Since this is an exercise and you have quite a bit of work ahead of you, I'll refrain from posting too many details here.