Mrs.H
Mrs.H

Reputation: 79

Print the shape of an X on screen

I want to print an X on screen like this:

*     *
 *   *
  * *
   *
  * *
 *   *
*     *

I tried with this code:

int main(){
    bool back = false;
    for (int i = 0; i < 7; ++i) {
        if (i == 4)
            back = true;   

        if (!back){
            for (int j = 0; j < i; ++j) {
                cout << " ";
            }
        } else{
            for (int j = 7-i-1; j > 0; --j) {
                cout << " ";
            }
        }
        cout << "*" << endl;
    }
}

The result is missing the right half:

*
 *
  *
   *
  *
 *
* 

The problem is that I can't figure out how to print the spaces between the stars and the stars that follow them.

Upvotes: 1

Views: 366

Answers (5)

balki
balki

Reputation: 27684

The pattern consists of two equations: x = y and x + y = 4

Just loop through the axes and plot the points that fall on any of the lines.

                 ( y )
           0   1   2   3   4
 ( x )   ---------------------
   0     | * |   |   |   | * |
         ---------------------
   1     |   | * |   | * |   |
         ---------------------
   2     |   |   | * |   |   |
         ---------------------
   3     |   | * |   | * |   |
         ---------------------
   4     | * |   |   |   | * |
         ---------------------

   Two Equations
   x = y
   x + y = 4

#include <iostream>    

int main() {
    int num_lines = 7;
    auto on_line1 = [](int x, int y) {
        return x == y;
    };

    auto on_line2 = [num_lines](int x, int y) {
        return (x + y) == (num_lines - 1);
    };

    for(int x = 0; x < num_lines; x++) {              // Simple looping
        for(int y = 0; y < num_lines; y++) {          // through the axes

            if(on_line1(x, y) or on_line2(x, y)) {    // If on any of the  line
                std::cout << '*';                     // Then plot it
            } else {
                std::cout << ' ';                     // Else leave it
            }

        }
        std::cout << '\n';
    }

    return 0;
}

PS: I copied the ascii table from the other answer.

Upvotes: 1

Matt
Matt

Reputation: 2802

If you're not required to loop you can create a string and print it.

#include <iostream>
#include <string>

int main(int argc, char * argv[]){

    std::string myX("*     *\n *   * \n  * *  \n   *   \n  * *  \n *   * \n*     *\n");
    std::cout << myX;
    return 0;
}

Upvotes: 0

karlphillip
karlphillip

Reputation: 93468

A more educational approach to solving this problem requires 2 loops.

The first for loop controls the height of the output, i.e. the number of lines printed. Each iteration prints a single line and ends it with a std::endl.

The second is a nested for loop, which controls the width and prints characters horizontally, i.e. it prints asterisk(s) and spaces for that line. Each iteration prints either a space or an asterisk.

This illustration might help to understand the values of the variables when x_size = 5:

                 (width)     
             0   1   2   3   4
(height)   ---------------------
   0       | * |   |   |   | * |      asterisk_pos = 0, end_pos = 4, inc =  1
           ---------------------
   1       |   | * |   | * |   |      asterisk_pos = 1, end_pos = 3, inc =  1
           ---------------------
   2       |   |   | * |   |   |      asterisk_pos = 2, end_pos = 2, inc =  1
           ---------------------
   3       |   | * |   | * |   |      asterisk_pos = 1, end_pos = 3, inc = -1
           ---------------------
   4       | * |   |   |   | * |      asterisk_pos = 0, end_pos = 4, inc = -1
           ---------------------

Source code:

int main()
{
    int x_size = 7;        // size of the drawing
    int asterisk_pos = 0;  // initial position of the asterisk
    int inc = 1;           // amount of increment added to asterisk_pos after an entire line has been printed

    // height is the line number
    for (int height = 0; height < x_size; height++)
    {
        // width is the column position of the character that needs to be printed for a given line
        for (int width = 0; width < x_size; width++)
        {
            int end_pos = (x_size - width) - 1; // the position of the 2nd asterisk on the line

            if (asterisk_pos == width || asterisk_pos == end_pos)
                cout << "*";
            else
                cout << " ";
        }

        // print a new line character
        cout << std::endl;

        /* when the middle of x_size is reached, 
         * it's time to decrease the position of the asterisk!
         */
        asterisk_pos += inc;    
        if (asterisk_pos > (x_size/2)-1)
            inc *= -1;
    }    

    return 0;
}

Output with x_size = 7:

*     *
 *   * 
  * *  
   *   
  * *  
 *   * 
*     *

Output with x_size = 3:

* *
 * 
* *

Upvotes: 3

Ali Faris
Ali Faris

Reputation: 18610

spaces between in upper part are decreasing by 2 and start with line - 2

spaces between in down part are incensing by 2

here how I solve your problem

void printSpaces(int count)
{
    for (int i = 0; i < count; ++i) {
        cout << " ";
    }
}

int main()
{
    int lines = 7;
    int spaceBefore = 0;
    int spaceBetween = lines - 2;
    bool backword = false;

    for (int i = 0; i < lines; ++i)
    {
        printSpaces(spaceBefore);

        cout << "*";

        if (spaceBetween > 0)
        {
            printSpaces(spaceBetween);
            cout << "*";
        }
        else
        {
            backword = true;
        }

        cout << "\n";

        spaceBefore = backword ? spaceBefore-1 : spaceBefore+1;
        spaceBetween = backword ? spaceBetween+2 : spaceBetween-2;
    }

    return 0;
}

Upvotes: 1

Jean-Baptiste Yun&#232;s
Jean-Baptiste Yun&#232;s

Reputation: 36431

Observe the sequence in each line. Look at the first part you have:

  • 0 space, 1 *, 5 spaces, 1 *, 0 space
  • 1 space, 1 *, 3 spaces, 1 *, 1 space
  • 2 spaces, 1 *, 1 space, 1 *, 2 spaces

Then for line i: i spaces followed by 1 * followed by 5-2 i spaces, followed by 1 *, followed by i spaces

Then the following should work:

for (int line=0; line<3; line++) {
    for (int n=0; n<line; n++) cout << ' ';
    cout << '*';
    for (int n=0; n<5-2*line; n++) cout << ' ';
    cout << '*';
    for (int n=0; n<line; n++) cout << ' ';
    cout << endl;
}

The middle line 3 is obvious, and the following is the reverse of the first part.

Another way is to observe sequence of positions of *: (0,6) (1,5) (2,4) (3,3) (4,2) (5,1) (6,0), thus:

for (int line=0; line<7; line++) {
    int pos1 = line;
    int pos2 = 6-line;
    for (int n=0; n<7; n++) {
        if (n==pos1 || n==pos2) cout << '*';
        else cout << ' ';
    }
    cout << endl;
}

You can then obviously remove pos1 and pos2...

Upvotes: 3

Related Questions