Comments
However, I was wondering if there are any other optimized way to print this pyramid, since mine includes 4 loops.
Probably a lot of ways to optimize the code. But nothing worth spending time over. The compiler will do a great job here.
I would not worry about the number of loops, that is not a big issue. It's the number of nested loops that is going to make code bad or have high complexity. You only have two layers of nesting so not that much to worry about.
Style Issues
Don't do this:
using namespace std;
Bad habit that will cause problems in the future.
Be specific in what you want to use.
My biggest issues with your style is readability:
for(int i=1;i<=a;i++)
You don't need to squash everything together as tight as possible. Put in some horizontal white space to make it readable.
for(int i=1 ; i <= a; ++i) // Side note prefer ++i to i++
And:
cout<<(k)<<" "; // My poor eyes are bleeding trying to read that.
Don't use std::endl
.
cout<<endl;
Forcing a flush of the stream is rarely (if ever) needed to be done manually like this. Simply use the "\n"
to add a newline.
Main does not need a return if it is always going to succeed
return 0;
Better Code
You could have a better naming convention for your variables.
Yes everybody and their grandfather used to use i
, j
, k
for their loop names. But have you ever tried to find all instances of the variable i
in even a moderately sized piece of code with comments? You get so many false positives that it's a pain.
Use readable and meaningful names for your variables. Something that is easy to search for and spot in the code and also something the conveys meaning to the reader.
Check that the read worked.
int a;
cin>>a;
If I type Loki
as input then a
has some random value in it. What the rest of the code will do then is technically undefined as you have not set the value, so reading from it is undefined.
for(int k=i;k<=(i+i-1);k++)
The max is (i+i-1)
Is this not the same as 2*i-1
and you are using <=
as the comparison so we could drop the -1
and use a less than.
for(int k=i; k < 2*i; ++k)
The only reason you are using 2*i
is that you start at i
and count up. It seems like this would be easier to read by adding i
to the output.
for(int k=0; k < i; ++k)
std::cout << (k + i) << " "; // Start at i count up.
OK. The next trick is probably not the most efficient way of doing something. But it is a reasonable trick for small layout examples like this. I would not do it for anything more than simple text layouts unless you think about it and decide it's worth it.
for(int j=a-i;j>=0;j--)
{
cout<<" ";
}
// We can replace this by printing a dynamically created string.
std::cout << std::string(' ', (a - i));
#include<iostream>
void printPryamid(int size)
{
for(int row = 1; row <= size; ++row)
{
std::cout << std::string((size - row) *2, ' ');
int loop;
for(loop = 0; loop < row; ++loop)
{
std::cout << (row + loop) << " ";
}
for(--loop; loop > 0; --loop)
{
std::cout << (row + loop - 1) << " ";
}
std::cout << "\n";
}
}
int main()
{
int size;
if (std::cin >> size)
{
printPryamid(size);
}
else
{
std::cout << "User Error on Input\n";
}
}