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.