Hi Everyone,

I have been stumped by this problem. I need to create a pattern such as:

1
21
221
2221
22221

Using nested for loops. I have something that does (A)

222221
222221
222221
222221
222221

and used to have something that did (B)

1
21
221
2221
22221
222221
2222222

Any hints would be helpful.

Post your code for B please. That's the closest you have, and it'll probably be easier to fix a minor bug than direct you toward the appropriate algorithm.

Start simpler. Create a function with the following signature:

void repeat_character(const char symbol, const int amount)

what it should do is it prints 'symbol' to stdout 'amount' times. So, print_character('2', 5); should print

22222

Once you have that solving your original problem should be easier. If you don't know how functions work yet you cuold either read into it, or you could fill in the following section between parenthesis:

void repeat_character(const char symbol, const int amount)
{
}

as if you were coding in 'main' where 'symbol' and 'amount' can be used without having to declare them in the body or assign values to them. Once you've managed to build this you could post it here and we could proceed with the rest.

-edit-
You can modify the function signature I suppose (e.g. non-const 'amount' or using an unsigned integer) though it's goal will be the same. It will make your initial problem trivial.

commented: Much better advice +14

The nested two for loops are usually the easiest way to make designs with rows and columns. You just need to see the pattern that is used, to make the design you want.

Use the outer for loop for the row control, and the inner for loop to control the chars being printed in the current row.

You need to add logic to both the inner, and the outer for loop, for this to work. Be careful that row logic goes in the outer for loop, and column logic goes in the inner for loop, only.

That's a good exercise for you to work with!

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.