Re: PRINTING DIAMOND SHAPE WITH LOOPS!

From: Paul Lutus (nospam_at_nosite.zzz)
Date: 08/27/04


Date: Thu, 26 Aug 2004 22:18:00 -0700

Jay Dean wrote:

> Paul,John,Adam,Liz, at al.,.
> Thank you all for your individual help and input into trying to
> helping me. Sorry, all I needed was one sentence:"Post your code that
> is not working". Instead, I was subjected to all sorts of unfair
> stereotypes.

Neither unfair nor a stereotype. Applying a sterotype would have required us
to use our misguided imaginations instead of the content of your posts.
There was no need for that. You asked a student question, then claimed
falsely not to be a student, while explaining what kind of student you are.
No imagination required.

But we can move past that, yes?

> Anyway, I tested John and Paul's codes in a main method and
> they worked perfectly,even thogh Paul intentionally meant to confuse
> me.

No, not at all. I meant to write a concise, efficient embodiment of the
algorithm. Its complexity to a student was a coincidental side effect.
Experienced programmers can see how it works at a glance. This makes it
space-efficient without sacrificing comprehensibility and maintainability.

> However, I have two main issues...
> (1)I STILL want HELP to implement the method with one of my OWN
> UNFINISHED codes below

That's more like it. We are much happier helping you with your own code. You
have no idea how much more acceptable this is.

> and
> (2) I would like you to help me understand your code with clear
> commenting if possible..., especially, does a "-" sign in front of an
> int variable imply ("opposite direction")?

No, it is called a unary operator: -1 means "minus one". In the same way, -x
means "minus x".

> Also, what do the symbols
> ?,:,etc mean in Java?

In this specific example it means:

(logical true-false test)?(do this if true):(do this if false);

Like this (not compiled or tested):

int x = -1;

System.out.println((x < 0)?"X is less than zero":"X is equal to or greater
than zero");

> (1). Here's one of my many attempts that I have struggled with for
> days:

Okay, with all respect I ask that you abandon this code entirely and start
with something simpler.

> if(i=((n+1)/2)+1) break;

And please do not use "break" in a program of this kind! This is very bad
practice. Constructs like "break" have a place, but they are very much out
of place in a simple, deterministic algorithm like this.

1. "Divide and conquer." Start with something simple, make it work, be sure
you understand it, and move on. Do not try to solve the entire problem in
one go.

2. Experiment with loops that create a row of spaces or a row of asterisks.
Think about how they work. Do not get bogged down with ambitious blocks of
code meant to solve the main problem, but that end up not doing what you
want and that you are unwilling to abandon.

3. Think about the logical flow of the program, and how to derive all the
required values from a few variables and control structures. Build
something easy first, before moving on to the main task.

4. Avoid "break." Breaks are innately confusing and to be avoided. Including
a break in a program is in most cases an admission of defeat in sorting out
a logical control flow for the algorithm.

With all respect, fixing the code you posted would not be particularly
productive, although I want to say it sends a very positive signal of your
seriousness, and you are to be commended for doing it.

-- 
Paul Lutus
http://www.arachnoid.com


Relevant Pages

  • Re: PRINTING DIAMOND SHAPE WITH LOOPS!
    ... Okay, Paul, et al., ... > falsely not to be a student, while explaining what kind of student you are. ... > of place in a simple, deterministic algorithm like this. ...
    (comp.lang.java.programmer)
  • Re: Why NP Problem is Important and Practical Examples
    ... there is a poly-time algorithm to verify a particular solution. ... My student was working as a software developer and he was meeting ...
    (comp.theory)
  • Re: "Sorting" assignment
    ... algorithm such as tbe bubble sort should be given a free pass because ... I would tailor which algorithm to start with by how the student thinks ... If you only learn the beautiful side of programming, ...
    (comp.programming)
  • Good algorithm for Inverse of cumulative Students t?
    ... I'm trying to set up a credit risk model that employees a Student t copula. ... the inverse of the cumulative distribution function of the Student t ... I also needed to setup a Gaussian copula for which I used Excel's built-in NORMSDISTfor the CDF which was good enough, and P.J. Acklam's inverse algorithm, since NORMSINVis meant to be quite horrible. ...
    (sci.stat.math)
  • Re: Obsessive Interest in Railways
    ... In message, at 17:32:47 on Sun, 23 Nov 2008, Arthur Figgis remarked: or just unfair to any student who doesn't happen to know about ...
    (uk.railway)

Loading