I can not cope with the task of programming. There is a task: to write a program so that it displays a hollow square. Use only while.

#include <stdio.h> #include <stdlib.h> main() { int a = 0; printf("Please input side a"); scanf("%d", &a); int count = 1; while (count <= a) { printf("*"); count++; } printf("n"); int count2 = 1; while (count2 <= (a - 2)) { a // ???????? count2++; } int count3 = 1; while (count <= a) { printf("*"); count3++; } system("PAUSE"); } 

I understood how to make a full square be displayed, but with a hollow one it does not work, I can not understand more precisely how to put * and at the end * , and in the middle there are spaces.



    2 answers 2

     #include <stdio.h> #define SQUARE_WIDTH 10 int main() { int height = SQUARE_WIDTH, i; while( height ) { putchar( '*' ); i = SQUARE_WIDTH - 2; while( i-- ) { if( height == SQUARE_WIDTH || height == 1 ) putchar( '*' ); else putchar( ' ' ); } putchar( '*' ); putchar( '\n' ); height--; } return SQUARE_WIDTH; } 
    • Thank you very much, but can I give an example easier? I still do not understand much, I didn’t want to go further, without a basic accumulation of knowledge. For example, how have I described it possible to do so? and without a constant? - Stee1House
    • It is described:> write a program so that it displays a hollow square. Use only while. So done, how even easier I do not know. The input of the side is implemented via scanf (), so it did not repeat. Well, insert it instead of height = SQUARE_HEIGHT, nothing will change in principle :) - user6550
    • Well, I did a little differently without a condition .. I don’t know what’s wrong, maybe from the outside, but with my knowledge I could only do this: int count3 = 3; while (count3 <= a) {printf ("*"); int count4 = 1; while (count4 <= (a - 2)) {printf (""); count4 ++; } printf ("* \ n"); count3 ++; } - Stee1House

    So? (I forgot about the while)

     #include <stdio.h> int main () { int a = 0; int i = 0; printf ("Please input side a>"); scanf ("%d", &a); while(++i <= a) { int j = 0; while (++j <= a) putchar((i == 1 || j == 1 || i == a || j == a) ? '*' : ' '); putchar('\n'); } return 0; } 

    If the row or column number is 1 or the side of a square, type an asterisk, otherwise, press a space. After the end of the line print \n

    • Thanks, interesting, more and more amazed how you can do the same thing in very different ways, and all are true in principle. - Stee1House