### Write a program that...

Write a program that reads in the size of the side of a square and then prints a hollow square of that size out of asterisks and blanks. Your program should work for square of all side sized between 1 and 20. for example, if your program reads a size of 5, it should print.
 ``` ***** * * * * * * ***** ```
Last edited on
You first.
closed account (3qX21hU5)
Also your square is kind of messed up ;p
How far have you gotten?

What ideas do you have on how you could complete this?
@ zerero, sorry it was due to my post ignores multiple spaces, but here I got how to do that..output should like this.

 ``` ***** * * * * * * ***** ```

I have no idea, I tried a lot..
managed to make square, but no idea to put spaces.
 ``12345678910111213141516171819202122232425`` ``````#include using namespace std; int main() { int x, square, c; c = 1; cout << "Enter side of a square: "; cin >> x; square = x * x; while ( c <= square ) { cout << "*"; if ( c % x == 0 ) cout << "\n"; c++; } cout << endl; return 0; }``````

and out put for 5 is:
 ``` ***** ***** ***** ***** ***** ```
Solved by Bikesh Kumar //very easy
 ``123456789101112131415161718192021`` ``````#include #include void main() { clrscr(); int n,i,j; cout<<"Enter Size of Square : "; cin>>n; for(i=1;i<=n;i++) { for(j=1;j<=n;j++) { if(i==1 || i==n || j==1 || j==n) cout<<"*"; else cout<<" "; } cout<

4 hours ago · Like · 1

 ``12345678910111213141516171819202122232425262728`` ``````/* Recode by Dipen45 since to use with while */ #include using namespace std; int main() { int i, j, num; i = 1; j = 1; cout << "Enter a side of square: "; cin >> num; while ( i <= num ) { j = 1; while ( j <= num ) { if ( i == 1 || i == num || j == 1 || j == num ) cout << "*"; // if any one out of 4 argument is true then it will print * otherwise ' ' else cout << " "; // prints if i=2 and j = 2 j++; } cout << endl; i++; } return 0; }``````
I don't know why the forum users are not helping.
they just say..do your own homework, you first or something like that.
 ``123456789101112131415161718192021`` ``````#include #include int main() { const unsigned int MAX_SIDE = 20; unsigned int x = 0; do { std::cout << "Enter a positive number less than " << MAX_SIDE + 1 << ": "; std::cin >> x; } while ( !x || MAX_SIDE < x ); std::cout << "\Here you are:\n\n" << std::right; for ( unsigned i = 0; i < x; i++ ) { std::cout << std::setfill( i % ( x - 1 ) ? ' ' : '*' ); std::cout << '*' << std::setw( x ) << "*\n"; } }``````
Last edited on
You would do well to test your code with a size of 1.

Just to illustrate a different approach:
 ``1234567891011121314151617181920212223242526272829303132333435`` ``````#include #include void print_square( unsigned size ) { using std::string ; if ( size == 0 ) return ; if ( size == 1 ) { std::cout << "*\n" ; return ; } string edge = string(size, '*') + '\n' ; string middle = "*" + string(size-2, ' ') + "*\n" ; std::cout << edge ; for ( unsigned i=0; i < size-2; ++i ) std::cout << middle ; std::cout << edge ; } int main() { unsigned num ; std::cout << "Enter a side of square: " ; std::cin >> num ; print_square(num) ; }``````
For the size equal to 1 it is enough to insert if statemeent

#include <iostream>
#include <opmanip>

int main()
{
const unsigned int MAX_SIDE = 20;
unsigned int x = 0;

do
{
std::cout << "Enter a positive number less than " << MAX_SIDE + 1 << ": ";
std::cin >> x;
} while ( !x || MAX_SIDE < x );

std::cout << "\Here you are:\n\n" << std::right;
for ( unsigned i = 0; i < x; i++ )
{
if ( x == 1 ) std::cout << "*\n";
else
{
std::cout << std::setfill( i % ( x - 1 ) ? ' ' : '*' );
std::cout << '*' << std::setw( x ) << "*\n";
}
}
}
Topic archived. No new replies allowed.