Advertisement
Exunys

Printing a hollow diamond shape inside a square (interactive)

Apr 11th, 2024 (edited)
841
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.75 KB | None | 0 0
  1. #include <iostream>
  2. #include <iomanip>
  3.  
  4. int drawDiamond(int, char);
  5.  
  6. using std::cout;
  7. using std::cin;
  8. using std::endl;
  9. using std::setw;
  10.  
  11. int main() {
  12.     int size;
  13.     char character;
  14.  
  15.     back_size:
  16.  
  17.     cout << "Input the size of the canvas <uint>: ";
  18.     cin >> size;
  19.  
  20.     if (static_cast<int>(size) < 3) {
  21.         cout << "Enter a number equal to / larger than 3!\n\n";
  22.         goto back_size;
  23.     }
  24.  
  25.     cout << "Input the character you want to be used <char>: ";
  26.     cin >> character;
  27.  
  28.     cout << endl;
  29.  
  30.     return drawDiamond(size, character);
  31. }
  32.  
  33. int drawDiamond(int size, char character) {
  34.     int i, j, wsize = (size - (size % 2)) / (size / 2);
  35.     int half = size / 2;
  36.  
  37.     cout << setw(wsize);
  38.  
  39.     // First half
  40.  
  41.     for (i = 1; i <= half + 1; i++) {
  42.         for (j = 1; j <= half + 1 - i; j++) {
  43.             cout << setw(wsize) << character;
  44.         }
  45.  
  46.         for (j = 1; j <= 2 * i - 1; j++) {
  47.             if (j == 1 || j == 2 * i - 1)
  48.                 cout << setw(wsize) << character;
  49.             else
  50.                 cout << setw(wsize) << ' ';
  51.         }
  52.  
  53.         for (j = 1; j <= half + 1 - i; j++) {
  54.             cout << setw(wsize) << character;
  55.         }
  56.  
  57.         cout << endl;
  58.     }
  59.  
  60.     // Second half
  61.  
  62.     for (i = half; i >= 1; i--) {
  63.         for (j = 1; j <= half + 1 - i; j++) {
  64.             cout << setw(wsize) << character;
  65.         }
  66.  
  67.         for (j = 1; j <= 2 * i - 1; j++) {
  68.             if (j == 1 || j == 2 * i - 1)
  69.                 cout << setw(wsize) << character;
  70.             else
  71.                 cout << setw(wsize) << ' ';
  72.         }
  73.  
  74.         for (j = 1; j <= half + 1 - i; j++) {
  75.             cout << setw(wsize) << character;
  76.         }
  77.  
  78.         cout << endl;
  79.     }
  80.  
  81.     return 0; // Return a uint to the OS
  82. }
  83.  
  84. // Exunys <3
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement