Help?

Write a program that prints the following pattern
*
* *
* * *
* * * *
* * * * *
* * * * * *
There are int n lines of asterisks. All asterisks * should be printed with a single cout << "* ";
Use two nested for loops.

Not sure how to do this one :(


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>
#include <math.h>
using namespace std;

int main() {

	int n,i;
	cout << "Enter n=";
	cin >> n;
	for ( i=1; i <= n; i++)
	
	
	cout << "*\n" << "**\n" << "***\n"<< "****\n" << "*****\n" << "******\n";

	return 0;
	}

this isnt right is it? cuz im not using the nested loop.
Last edited on
You're right to say that that is incorrect because you aren't using a nested for loop.

What you're doing with that code is printing the pattern you have pasted up top N times.

Say N = 2, output would be:

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



Your i loop looks good, now try a nested loop. I won't give you the loop, although it isn't too difficult if you think about what you want the number of asterisks to be per line, but I will say that rather than doing:

cout << "*\n";

which will give you a new line after every asterisk, you should format your loops as:

1
2
3
4
5
6
7
8
9
	for ( i=1; i <= n; i++)
	  {
	    for ( You do this)
	    {
	
	  cout << "*";
	    }
	    cout << endl;
	  }


To allow the nested loop to traverse as much as necessary before going to the next line.

Hope that helps.
Last edited on
Cheater, doing that only works if the number of lines is 6.
 
cout << "*\n" << "**\n" << "***\n"<< "****\n" << "*****\n" << "******\n";

You will need a nested for loop, the outer for loop counts the amount of lines and the inner prints the asterisks.
how about now? is it right?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>
using namespace std;

int main() {

	int i,k;
	for ( i=1; i <= 6; i++)
	{
	for ( k=1; k <= i;k++)
	{
	cout << "*";
	}
	cout <<endl;
	}
	return 0;
	}
Last edited on
and what's the difference between using the loops to print this pattern and just printing the pattern straight forward by using
1
2
3
4
5
6
7
8
9
#include <iostream>
using namespace std;
int main() {
cout <<" *"<<endl;
cout <<" **"<<endl;
cout <<" ***"<<endl;
cout <<" ****"<<endl;
cout <<" *****"<<endl;
cout <<" ******"<<endl;
Last edited on
and what's the difference between using the loops to print this pattern and just printing the pattern straight forward by using


What would you do if you had to print 200 *'s ? 200 Lines of code? What about 1000 *'s? What if it changed to being $ instead of * ? This is the essence of why programming languages have such things as loops - so you don't have to mindlessly repeat code. The idea is to get the computer to do the work for you.

The idiom for doing something n times is this:

1
2
3
4
const int n = 10;    //helps avoid magic numbers in the code
for (int a = 0; a < n; ++a) {  //uses a variable instead of a magic number
    // do something
}


Try to avoid using the <= operator and start from 0 not 1. This is because when you learn about arrays or many other types of containers, the subscripts start at 0. And if you use <= you will go past the end, causing the program to crash.
so my code is right?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>
using namespace std;

int main() {

	int i,k;
	for ( i=1; i <= n; i++)
	{
	for ( k=1; k <= i;k++)
	{
	cout << "*";
	}
	cout <<endl;
	}
	return 0;
	}
The assignment looks like it wants for loops

There are int n lines of asterisks. All asterisks * should be printed with a single cout << "* ";
Use two nested for loops.


1
2
3
4
5
6
7
8
for( int rows = 0; rows < N; ++rows )
{
    for( int columns = 0; columns < rows + 1;  ++columns )
    {
        std::cout << '*';
    }
    std::cout << std::endl;
}


http://ideone.com/xBOejD

*forgot to add one to rows
Last edited on
so my code is right?


You didn't pay attention to this part:

TheIdeasMan wrote:
Try to avoid using the <= operator and start from 0 not 1. This is because when you learn about arrays or many other types of containers, the subscripts start at 0. And if you use <= you will go past the end, causing the program to crash.


Also, look at the indentation in giblit's code - see how much nicer it looks?

Last edited on
so my code is right?

Well did you test it? The biggest thing about writing programs is not writing them, its testing them. Type it into your IDE, press run, and see what happens.

If an error comes up that you cannot figure out, ask the forums.
Topic archived. No new replies allowed.