Hello!
With an ordinary square, everything is simple, but how to put a smaller square into it, this is a problem ... Help please)

#include <iostream> using namespace std; void main() //Вывести на экран квадрат в квадрате { setlocale(LC_ALL, "Russian"); int side; cout << "Введите 1 сторону квадрата = "; cin >> side; for (int i = 1; i <= side ; i++) { for (int j = 1; j <= side * 2; j++) { if (i == 1 || j == side * 2 || j == 1 || i == side ) cout << '*'; else cout << ' '; } for (int j = 1; j <= side; j++) { if ( i == side / 3) cout << '*'; else cout << ' '; } cout << endl; } } 

I have already started to think of applying the third for with my if, but the condition for this if does not go to my head ... i.e. 21 line is not correct (

  • inner square 3x3 stars in the center? or may be of different size and it is not known where it is located? why in the second side * 2 loop side * 2 number of iterations? - Grundy
  • This task is much easier to solve if you do one trick - to draw in memory. That is, we start an array of char a[80][25] and draw all the shapes on it. And then, in two cycles (and you can, if you think a little and with one, and even without a cycle) draw everything. - KoVadim
  • They would bring at least what the result should look like. Simply, if I understand correctly, then one cycle is enough. This is a square :) - Sublihim
  • I talked about loops for drawing an array onto the screen. And how to draw a square is a well-known case. - KoVadim
  • @KoVadim, yes, I actually had in mind the example given) - Sublihim

1 answer 1

Here is my implementation of your task.

 #include <iostream> #include <stdio.h> using namespace std; int main() { int a, b; int k = 0; cout << "INPUT SIZE 1: "; cin >> a; cout << "INPUT SIZE 2: "; cin >> b; if(a >= 20 || b >= 20){ cout << "Size >= 20 ! is err"; return 0; } if(a == b){ cout << "Size 1 == size 2 ! is err"; return 0; } if(a > b){ k = (a - b) / 2; } if(a < b){ k = (b - a) / 2; } char arr[20][20] = {0}; for(int i=0; i<a; i++){ arr[i][0] = '*'; arr[0][i] = '*'; arr[a-1][i] = '*'; arr[i][a-1] = '*'; } b += k; for(int i=k; i<b; i++){ arr[i][k] = '*'; arr[k][i] = '*'; arr[b-1][i] = '*'; arr[i][b-1] = '*'; } for(int i=0;i<20;i++){ for(int j=0;j<20;j++){ cout << arr[i][j]; } cout << endl; } getchar(); return 0; } 

Result Screen

  • The inner square should be in the middle of the outer square, and you can’t use anything other than for and if - Eugene
  • That's better? I hope helped - Eugene Sukhodolskiy