This problem is to print a square using a number that the user enters, which would be in the center. If the user entered a 5, then the program should output this:

111111111

122222221

123333321

123444321

123454321

123444321

123333321

122222221

111111111

If you're wondering, this was not a homework assignment. It was on a quiz, and I did not know how to do it. So if someone could give me at the very least some guidance on how to solve this problem, then that'd be nice. I know that I need to use for loops, but I'm not quite sure how. Thank you for your time and assistance.

Seemingly, the number has to be between 1 and 9, right?

...but if this is on a quiz, why would you want someone else to answer it?

The program sounds fun so I did it myself..used a whole mess of if else statements inside an inner loop...though the program might be simplified if you use a recursive function but I'm not sure how to do this yet

Here's a rendition (I'm sure this is not acceptable) that might shed some light on alternatives.

```
using namespace System;
using namespace System::Collections::Generic;
void DoBlock(int intNum)
{
List<String^>^ lst_str = gcnew List<String^>();
int intNumLines = ((intNum*2)-1);
array<wchar_t,1>^ arr = (gcnew String('0', intNumLines))->ToCharArray();
int iPos = 0;
//
for(int i = intNumLines; (i > 0 && iPos < intNum); i--)
{
for(int n = iPos; n < i; n++)
{
arr[n] = (iPos+1).ToString()[0];
}
lst_str->Add(gcnew String(arr));
iPos++;
}
lst_str->ForEach(gcnew Action<String^>(Console::WriteLine));
lst_str->Reverse();
lst_str->RemoveAt(0);
lst_str->ForEach(gcnew Action<String^>(Console::WriteLine));
}
int main(array<System::String ^> ^args)
{
DoBlock(7);
return 0;
}
```

I forgot to mention that I'm relatively new to C++, and I have no idea what most of that means :p

Seemingly, the number has to be between 1 and 9, right?

...but if this is on a quiz, why would you want someone else to answer it?

It was on a quiz that I had already done, and I couldn't figure out the answer. So I'm curious as to what the answer is, or at least I'd like a starting point so that I can find the answer.

If you use a FOR loop from 1 to N, this loop would control each line.

Then inside that FOR loop you have more loops that control the pattern of each line.

Your job is to figure out how to create the line pattern. What is the same in each line? What's different? Find the one pattern that defines each line.

Hint: 3 loops...

If you use a FOR loop from 1 to N, this loop would control each line.

Then inside that FOR loop you have more loops that control the pattern of each line.Your job is to figure out how to create the line pattern. What is the same in each line? What's different? Find the one pattern that defines each line.

Hint: 3 loops...

I see that the square, in this case, is a 9 x 9 square, and that each descending number appears as a multiple of 8 (There are 8 4's, 16 3's, 24 2's, and 32 1's), but i'm not sure of any other patterns. Im sorry, I'm not much of a pattern person. I really do like programming, it's just that the problem solving part is difficult for me. So if you could give me another hint, that'd be nice. I'm not asking you for the answer though. Thank you.

You can e.g. find a formula for calculating how many chars are in one line

From this point you could find another formula for every char in a line.

I really do like programming, it's just that the problem solving part is difficult for me.

I have devastating news for you.

I forgot to mention that I'm relatively new to C++, and I have no idea what most of that means :p

Don't fret. I've been using C++ for five years and I *still* don't know what most of that code means.

It was on a quiz that I had already done, and I couldn't figure out the answer.

There's no "the answer" when coding. There's always multiple different ways to do the same thing, depending on how you attack the problem. You'll see my version is vastly different than thines01, and probably different than how WaltP would do it.

Here's the way I would have done it. No templates, two loops, can be easily converted to use vanilla C. Portable. Delicious.

```
int main()
{
int input;
cout << "Input a number." << endl;
cin >> input;
if(input < 1 || input > 9) //Bounds checking...
{
cout << "Sorry, must be between 1 and 9." << endl;
return 0;
}
//Print square; the closer to the edge, the smaller the number.
for(int line = 0; line < SQUARESIZE; line++)
{
for(int ch = 0; ch < SQUARESIZE; ch++)
cout << min(min(line+1, ch+1), min(SQUARESIZE-line,SQUARESIZE-ch));
cout << endl;
}
return 0;
}
```

"Problem solving" isn't something you can really learn on Daniweb or in a C++ class. That's just something that takes practice.

I see that the square, in this case, is a 9 x 9 square, and that each descending number appears as a multiple of 8 (There are 8 4's, 16 3's, 24 2's, and 32 1's), but i'm not sure of any other patterns.

You're kidding.....

Line **1**: 111111111 all 1's

Line **2**: 122222221 1's and 2's

Line **5**: 123454321 12345....

I really do like programming, it's just that the problem solving part is difficult for me.

As **Tumlee** implies, you had better get good at it. It's one definition of programming.

I see that the square, in this case, is a 9 x 9 square, and that each descending number appears as a multiple of 8 (There are 8 4's, 16 3's, 24 2's, and 32 1's), but i'm not sure of any other patterns. Im sorry, I'm not much of a pattern person. I really do like programming, it's just that the problem solving part is difficult for me. So if you could give me another hint, that'd be nice. I'm not asking you for the answer though. Thank you.

To figure this out, just do it inch by inch. Start with the basic framework of the program, like setting up the loops. Here is what the square looks like when 5 is entered by the user:

111111111

122222221

123333321

123444321

123454321

123444321

123333321

122222221

111111111

