Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.programmer > #26467

Re: PRINTING DIAMOND SHAPE WITH LOOPS!

From Mike Amling <mamling@chaff.us>
Newsgroups comp.lang.java.programmer
Subject Re: PRINTING DIAMOND SHAPE WITH LOOPS!
Date 2013-11-01 09:59 -0500
Message-ID <bdhtv3F578bU2@mid.individual.net> (permalink)
References <bd7dee67.0408241759.3884e4e9@posting.google.com> <4wTWc.18629$cx.18110@bignews4.bellsouth.net> <llWWc.228978$eM2.215971@attbi_s51> <ecaee893-0ad6-48a6-b345-24a33436b4df@googlegroups.com>

Show all headers | View raw


On 11/1/13 3:30 AM, jyothi.m1089@gmail.com wrote:
 > On Wednesday, August 25, 2004 11:59:37 AM UTC+5:30, Liz wrote:
 >> "Hal Rosser" <hmrosser@bellsouth.net> wrote in message
 >> news:4wTWc.18629$cx.18110@bignews4.bellsouth.net...
 >>>
 >>> "Jay Dean" <fresh1700@yahoo.com> wrote in message
 >>> news:bd7dee67.0408241759.3884e4e9@posting.google.com...
 >>>> I need simple nested loops (either for, or while) code for a method to
 >>>> print a diamond shaped stars for n stars, where n is odd.
 >>>>    Example. n=9 would produce
 >>>>
 >>>>          *
 >>>>         ***
 >>>>        *****
 >>>>       *******
 >>>>      *********
 >>>>       *******
 >>>>        *****
 >>>>         ****
 >>>>          *
 > can you please explain the logic of this program....

I haven't looked at the program, but looking at the diamond I see the 
first and last lines have one star, but the second and next-to-last have 
three stars and four stars respectively. I assume the four-star line was 
supposed to have three stars.

The number of lines to be written is n, so we might hope that an 
outermost loop would look like

for (int jj=0; jj<n; ++jj) {
   ...
   System.out.println();
}

The number of stars in each line is maximal, at n stars, when jj is 
around n/2, so I'm thinking something like ss=n-abs(jj-n/2), which has 
its largest value when jj=n/2 and decreases linearly as jj gets away 
from n/2. If ss is not exactly right, then let's fiddle with it. For 
n=9, jj=0, we want 1 star and ss is 9-5=4. For n=9, jj=4, we want 9 
stars and ss is 9-0=9.

The proposed ss needs to decrease faster as jj gets away from n/2. How 
about doubling the expression inside the abs()?
tt=n-abs(2*jj-n)
This decreases the number of stars by 2 for every step jj moves away 
from n/2. That's good. For n=9, jj=0, tt is 9-9=0. For n=9, jj=4, tt is 
9-1=8. I guess we just want to add 1 to tt to get the desired number of 
stars.
uu=n-abs(2*jj-n)+1

Adding an inner loop to print the stars, we get

for (int jj=0; jj<n; ++jj) {
   ...
   for (int kk=0; kk<n-Math.abs(2*jj-n)+1; ++kk) {
     System.out.print('*');
   }
   System.out.println();
}

We still want some spaces before the stars, and the number of spaces is 
minimal when jj=n/2 and increases as jj moves away from n/2. That suggests
vv=abs(jj-n/2)
For n=9, jj=0, vv is 4. For n=9, jj=4, vv is 0. Huh. That's exactly 
correct already.

Adding an inner loop to print the spaces, we get

for (int jj=0; jj<n; ++jj) {
   for (int kk=0; kk<Math.abs(jj-n/2); ++kk) {
     System.out.print(' ');
   }
   for (int kk=0; kk<n-Math.abs(2*jj-n)+1; ++kk) {
     System.out.print('*');
   }
}

I think that's correct and halfway understandable.

My overdeveloped sense of microefficiency wants to avoid the possibility 
that the JITC will produce code that evaluates n-Math.abs(2*jj-n)+1 more 
often than necessary, so if I were actually implementing this, I would 
write it as

for (int jj=0; jj<n; ++jj) {
   for (int kk=Math.abs(jj-n/2); --kk>=0; ) {
     System.out.print(' ');
   }
   for (int kk=n-Math.abs(2*jj-n)+1; --kk>=0; ) {
     System.out.print('*');
   }
}

Mike Amling
-- 
"Fiat lux" is the Big Bang in Latin.

Back to comp.lang.java.programmer | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

