Re: Is there a better way of getting the following output: ?



Hi Ivan:

Well as you can probably tell you've solved it the quicker more efficient
way but probably not the better way (depending upon one's definition of
better.) You can imagine how it would be to write an 80-row version or if
having completed it the "boss" says "oh but the user gets to choose the
characters to use did I forget to tell you that?"

I can post the code but you should try it first. Don't think of this as
asterisks and spaces it's all about a pattern and that requires an
algorithm. Roedy pointed out the pattern you should be trying to solve.
Notice one thing however that the right digit is always the same as the left
digit. That means you only need to solve for the number of stars and 1/2
the number of spaces.

I wrote it so a maximum number of rows plus two characters can be set and it
generates the pattern. The only restriction is that the maximum rows must
be odd which is the only way you can get one star in the middle. I did use
nested loops but they don't look nested since I put the second loop into
it's own procedure so I didn't have to repeat it 3 times.

I noticed your code earlier and your comment about getting the "top-half" to
work. Avoid thinking about it as having two parts. I recommend an
iterative process so make sure you can print <n> rows of <n> stars. Then
make it print the same <n> rows but printing <n1> spaces (I think you should
use periods though, you can see them better) plus <n2> stars plus <n1>
spaces.

So you would get something like this:

....*****...
....*****...
....*****...
....*****...
....*****...

It will be working but clearly it's just a square. So now the algorithm
comes down to "how do increase the number of periods and reduce the number
of stars (until there is one star) and then reverse the process"?

Tom

"Oliver Wong" <owong@xxxxxxxxxxxxxx> wrote in message
news:hjeUf.123$B_1.23@xxxxxxxxxxx

"Ivan" <find.ivan@xxxxxxxxx> wrote in message
news:1143002182.004749.15700@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
hhm..
Okay.. This is what I have so far..


import java.io.*;
import java.math.*;
public class Pattern
{
public static void main( String args[] )
throws IOException
{
int y=1;
int x=1;
int z=1;
int gap = 4;
int size=9;

for (x=1; x<=size; x+=2)
{
for (z=1; z<=gap; z++)
{
System.out.print(" ");
}
for (y=1; (y<=(x)); y++)
System.out.print("*");
System.out.println();

gap--;
}
}
}


i've made it this far and it seems as if though I can't get my head
around getting the top half to work..

You've got the right idea. This program only prints the "bottom"
triangle. Consider this strategy: Put this program aside for now, and
start writing a new program; one which only prints the "top" triangle.

Then, take both programs and try to combine them together, to see if
you can produce the whole figure. You'll probably have 2 sets of nested
loops, e.g.


for (something) {
for (something) {
do something;
}
}
for (something) {
for (something) {
do something;
}
}

- Oliver


.



Relevant Pages

  • Re: What will be the next MAJOR programming language for commercial use?
    ... Variants can be considered as such a common OO pattern that it is worth ... Int of int ... As lists are built-in, they can be decomposed in a pattern just as they are ... features found in other state-of-the-art programming languages. ...
    (comp.lang.misc)
  • Re: Speeds of light
    ... > appeared in talk.origins, posted by iaoua iaoua ... > identified even when the entire pattern shifts, ... So when did you take a sample of two stars of the same composition to ... He's denying the constancy of the laws of physics over space, which by Noether's Theorem is equivalent to denying conservation of momentum aka Newton's 1st and 3rd laws of motion. ...
    (talk.origins)
  • Re: Working on a cryptogram decryption program, need some advice.
    ... If your cryptogram retains spaces, it's worth looking at word patterns ... If you found the space-delimited pattern abccbccbddb in your ciphertext, ... This is not so much "letter frequency analysis" as "letter pattern ... int FrequencyCompare ...
    (rec.puzzles)
  • Re: Working on a cryptogram decryption program, need some advice.
    ... If your cryptogram retains spaces, it's worth looking at word patterns ... If you found the space-delimited pattern abccbccbddb in your ciphertext, ... This is not so much "letter frequency analysis" as "letter pattern ... int FrequencyCompare ...
    (rec.puzzles)
  • Re: [ANNOUNCE/RFC] Really Fair Scheduler
    ... int runus,sleepus=7000; ... void terminal_error ... In an unoptimised loop we try to benchmark how many meaningless loops ... unsigned long long cur, last, start, act; ...
    (Linux-Kernel)