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.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include <iostream>
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
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include<iostream.h>
#include<conio.h>
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<<endl;
}
getch();
}

4 hours ago · Like · 1

Pasted from <http://www.facebook.com/groups/programmingfever/498468720198044/?comment_id=498658436845739&ref=notif&notif_t=group_comment>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
/* Recode by Dipen45 since to use with while */
#include <iostream>
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.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#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++ )
	{
		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:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#include <iostream>
#include <string>

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.