### Pinwheel pattern

this is what i've done so far.

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109`` ``````#include 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
 ``1234567891011121314151617181920212223242526272829303132333435363738394041424344454647`` ``````#include 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:
 ``123456789101112131415161718192021222324252627282930`` ``````#include 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.