Bordered Plus Pattern Coding Problem

So I've been working on this code to make a bordered plus pattern with just using nested loops and conditional statements, and I cannot figure out how to get this right. I just started learning how to code.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40

#include <iostream>
#include <cstdlib>
using namespace std;
int main()
{
    char character;
    int size;
    cout << "Please enter the character you wish to use: ";
    cin >> character;
    cout << "Please enter the size: ";
    cin >> size;
    if (size <= 0)
    {
        cout << "Impossible.";
        exit(0);
    }
    else
    {
        for (int rows = 0; rows < size; rows++)
        {
            for (int cols = 0; cols < size; cols++)
            {
                if (((rows == 0) || (cols == 0) || (rows == size - 1) || (cols == size - 1) // border
                    || (rows == cols) || (cols == size - 1 - rows)) // Plus pattern
                    && !((((rows == 1) && ((cols == 1) || (cols == size - 2))) //space b/w bord & pat
                    || ((rows == size - 2) && ((cols == 1) || (cols == size - 2))))))
                {
                    cout << character;
                }
                else
                {
                    cout << " ";
                }
            }
            cout << endl;
        }
    }
    return 0;
}
That code seems pretty good for making a diagonal cross.

To make a plus shape is similar, if not a little easier. The border is the same, the inner feature will make use of rows == size/2 for the horizontal, and cols == size/2 for the vertical. Then just add the extra check for leaving the blank between border and pattern.
Topic archived. No new replies allowed.