Search code examples
c++algorithmshapessquare

How to print a X with two letters inside a square


I am trying to make the this shape but stuck at here. I tried with counters more than one but failed again.

P p p p p p Q
p P p p p Q q
p p P p Q q q
p p p Q q q q
p p Q q Q q q
p Q q q q Q q
Q q q q q q Q
int main()
{  
    int loopLimit = 7;
    int upperCharacter = 0;
    
    do {
        int loopCounter = 0;

        do {
            if (loopCounter == upperCharacter) std::cout << "P ";
            else std::cout << "p ";
            loopCounter = loopCounter + 1;
        } while (loopCounter < loopLimit);

        upperCharacter = upperCharacter + 1;
        loopLimit = loopLimit - 1;
        std::cout << "\n";
    } while (loopLimit > 0);
}

My code currently outputs:

P p p p p p p 
p P p p p p 
p p P p p 
p p p P 
p p p 
p p 
p 

Solution

  • A pen and a paper solved this question.

    Here's the solution:

    for (int satirSayaci = 1; satirSayaci < 8; satirSayaci++) {
            for (int karakterSayaci = 1; karakterSayaci < 8; karakterSayaci++)
            {
                if (karakterSayaci >= 8 - satirSayaci) {
                    if (karakterSayaci == 8 - satirSayaci || karakterSayaci == satirSayaci) std::cout << "X ";
                    else std::cout << "x ";
                } else {
                    if (karakterSayaci == satirSayaci) std::cout << "P ";
                    else std::cout << "p ";
                }
            }
    
            // Yeni bir satir yazdir
            std::cout << std::endl;
        }