ashur
ashur

Reputation: 4317

C++: Displaying characters

I'm learning and improving my programming skills from "Think Like a programmer" book and I was asked to display this kind of pyramid.

########
 ######
  ####
   ##

I did it with this code

for(int i = 0; i < 4; i++){

    for(int k = 0; k < i; k++)
        cout << ' ';

    for(int j = 0; j < 8 - i * 2; j++)
        cout << '#';

    cout << '\n';
}

But... the questions was "Using the same rule as the shapes programs from earlier in the chapter (only two output statements—one that outputs the hash mark and one that outputs an end-of-line), write a program that produces the following shape:"

I'm not sure, but is it possible to display something like this with only 2 statements and without using space character?

edit.

Thanks for an answer guys. But according to author I should do this with only cout << '#' and cout << '\n'. And here is my point, because it seems that manipulating with some methods or functions is not an option.

Write a program that uses only two output statements, cout << "#" and cout << "\n", to produce a pattern of hash symbols shaped like a ... Of Course with use of loops :P

Upvotes: 8

Views: 1856

Answers (4)

V Anton Spraul
V Anton Spraul

Reputation: 86

Unfortunately, the answer to this question is much simpler: author error. Those first few questions should have been worded to allow the use of a single space output as well (cout << " ";). I don't know how we all missed this in editing, and I apologize for the confusion this has caused. In general, any exercise that is intended to force the reader to go "digging" will be clear on that point.

By the way, this and other issues from the first edition are discussed in the document that can be found on my site here. If you have further questions or problems with the book, please do contact me.

Upvotes: 7

Anne van Rossum
Anne van Rossum

Reputation: 3149

I don't know if it's about the "art" of not using cout << ' ', but in that case there is another neat solution:

#include <iostream>

int main() {
    for (int i = 0; i < 4; ++i, std::cout << '\n') {
        for (int j = 0; j < 8-i; ++j) {
            std::cout << (char)('#' - 3*(j<i));
        }
    }
}

And, another, even more obscure version that uses the ASCII representation rather than writing ' ' explicitly, using only one for-loop:

int main() {
    for (int i = 1; i < 37; ++i) {
        std::cout << (char)(35-3*((i%9)<=((i-1)/9)) - 3*((i%9)>=(9-(i-1)/9)) \
            - 22*(!(i%9)) );
    }
}

I do not say this is good programming practice, but maybe it helps someone in realizing - again - that it's in the end all numbers and registers.

Upvotes: 0

user1300630
user1300630

Reputation:

You can check the position in a loop for every line. This combined with a 3 operand operator could solve this with two output statement.

for(int i = 0; i < 4; i++){

    for(int k = 0; k <= 8-i; k++)
        cout << k<i ? ' ' : '#';

    cout << endl;
}

Leading space characters will be there anyway, You can not make an "empty" space instead of them.

Upvotes: 0

paddy
paddy

Reputation: 63481

#include <iostream>
#include <iomanip>
#include <string>

using namespace std;

int main()
{
    for(int i = 0; i < 4; i++) {
        cout << setw(8-i) << string(8-i*2, '#') << endl;
    }
    return 0;
}

Upvotes: 10

Related Questions