By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,463 Members | 1,573 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,463 IT Pros & Developers. It's quick & easy.

Making patterns using C

P: 1
Ok, I need help and bad. I'm like freaking out because normally I can figure things out, but I'm going insane. I have no idea what I'm supposed to do/where to start. I am NOT looking for the direct answer because that will not help me with future projects, but rather I am looking for someone to walk me through what I am supposed to do or at least give me some really detailed starting tips.

So far I have:
int row;
int num;

for(row=4; row>=0; row--){
for (num=row; num>0; num--){
printf("$");}
printf("5\n");
}

but this only creates part of pattern B and I'm still so confused. I'm still not even completely sure about what I just did.

How am I supposed to create these patterns?? It just boggles my mind like nothing I've ever done before.

If someone can help me out then it would be appreciated to the fullest. Like I said though, some detailed explanations and steps are preferred rather than the flat-out answer.

Here's what I'm supposed to be doing:

The Task
The program you write will deal with printing patterns. You will plot a filled-in box using an ascii character and also the number corresponding to the size of the box. The size of the box will be entered by the user and will be both the number of rows and the number of columns in the box. You will print one of three patterns depending on the menu option the user selects. For a box of size 5 and the character '$', the following pattern options could be produced:

A B C
5$$$$ $$$$5 $$$$$
$5$$$ $$$5$ 5$$$$
$$5$$ $$5$$ 55$$$
$$$5$ $5$$$ 555$$
$$$$5 5$$$$ 5555$


You will ask the user to input the size of the box and also the character you should use to display the pattern.

For the size, you should only allow the user to enter a number between 2 and 9, inclusive. So, 1, for example, is not a valid size. You should error check the input and allow the user to keep entering numbers until he/she enters a number in the correct range.
Nov 7 '08 #1
Share this Question
Share on Google+
4 Replies


100+
P: 687
I'd write a function of (x,y) coordinates that return 0 or 1 depending on what desired pattern is, e.g. true if x==y or true if x<=y and then loop for x and y from 0 to N calling it.
Nov 7 '08 #2

Expert 100+
P: 2,415
First, decide which output option you will produce. It sounds like you have a lot of flexibility -- you listed 3 options, but it sounds like others are permissible. It doesn't matter which output option you choose, but you need a firm decision so you can write the code.

Notice that you will print out a square pattern of fill characters with a smaller number of digit characters. The trick is making the digit characters go where you want them. Study the pattern, looking at the row and column indices where the digit characters go.
Nov 7 '08 #3

100+
P: 424
Instead of tackling the whole problem at once, how about writing a program that does what the task asks for, but for the first line only (for any number supplied by the user)? For example, if the user provides 4 then it should print "4$$$" or "$$$4" or "$$$$", depending on the user's choice of pattern. This will get you started.

Then put that printing code into a function which takes as arguments
  • the number given provided by the user
  • the character provided by the user
  • the pattern design chosen by the user
  • the line number within the pattern.
Test your function for line = 1, it should print the first line as before (for any number and character provided by the user). Then try to modify your function to print any line in the pattern. Working one pattern design at a time, think about what changes on the next line and how the position of the number (5 in the example) will change depending on the line number. You already have the correct basic idea.

Finally, to print the whole pattern, just call your function once for each line. As a final touch, add some error checking to make sure the number supplied by the user is within the correct bounds, etc. And you're done!
Nov 7 '08 #4

Expert 100+
P: 2,415
First, decide which output option you will produce. It sounds like you have a lot of flexibility -- you listed 3 options, but it sounds like others are permissible. It doesn't matter which output option you choose, but you need a firm decision so you can write the code.
Never mind! I didn't read the OP carefully enough. I now see that he has to support all three output formats. Sorry for any confusion.
Nov 7 '08 #5

Post your reply

Sign in to post your reply or Sign up for a free account.