0

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;

}

Edited by Nick Evan: added question

3
Contributors
6
Replies
7
Views
7 Years
Discussion Span
Last Post by sidra 100
0

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.

0

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

1

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)
}
0

thanks very much i will try with that

0

managed it without the break.

used two for loops

thank you very much for your help

-2

can u write the whole program 4 me nw

Votes + Comments
Too lazy to even write all of the letters when asking others to do your homework?
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.