Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Nested for loop

2 views
Skip to first unread message

f2prateek

unread,
Apr 9, 2007, 12:09:28 PM4/9/07
to
I want my output to be something like this using only a single nested
for loop:-

*
**
***
****
*****
****
***
**
*

But I have not been able to do it using only one nested loop. I have
done it with two nested for loops, but need help in using only a
single for loop. Any suggestions?

denJs

unread,
Apr 9, 2007, 3:44:41 PM4/9/07
to
May be you should look at this output as at a sequence of characters,
where newline character must be at certain indices.
Something like 1, 4, 8, 13 and so on. First increasing, then after
some point decreasing.
Then you can do it in one loop.

jt

unread,
Apr 9, 2007, 6:29:23 PM4/9/07
to
Alex Hunsley wrote:
> If you could work out how to print out:
>
> 1 2 3 4 5 4 3 2 1
>
> using only a single for loop, you'd be well on your way.
> lex
>
>
>
>>
Yipppeeee... I got it...
The hardest part was working "...out how to print out 1 2 3 4 5 4 3 2 1
using only a single for loop"

Needed to go back to my highschool algerbra but it works...
If the OP ever responds, I'd be curious as to what the group thinks of
my solution. I won't post it before the OP responds..


--
There are 10 types of people in this world. Those who understand binary
and those who don't.

Alex Hunsley

unread,
Apr 9, 2007, 6:55:36 PM4/9/07
to

I'd be curious to see what how you've done it... (but yeah, perhaps wait
until OP gets the answer themselves).
lex

jt

unread,
Apr 9, 2007, 7:02:26 PM4/9/07
to
Alex Hunsley wrote:

> I'd be curious to see what how you've done it... (but yeah, perhaps wait
> until OP gets the answer themselves).
> lex

One question, how did you think to transform the *'s into numbers? That
would never have crossed my mind.

If your email address is correct, I could send you a link where you can
find a zip file of my source.

Alex Hunsley

unread,
Apr 9, 2007, 10:29:25 PM4/9/07
to
jt wrote:
> Alex Hunsley wrote:
>
>> I'd be curious to see what how you've done it... (but yeah, perhaps wait
>> until OP gets the answer themselves).
>> lex
> One question, how did you think to transform the *'s into numbers? That
> would never have crossed my mind.

Just a classic sort of problem simplification: output something that
requires the same information (i.e. number of stars that would be
output), but is simpler to do - helps you concentrate on the problem at
hand...

> If your email address is correct, I could send you a link where you can
> find a zip file of my source.

Feel free. You'll have to reply to the anti-spam confirm message you get
sent though.
lex

JohnT

unread,
Apr 10, 2007, 10:44:28 AM4/10/07
to
sent this morning... cool anti-spam thing... is that a feature of your ISP?

Lew

unread,
Apr 10, 2007, 7:09:28 PM4/10/07
to
JohnT wrote:
> Yipppeeee... I got it...

Did your solution involve Math.abs()?

--
Lew

jt

unread,
Apr 10, 2007, 8:15:22 PM4/10/07
to
Nope. I don't think the OP is going to post back here so if no one
objects, I'll post my solution tomorrow. Lex has already seen it.

Greg R. Broderick

unread,
Apr 10, 2007, 11:07:06 PM4/10/07
to
"f2prateek" <f2pr...@gmail.com> wrote in news:1176134968.542427.180300
@d57g2000hsg.googlegroups.com:

> But I have not been able to do it using only one nested loop. I have
> done it with two nested for loops, but need help in using only a
> single for loop. Any suggestions?

Recursion.

--
---------------------------------------------------------------------
Greg R. Broderick gregb+use...@blackholio.dyndns.org

A. Top posters.
Q. What is the most annoying thing on Usenet?
---------------------------------------------------------------------

The Billboard Hot 100

unread,
Apr 11, 2007, 6:17:01 AM4/11/07
to

Hi there, I think that method is what you are looking for.

