Advertisement
pmcgee

Diamond via string

Nov 7th, 2020
2,276
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.99 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <vector>
  4. #include <algorithm>
  5. using namespace std;
  6.  
  7. int main(void) {
  8.     int i, j, k, s, no;
  9.     char sym = '#';
  10.     no = 11;                  // pattern width = odd
  11.     s  = (no /2) +1;          // mid-point
  12.  
  13.     string st;
  14.     for (i=1;  i<s; ++i)      st.push_back(' ');
  15.     int p = s-1;
  16.  
  17.     for (i=1;  i<s; ++i) {
  18.         cout << st << sym;    reverse(st.begin(), st.end());
  19.         cout << st << endl;   reverse(st.begin(), st.end());
  20.         --p;
  21.         st[p] = sym;
  22.     }
  23.         cout << st << sym;    reverse(st.begin(), st.end());
  24.         cout << st << endl;   reverse(st.begin(), st.end());
  25.         --p;
  26.  
  27.     for (i=1;  i<s; ++i) {
  28.         cout << st << sym;    reverse(st.begin(), st.end());
  29.         cout << st << endl;   reverse(st.begin(), st.end());
  30.         ++p;
  31.         st[p] = ' ';
  32.     }
  33.         cout << st << sym;    reverse(st.begin(), st.end());
  34.         cout << st << endl;   reverse(st.begin(), st.end());
  35. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement