Pinwheel pattern

this is what i've done so far.

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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
#include <iostream>

using namespace std;

int main()

{

    int inp, h, st[4] = {1,0,2,0}, sp[4] = {0,0,0,0} ,i[5];

    bool Cond = true;

    while(Cond) {

         cout << "Input a number(Odd numbers only): ";

         cin >> inp;

         cin.ignore();

         if ( inp % 2 != 0 )

              Cond = false;

         }

    st[1] = ((inp-3)/2)+2;

    st[3] = ((inp-3)/2)+1;

    sp[0] = (inp-3)/2;

    sp[2] = (inp-3)/2;

    for ( h = 1; h <= inp; h++ ) {

        if ( h > ((inp-1)/2) && h != ((inp-1)/2)+1 ) {

             for ( i[0] = 1; i[0] <= sp[2]; i[0]++ )

                 cout << ' ';

             for ( i[1] = 1; i[1] <= st[2]; i[1]++ )

                 cout << '*';

             for ( i[2] = 1; i[2] <= sp[3]; i[2]++ )

                 cout << ' ';

             for ( i[3] = 1; i[3] <= st[3]; i[3]++ )

                 cout << '*';

             sp[2] --;

             st[2] ++;

             st[3] --;

             sp[3] ++;

             cout << endl;

             }

        else if ( h == ((inp-1)/2)+1 ) {

             for ( i[4] = 1; i[4] <= inp; i[4]++)

                 cout << '*';

             cout << endl;

             }

        else {

             for ( i[0] = 1; i[0] <= st[0]; i[0]++ )

                 cout << '*';

             for ( i[1] = 1; i[1] <= sp[0]; i[1]++ )

                 cout << ' ';

             for ( i[2] = 1; i[2] <= st[1]; i[2]++ )

                 cout << '*';

             st[0] ++;

             sp[0] --;

             st[1] --;

             cout << endl;

             }

        }

    cout << endl;    

    cin.get();

    return 0;

}



but i think this code is too long :o
are there any other effective way of doing this?
Thanks :)
Maybe using a macro? or a function?

Aceix.
A minor simplification, change int i[5]; to int i; and remove all the [index] where i is used.
How does this look
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
41
42
43
44
45
46
47
#include <iostream>

using namespace std;

int main()
{
    int inp;
    bool Cond = true;

    while(Cond) {
        cout << "Input a number(Odd numbers only): ";
        cin >> inp;
        cin.ignore();
        if ( inp % 2 != 0 )
            Cond = false;
    }

    const int n = inp / 2;
    for(int r = 0; r < inp; r++) {
        for(int c = 0; c < inp; c++) {
            bool d = false;

            if(!d)
                d = (r == n) || (c == n);

            if(!d)
                d = (r < n) && (c < n) && (c <= r);

            if(!d)
                d = (r < n) && (c > n) && (c < (inp - r));

            if(!d)
                d = (r > n) && (c < n) && (c > (inp - r - 2));

            if(!d)
                d = (r > n) && (c > n) && (c > r -1);

            if(d)
                cout << '*';
            else
                cout << ' ';
        }
        cout << endl;
    }
    cin.get();
    return 0;
}
Just playing with the code here. This is effectively the same as the code from Santosh Reddy above:
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
#include <iostream>

using namespace std;

int main()
{
    int inp = 0;

    while ((inp&1) == 0) {
        cout << "Input a number(Odd numbers only): ";
        cin >> inp;
        cin.ignore();
    }

    const int mid = inp / 2;
    for (int row = 0; row < inp; row++)
    {
        for (int col = 0; col < inp; col++)
            cout <<
              (( (row == mid) || (col == mid)
              || (row < mid) && (col < mid) && (col <= row)
              || (row < mid) && (col > mid) && (col < (inp - row))
              || (row > mid) && (col < mid) && (col > (inp - row - 2))
              || (row > mid) && (col > mid) && (col > row - 1)
              )?'*':' ');
        cout << endl;
    }
    cin.get();
    return 0;
}
Last edited on
Topic archived. No new replies allowed.