Member Avatar for kellnerq

Question-
Write a function that displays at the left margin of the screen a solid square of asterisks whose
side is specified in integer parameter side. For example, if side is 4, the function displays:
Sample Screen Display
****
****
****
****

i have come this far but i get the output

*
*
*
*
*
*

if i put in side as 2

here is my code

#include <iostream>

using std::cin;
using std::cout;
using std::endl;

int main ()

{
	int side;

	cout << "This program will display a solid square of asterisks whose side is specified by the user." << endl;

	cout << "Please enter an integer specifying the length of the side." << endl;
	cin >> side;

	for ( int i = 0 ; i < side ; i++)

	{
		cout << " * " << endl;


		for ( int j = 0 ; j < side ; j++)

		{

			cout << " * " << endl;

		}

	}

	return 0;

}

Recommended Answers

All 6 Replies

Hint: Only use the outer for loop to control the lines, use the inner for loop to control the number of stars on each line. So therefore, you only need to put in the line breaks at a certain point.

Member Avatar for kellnerq

do i need to put an if loop into the inner for loop?

i tried it with taking out the endl in line 27.

the outcome if i type in 4 is:

*
*****
*****
*****
****

i then changed line 23 to:

for ( int j = 0 ; j < side -1 ; j++)

that changed my output to
*
****
****
****
***

thanks for the tip.
I'm nearly there

No, you want to go from 0 to side, since you loop is j<side, in this case side is 4, giving 0 1 2 3 (so 4 stars). You don't need the star at the beginning either.

So it goes:

for loop (over the rows)
{     
         for loop over the stars
         {
                 Print stars
         }

         line break (cause once we're done here we're off to the next row 
                           in the loop)
}
Member Avatar for kellnerq

thanks very much i will try with that

Member Avatar for kellnerq

managed it without the break.

used two for loops

thank you very much for your help

can u write the whole program 4 me nw

commented: Too lazy to even write all of the letters when asking others to do your homework? -2
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.