If this is your first visit, be sure to
check out the FAQ by clicking the
link above. You may have to register or Login
before you can post: click the register link above to proceed. To start viewing messages,
select the forum that you want to visit from the selection below.

Last edited by cjard; February 4th, 2005 at 03:45 AM.
Reason: c and d dont appear correctly because of the way html works.. putting it in a php or code block preserves the leading spaces (as if it were code)

Re: problem displaying patterns correctly using for loops

For c, think about how many spaces are at the beginning of the line... and what your current iterative position in the loop is... for example, iteration 0 (the first one) has 0 spaces at the front. Iteration 1 has... well, you get the idea.

Another hint would be to think: "two loops within a loop." One printing spaces, the other printing asterisks.

Re: problem displaying patterns correctly using for loops

you CAN do this either with 2 loops inside a loop, or with just one loop inside a loop. using one loop is harder, as it requires an IF test. the algorithm for that would be:
"when printing character number N, if N is greater than a certain number M, then we print asterisks, otherwise we print spaces"
and naturally, you decrease M each time to your program switches from spaces to asterisks one character sooner..

now you try to put this algorithm into the code you wrote! the comment should go above the line of code that carries out the step in the algorithm!

remember that even real advanced programmers write algorithms in ENGLISH before they translate to java.. you grew up thinking in english, and that hasnt changed. you cant speak java yuet, so dont try to think in it. If you speak french (for example) think about HOW you speak it; you build the sentence you want to say in your head in ENGLISH, then you translate to FRENCH and speak it. your programming should be no different

Re: problem displaying patterns correctly using for loops

well i got c and d and im not sure how. I wish i knew how to implement algorithims better. Could you show me how your algorithim fits into d because i just dont see it? I just kept changing things around until it worked which is not the way to program, but the only way i know how.

I have not tested this code, but the basic idea is to keep note of how many stars are being printed with the if condition, the first For is the number of lines you wish to draw, I have chosen ten.

Finally, we decrease the number of spaces per line run, and increase the number of stars. By switching these increases/decreases around, and also changing the Star and Space variable starting values, we can create different shapes.

This is not the best way of doing this, however, I'm sure you can now understand at least ONE way to implement the advice in this thread

Re: problem displaying patterns correctly using for loops

Originally Posted by raged789

I just kept changing things around until it worked which is not the way to program, but the only way i know how.

Works for me whenever I'm lazy .

Here's another way of thinking it (this is for C), each line requires a total of 10 characters to be printed. Each line requires a proportion of astericks and spaces that add up to 10. Example, the middle line has 5 astericks and 5 spaces, both combined to give a total of 10 charaters per line. The first line as 0 spaces and 10 astericks giving a total of 10 characters. The last line has 9 spaces and 1 asterick printed, giving a total of 10 characters. The tricky part, of course, is determing the proportions for each line, you're on the right track with the two loops inside the main loop, the loop boundary for the first loop has to print up to current iteration of the main loop, and the second loop has to finish from the current iteration of the main loop to 10 (or however many characters are required per line). I think if you coded your loops to increment rather then decrement, you might see the picture more clearly:

Or another direct way of looking at it, the number of spaces that are required to be printed out is whatever the value of i is (when the program starts, i = 0, so 0 spaces are printed out, when i = 5, 5 spaces need to be printed out, when i = 9, 9 spaces need to be printed out).

Re: problem displaying patterns correctly using for loops

Sorry dude - I mean Space = Space - 1;
...
I'm a little rusty when it comes to programming - I learned Basic back in the 80's and am more prone to old fashioned styles of code.

It would be helpful if you could use Java code and Java naming conventions when posting solutions here - alternatively post pseudocode, but please flag it clearly as pseudocode otherwise you risk further confusing an already confused novice...

The trouble with programmers is that you can never tell what a programmer is doing until it's too late...
Seymour Cray

Please use &#91;CODE]...your code here...&#91;/CODE] tags when posting code. If you get an error, please post the full error message and stack trace, if present.

Re: problem displaying patterns correctly using for loops

Originally Posted by cma

Works for me whenever I'm lazy .

Here's another way of thinking it (this is for C), each line requires a total of 10 characters to be printed. Each line requires a proportion of astericks and spaces that add up to 10. Example, the middle line has 5 astericks and 5 spaces, both combined to give a total of 10 charaters per line. The first line as 0 spaces and 10 astericks giving a total of 10 characters. The last line has 9 spaces and 1 asterick printed, giving a total of 10 characters. The tricky part, of course, is determing the proportions for each line, you're on the right track with the two loops inside the main loop, the loop boundary for the first loop has to print up to current iteration of the main loop, and the second loop has to finish from the current iteration of the main loop to 10 (or however many characters are required per line). I think if you coded your loops to increment rather then decrement, you might see the picture more clearly:

Or another direct way of looking at it, the number of spaces that are required to be printed out is whatever the value of i is (when the program starts, i = 0, so 0 spaces are printed out, when i = 5, 5 spaces need to be printed out, when i = 9, 9 spaces need to be printed out).

And Blaze, what's with: space = space--; ?

thank you, could you put an algorithim for that example you gave me? I would like to see how it goes together.

see and there arent even any for loops in there. i have no clue. Thank you for helping.

okay.. i'll implement my algorithm, with annotation. everything in italics is something i'm thinking, and i'll put a NUMBER indicating the order in which i thought about it, e.g. 3) I thought about this third..

Code:

5) here is where i write the main loop start. READ THE LAST THOUGHT in this code for more info
this was my FIFTH thought, NOT my first one! start below!
for(int mainLoopCount = 0; mainLoopCOunt < 10; mainLoopCount++){
//x=10, y=1. 1) i need to make 2 variables for x and y
int x=10;
int y=1;
//print X spaces 2) i need to repeatedly print some characters. i'll use a loop
for(int count=0; count < x; count ++){ 2a) that should print X number of spaces
System.out.print(' ');
}
//print Y asterisk 3) same again, another repetitive thing, another loop
for(int count=0; count < y; count ++){ 3a) that should print Y number of asterisks
System.out.print('*');
}
//decrement X, increment Y
x--;
y++;
//go back to the start 4) well, the only way we can go back to a certain point in java
is to either make a loop, or a recursive method call. as we've been told to use loops
i guess that "go back to the start" means we are at the end of the loop, so i need to make
another loop at the top of the code, and end that loop here
} //end of the main loop

so you see, when implementing an algorithm, we just translate simple english into simple java. you can do everything in this code, as its just writing basic loops. try not to think about it all at once. do a little bit, and integrate it piece by piece