Re: I need to create an inverted pyramid, please help me
ZikO wrote:
You need to learn to quote properly. Where's attribution?
#include <iostream>
int main()
{
std::cout << "******\n"
<< " ****\n"
<< " **\n"
<< " *\n";
}
It's only appears to be easy this way because you have focused only
on 4 rows. I believe OP wants to work out an algorithm which is more
general
P. let's 50 rows. That's not easy by your way.
#include <string>
#include <iostream>
#include <ostream>
template<size_t spaces, size_t stars> void contents() {
std::cout << std::string(spaces, ' ')
<< std::string(stars>1?(stars-1)*2:1, '*');
}
template<size_t offset, size_t sp, size_t st> void row() {
std::cout << std::string(offset, ' ');
contents<sp,st>();
std::cout << std::endl;
}
template<size_t howmany, size_t offset = 0> struct pyramid
{
pyramid() {
row<offset,0,howmany>();
pyramid<howmany-1,offset+1>();
}
};
template<size_t offset> struct pyramid<0,offset>
{
};
int main() {
pyramid<50>();
}
----------------------------------------- or
#include <string>
#include <iostream>
#include <ostream>
void contents(size_t spaces, size_t stars) {
std::cout << std::string(spaces, ' ')
<< std::string(stars>1?(stars-1)*2:1, '*');
}
void row(size_t offset, size_t sp, size_t st) {
std::cout << std::string(offset, ' ');
contents(sp, st);
std::cout << std::endl;
}
void pyramid(size_t howmany, size_t offset = 0)
{
if (howmany > 0)
{
row(offset,0,howmany);
pyramid(howmany-1,offset+1);
}
}
int main() {
pyramid(50);
}
I like the former better. I am sure it can be simplified.
V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask