The Student Room Group

About a pattern problem

can someone help me to code a program for the pattern as below in c.
for n=5.
5 5 5 5 5 5 5 5 5
5 4 4 4 4 4 4 4 5
5 4 3 3 3 3 3 4 5
5 4 3 2 2 2 3 4 5
5 4 3 2 1 2 3 4 5
5 4 3 2 2 2 3 4 5
5 4 3 3 3 3 3 4 5
5 4 4 4 4 4 4 4 5
5 5 5 5 5 5 5 5 5
(edited 5 years ago)
Firstly, break down the difficult, complex problem into easier, simpler problems -- when something seems too difficult and too complicated, then try to solve something easier instead, small bits of it at a time, and build up the solution gradually. Almost any complex problem can really just be solved by turning it into loads of simple problems.

To begin with, start out with one or more small programs which only solve small parts of the problem -- they don't even need to give you all the 'right' output to begin with, and it can help to sketch out some ideas on paper if you're having a hard time visualising things.

Firstly, can you write out a program which repeatedly outputs the same character (for example a "#" or "." character) on a single line many times?
Secondly, can you write out a program which repeatedly outputs the same character on the multiple lines many times? - e.g. a square or rectangular shape on the screen with lots of "#" characters or "." characters.

Doing this should solve the general problem of writing a square/rectangular shape -- after that you just need to worry about the position of different characters

So afterwards, consider making something a bit more complex - but stick with a "hard-coded" value for 'N' (e.g. 5) to begin with. For example:
- Can you write a program which only outputs the first line? (5 5 5 5 5 etc...)
- Can you write a program which only outputs the second line and calculates the number to print? (5 4 4 4 4.... 4 5)
- Can you write a program which only outputs the third line and calculates the number to print? (5 4 3 3 3 ... 3 4 5)
- Lastly, can you write a program which only outputs the line in the middle of the block? (5 4 3 2 1 2 3 4 5 )

Try doing this and look for similarities in the code -- you should notice some obvious similarities emerge after you've done it 3 times; similarites in code can be turned into an algorithm. Once you have that algorithm, you can then modify it for different values of 'N'

divide-and-conquer is a very powerful and useful way of breaking down a hard problem into small things which are easier to reason over, and a lot more managable.

Don't worry if the code you end up with looks a bit messy or inefficient - the most important thing is to get something working -- once you have some working code you can then worry about tidying it up and making it efficient.


As you're writing the code, it's helpful to use lots of 'print' messages which show what's happening with variables in your program.

Alternatively, it's also helpful to make sure you learn how to use the debugger for your language/IDE (e.g. IDLE in Python, or the Visual Studio debugger for C#/VB) -- if you've never used a debugger before and haven't used "breakpoints" then it's reallly useful to learn, and will help you to understand what a program is really doing by letting you 'inspect' the program step-by-step, watching what happens in slow-time. debuggers and breakpoints should save you loads of frustration, so it's worth spending the 20-30 minutes needed to find out about those. (a debugger is quicker to use and more insightful than "print" statements)
(edited 5 years ago)

Quick Reply

Latest

Trending

Trending