Re: PRINTING DIAMOND SHAPE WITH LOOPS! jyothi.m1089@gmail.com - 2013-11-01 01:30 -0700
  Re: PRINTING DIAMOND SHAPE WITH LOOPS! rossum <rossum48@coldmail.com> - 2013-11-01 13:03 +0000
  Re: PRINTING DIAMOND SHAPE WITH LOOPS! Mike Amling <mamling@chaff.us> - 2013-11-01 09:59 -0500
  Re: PRINTING DIAMOND SHAPE WITH LOOPS! Roedy Green <see_website@mindprod.com.invalid> - 2013-11-01 18:32 -0700
    Re: PRINTING DIAMOND SHAPE WITH LOOPS! pchristor@yahoo.co.uk - 2013-11-12 05:21 -0800
      Re: PRINTING DIAMOND SHAPE WITH LOOPS! Lew <lewbloch@gmail.com> - 2013-11-27 15:57 -0800
        Re: PRINTING DIAMOND SHAPE WITH LOOPS! Arne Vajhøj <arne@vajhoej.dk> - 2013-11-27 19:21 -0500
        Re: PRINTING DIAMOND SHAPE WITH LOOPS! pchristor@yahoo.co.uk - 2013-11-28 17:45 -0800
          Re: PRINTING DIAMOND SHAPE WITH LOOPS! Arne Vajhøj <arne@vajhoej.dk> - 2013-11-28 21:10 -0500
            Re: PRINTING DIAMOND SHAPE WITH LOOPS! pchristor@yahoo.co.uk - 2013-11-29 03:03 -0800
              Re: PRINTING DIAMOND SHAPE WITH LOOPS! Joerg Meier <joergmmeier@arcor.de> - 2013-11-29 12:33 +0100
                Re: PRINTING DIAMOND SHAPE WITH LOOPS! pchristor@yahoo.co.uk - 2013-11-29 09:01 -0800
                Re: PRINTING DIAMOND SHAPE WITH LOOPS! Arne Vajhøj <arne@vajhoej.dk> - 2013-11-29 13:15 -0500
                Re: PRINTING DIAMOND SHAPE WITH LOOPS! pchristor@yahoo.co.uk - 2013-11-29 17:06 -0800
                Re: PRINTING DIAMOND SHAPE WITH LOOPS! pchristor@yahoo.co.uk - 2013-11-29 17:24 -0800
                Re: PRINTING DIAMOND SHAPE WITH LOOPS! Arne Vajhøj <arne@vajhoej.dk> - 2013-11-29 20:59 -0500
                Re: PRINTING DIAMOND SHAPE WITH LOOPS! pchristor@yahoo.co.uk - 2013-11-30 06:57 -0800
                Re: PRINTING DIAMOND SHAPE WITH LOOPS! Arne Vajhøj <arne@vajhoej.dk> - 2013-11-30 10:31 -0500
                Re: PRINTING DIAMOND SHAPE WITH LOOPS! Lew <lewbloch@gmail.com> - 2013-12-02 15:32 -0800
                Re: PRINTING DIAMOND SHAPE WITH LOOPS! pchristor@yahoo.co.uk - 2013-12-04 03:45 -0800
                Re: PRINTING DIAMOND SHAPE WITH LOOPS! Arne Vajhøj <arne@vajhoej.dk> - 2013-12-09 21:10 -0500
                Re: PRINTING DIAMOND SHAPE WITH LOOPS! pchristor@yahoo.co.uk - 2013-12-05 00:15 -0800
                Re: PRINTING DIAMOND SHAPE WITH LOOPS! Arne Vajhøj <arne@vajhoej.dk> - 2013-12-09 21:14 -0500
                Re: PRINTING DIAMOND SHAPE WITH LOOPS! Joerg Meier <joergmmeier@arcor.de> - 2013-11-29 19:58 +0100
                Re: PRINTING DIAMOND SHAPE WITH LOOPS! pchristor@yahoo.co.uk - 2013-11-29 17:12 -0800
                Re: PRINTING DIAMOND SHAPE WITH LOOPS! Joerg Meier <joergmmeier@arcor.de> - 2013-11-30 02:55 +0100
                Re: PRINTING DIAMOND SHAPE WITH LOOPS! Arne Vajhøj <arne@vajhoej.dk> - 2013-11-29 21:01 -0500
                Re: PRINTING DIAMOND SHAPE WITH LOOPS! Lew <lewbloch@gmail.com> - 2013-12-02 15:31 -0800
              Re: PRINTING DIAMOND SHAPE WITH LOOPS! Arne Vajhøj <arne@vajhoej.dk> - 2013-11-29 12:48 -0500

csiph-web