public static void drawDiamond(int n) {
for(int i = 0; i < n * n; i++)
System.out.print((Math.abs(i%n - n/2) + Math.abs(i/n - n/2) > n/
2?"":"*") + (i%n == n-1?"\n":""));
}


Reference:

http://www.velocityreviews.com/forums/showthread.php?s=3c9a5c0ae8f73e70acef59996dbe836a&t=136340&page=2&pp=10
by Larry Barowski

Have a good one Emre Simtay


The Billboard Hot 100

unread,
Apr 11, 2007, 6:22:21 AM4/11/07
to

I think that method is what you are looking for.

public static void drawDiamond(int n) {

for(int i = 0; i < n * n; i++)
System.out.print((Math.abs(i%n - n/2) + Math.abs(i/n - n/2) > n/
2?"":"*") + (i%n == n-1?"\n":""));
}

Reference:
http://www.codecomments.com/archive245-2004-8-265254.html
by
Larry Barowski

Have a good day
Emre SIMTAY

The Billboard Hot 100

unread,
Apr 11, 2007, 6:23:21 AM4/11/07
to

JohnT

unread,
Apr 11, 2007, 12:05:30 PM4/11/07
to
import java.io.StringWriter;

public class ShowMe {
public static void main(String [] args) {
String allStars = "*****";
int count = 0;
for (int x=1;x<10;x++) {
count = x;
if (x > 5)
count = x + (( -2 * x) + 10);
StringWriter sb = new StringWriter(count);
sb.write(allStars, 0, count);
String correctNumberOfStars = sb.toString();
System.out.println(correctNumberOfStars);
}
}
}

Lex pointed out to me that my expression was a bit more complex than it
needed to be, but I didn't think my effort was too bad for about 45
minutes of work. Most of that time was working out the formula. Once I
had that figured out, it wasn't too difficult to search the java docs
for a "string" type class that had a method I needed.

f2prateek

unread,
Apr 12, 2007, 7:17:16 AM4/12/07
to
What exactly does Math.abs() stand for?


Lew

unread,
Apr 12, 2007, 8:28:52 AM4/12/07
to
f2prateek wrote:
> What exactly does Math.abs() stand for?

Hey, there. How's the homework coming along?

It's the abs() method of the java.lang.Math class.

Hint: The Java API docs are filled with useful information.

How is your maths skill? What sort of background do you have with respect to
maths?

--
Lew

Alex Hunsley

unread,
Apr 13, 2007, 7:54:42 AM4/13/07
to

No, it's a free Bluebottle account - website based anti-spam email
service. You can sign up yourself for nothing if you like it.
lex

Alex Hunsley

unread,
Apr 13, 2007, 7:56:25 AM4/13/07
to
Lew wrote:

> JohnT wrote:
>> import java.io.StringWriter;
>>
>> public class ShowMe {
>> public static void main(String [] args) {
>> String allStars = "*****";
>> int count = 0;
>> for (int x=1;x<10;x++) {
>> count = x;
>> if (x > 5)
>> count = x + (( -2 * x) + 10);
>> StringWriter sb = new StringWriter(count);
>> sb.write(allStars, 0, count);
>> String correctNumberOfStars = sb.toString();
>> System.out.println(correctNumberOfStars);
>> }
>> }
>> }
>>
>> Lex pointed out to me that my expression was a bit more complex than
>> it needed to be, but I didn't think my effort was too bad for about 45
>> minutes of work. Most of that time was working out the formula. Once
>> I had that figured out, it wasn't too difficult to search the java
>> docs for a "string" type class that had a method I needed.
>
> I was thinking of a slightly different approach (untested):