OK, so we can see that there are lines to print out, and that there are a certain number of characters in each line. Lets start by concentrating on printing out one line. Then we will loop around and print out all of the other lines.

So then after we print out the first character in a line, we just loop around and print out the rest for that line. But how many digits are there per line? We can see that it depends on what our input number is. The line counts from 1 up to the input number, then counts down to 1 again (example: with 5 as input - 123454321). So we can see that the number of digits in a line equals the number that is input times 2 minus 1, or 2x - 1. So if we input 5, then there are 9 digits in the line, and if we input 8 then there are 15 digits in the line. And we want to enter only numbers between 1 and 9 to keep our square square, however, I left that out of the code.

So let's start coding our basic framework for our program:

```
#include<iostream> //so we can use cout and cin, etc.
using namespace std; //for convenience so we can write, cout instead of std::cout, etc.
int main()
{
cout <<"\nEnter a number: ";
int input; cin >> input; //make a variable and then slam a number into it : )
int lineLength = (2 * input - 1); //number of digits in a line.
for(int i = 0; i < lineLength; i++)
{
//in here goes our code to print out one digit.
//the code in here gets repeated lineLength times to make one whole line.
}
cout << endl; //at this point, one line has been printed, so we move down a line.
return 0;
}
```

So far we have the framework to print one whole line then move down to start the next line. So how do we print out another line? By simply going back and doing the whole process again, and that is by running that **for** loop again. But how many lines do we need? Well, since we are printing a square, the number of lines equals the number of digits in a line. So again, the number of lines to print out = (2x - 1). We could use another **for** loop to loop around to print the rest of the lines, but a simpler **while** loop will work just fine. So let's put a **while** loop around our **for** loop:

```
#include<iostream> //so we can use cout and cin, etc.
using namespace std; //for convenience so we can say, cout instead of std::cout, etc.
int main()
{
cout <<"\nEnter a number: ";
int input; cin >> input; //make a variable and slam a number into it : )
int lineLength = (2 * input - 1); //number of digits in a line.
int numOfLines = linelength; //number of lines - same as number of digits in a line.
while(numOfLines > 0) //keep looping until all lines are printed.
{
for(int i = 0; i < lineLength; i++)
{
//in here goes our code to print out one digit.
//the code in here gets repeated lineLength times to make one whole line.
}
cout << endl; //at this point, one whole line has been printed, so we move down a line.
--numOfLines; //subtract 1 from count of lines that are needed to be printed.
}
return 0;
}
```

Now let's concentrate on making one line in that **for** loop. The body of the **for** loop prints one digit for each iteration of the loop. And when a digit is printed, there are only three possibilities for the value of that digit:

1) The digit is the same value as the previous one, or

2) The digit is one greater than the previous one, or

3) The digit is one less than the previous one.

So before you print out a digit, you must make that determination, which may depend on what line you're on and what digit in the line is being printed. You could use **if** statements. Also, **int i** in the **for** loop increases with each iteration, so you might want to consider using that as your digit to print out, like this: **cout << i;** . But if you do use **i**, then you have to start it out as 1, and likewise increase **lineLength** by 1 to compensate, like so: **for(int i = 1; i < lineLength + 1; i++)** or something similar.

OK, I'll let you do some figuring on your own. Give it some thought and reply back if you get stuck on anything.

Here's the way I would have done it.

Err... oops. I forgot:

```
#include <iostream>
using namespace std;
#define SQUARESIZE (input*2-1)
```

Are you familiar with functions? If not, it's a good time to start learning about

them. As pointed out by others above, breaking the task into smaller steps

helps a lot. So, let's focus on building a line first. Take a look at these lines:

122222221

123333321

123444321

There is a pattern here. Each one of them consists of a left, a middle and a right part,

where the right part is the left one reversed and the middle part is a repetition of the

same number. Let's write them again, so that this becomes more obvious:

1 - 2222222 - 1

12 - 33333 - 21

123 - 444 - 321

This line also follows the same pattern:

111111111

It just happens that the left and right parts are empty.

Now, let's write a function that builds a line like this...

```
string line(int n, int i)
{
// use the appropriate constructor to set the size for the left
// and right parts and properly initialize the middle part
string mid( /* ... */ );
string left( /* ... */ );
string right( /* ... */ );
// set the left and right parts
for (int j = 0; j < i - 1; ++j)
{
// remember that 1 + '0' == '1',
// 2 + '0' == '2', etc...
}
return left + mid + right;
}
```

Remember that we want `line(5, 1) == "111111111"`

, `line(5, 2) == "122222221"`

, ..., `line(5, 5) == "123454321"`

Now, inside main, you'll want to get a number between 1 and 9 from

the user - let's call this number n - and then do something like this...

```
cout << line(n, 1) << endl;
cout << line(n, 2) << endl;
cout << line(n, 3) << endl;
/* ... */
cout << line(n, n - 1) << endl;
cout << line(n, n) << endl;
cout << line(n, n - 1) << endl;
/* ... */
cout << line(n, 3) << endl;
cout << line(n, 2) << endl;
cout << line(n, 1) << endl;
```

This can easily be done using a for loop, then a

single cout statement and then another for loop.

By the way, these problems are extremely fun to solve

in functional languages -> http://codepad.org/cWsEmCCA

Useful links-> std::string functions (part 1) functions (part 2)

Are you familiar with functions? If not, it's a good time to start learning about them.

If not, maybe the instructor isn't ready to teach them...

Well, the OP said that this is not a homework assignment. Also,

this is not the first approach in this thread that uses functions.

So, is this something personal or what?