>
> public static void main(String [] args)
> {
> String allStars = "*****";
> for ( int ix = -4; ix < 5; ++ix )
> {
> System.out.println( allStars.substring( 0, 5 - Math.abs(ix) );
> }
> }

This solution crossed my mind too! It's quite nice for the avoidance of
an if.
lex

>

Dustan

unread,
Apr 16, 2007, 8:35:03 PM4/16/07
to

Why not do something like this?

System.out.println("* ");
System.out.println("** ");
System.out.println("*** ");
System.out.println("**** ");
System.out.println("*****");
System.out.println("**** ");
System.out.println("*** ");
System.out.println("** ");
System.out.println("* ");

Sorry; I couldn't resist. ;-)

TideRider

unread,
Apr 16, 2007, 9:34:43 PM4/16/07
to
"Dustan" <Dustan...@gmail.com> wrote in message news:1176770103.6...@y80g2000hsf.googlegroups.com...

| On Apr 9, 11:09 am, "f2prateek" <f2prat...@gmail.com> wrote:
| > I want my output to be something like this using only a single nested
| > for loop:-
| >
| > *
| > **
| > ***
| > ****
| > *****
| > ****
| > ***
| > **
| > *
| >
| > But I have not been able to do it using only one nested loop. I have
| > done it with two nested for loops, but need help in using only a
| > single for loop. Any suggestions?

Iterate i from 0 to 9, write the number of asterisks min(i, 10-i) in each iteration?


Dustan

unread,
Apr 17, 2007, 5:49:09 AM4/17/07
to
On Apr 16, 8:34 pm, "TideRider" <4me2k...@noyb.com> wrote:
> "Dustan" <DustanGro...@gmail.com> wrote in messagenews:1176770103.6...@y80g2000hsf.googlegroups.com...

That's two for-loops: the 1 that you explicitly specified, and the one
to "write the NUMBER of asterisks", unless, of course, you have
another way, which is plausible, but you didn't specify; previous
suggestions have been aimed at eliminating that latter for-loop.

Lew

unread,
Apr 17, 2007, 7:29:09 AM4/17/07
to
"TideRider" <4me2k...@noyb.com> wrote:
>> Iterate i from 0 to 9, write the number of asterisks min(i, 10-i) in each iteration?

Dustan wrote:
> That's two for-loops: the 1 that you explicitly specified, and the one
> to "write the NUMBER of asterisks", unless, of course, you have
> another way, which is plausible, but you didn't specify; previous
> suggestions have been aimed at eliminating that latter for-loop.

Number of asterisks may be a loop, but it isn't a for-loop, it's a
String.substring() loop.

--
Lew

TideRider

unread,
Apr 17, 2007, 10:50:13 PM4/17/07
to
"Lew" <l...@nospam.lewscanon.com> wrote in message news:4K2dnUAxzKMYMrnb...@comcast.com...

Also, you specified a *nested* for loop, which means a loop contained within a loop.
Of course, you could always eliminate the inner loop with:

String outText = "*****".substring(0, min(i, 10-i));


Alex Hunsley

unread,
Apr 9, 2007, 12:38:12 PM4/9/07
to

If you could work out how to print out:

Lew

unread,
Apr 11, 2007, 8:24:32 PM4/11/07
to
JohnT wrote:
> import java.io.StringWriter;
>
> public class ShowMe {
> public static void main(String [] args) {
> String allStars = "*****";
> int count = 0;
> for (int x=1;x<10;x++) {
> count = x;
> if (x > 5)
> count = x + (( -2 * x) + 10);
> StringWriter sb = new StringWriter(count);
> sb.write(allStars, 0, count);
> String correctNumberOfStars = sb.toString();
> System.out.println(correctNumberOfStars);
> }
> }
> }
>
> Lex pointed out to me that my expression was a bit more complex than it
> needed to be, but I didn't think my effort was too bad for about 45
> minutes of work. Most of that time was working out the formula. Once I
> had that figured out, it wasn't too difficult to search the java docs
> for a "string" type class that had a method I needed.

I was thinking of a slightly different approach (untested):

public static void main(String [] args)
{
String allStars = "*****";


for ( int ix = -4; ix < 5; ++ix )
{
System.out.println( allStars.substring( 0, 5 - Math.abs(ix) );
}
}

--
Lew

0 new messages