help drawing triangle

J

jinto12

Hi,

i need to draw a empty triangle using only characters such as (*), the
thing is i need help figuring out the right side of the triangle.

public class Subclass {

public static void main (String args[]){

int counter=1;
int base=15;




while(counter<=(base/2)){
System.out.print(" ");

System.out.print("\n ");


for(int i= counter; i<=((base/2)-1);i++){//prints n-1 spaces
System.out.print(" ");

}

System.out.print("*");

for(int m=1; m<=((counter*2)-3); m++){//problem with this loop,
need help
System.out.print(" ");
}

System.out.print("*");
counter++;
}

System.out.print("\n");

for(int a=1; a<=base ;a++){System.out.print("*");}//base of the
triangle





}


}
 
B

Benji

i need to draw a empty triangle using only characters such as (*), the
thing is i need help figuring out the right side of the triangle.

Hi,

Is this your homework? If it is, posting your code to a newsgroup and
asking someone to fix it is inappropriate.
 
B

Benji

Is this your homework? If it is, posting your code to a newsgroup and
asking someone to fix it is inappropriate.

Well, I'm going to assume your answer is yes, since you're posting from
your dorm room at Cal Poly Pomona.

Go ask your TA, or your professor.
 
J

jinto12

Benji said:
Hi,

Is this your homework? If it is, posting your code to a newsgroup and
asking someone to fix it is inappropriate.

sorry about that i just posted the code to see if anybody can give any
suggestions.
 
B

Benji

sorry about that i just posted the code to see if anybody can give any
suggestions.

the problem is that unless you tell us what CS program you're in, and
what the ethic codes are, we don't know how we can help you without
violating the rules of your school.
 
A

Andrew Thompson

Benji said:
...
Is this your homework? If it is, posting your code to a newsgroup and
asking someone to fix it is inappropriate.

Some people (myself included) feel it is perfectly fine
to post homework questions, verbatim, so long as the OP
is merely doing it as background information to lead into
a specific technical question.

I feel there is a huge difference between posts that
might both start with a 'copy/paste' of the question,
but end with ..'can you do my homework?' as opposed to
'I cannot understand why it throws an NPE at line 34.
Where am I going wrong?'/'The compiler is reporting an..
How/where do I investigate this further?'

[ FWIF - I do not know which of the above possibilities
is the most accurate representation of this thread. ..and
perhaps I read too much into your comment - I suppose it
depends on the exact level of 'fix', but I feel the OP
might have simply been asking for general direction
and advice, rather than a 'paint by numbers' description. ]
 
B

Benji

Andrew said:
Some people (myself included) feel it is perfectly fine
to post homework questions, verbatim, so long as the OP
is merely doing it as background information to lead into
a specific technical question.

I agree with this.
[ FWIF - I do not know which of the above possibilities
is the most accurate representation of this thread. ..and
perhaps I read too much into your comment - I suppose it
depends on the exact level of 'fix', but I feel the OP
might have simply been asking for general direction
and advice, rather than a 'paint by numbers' description. ]

Well, I think that at very least, he should say that it's his
homework. But not only that, he just said what it was supposed
to do, and pasted the code. So, it was essentially what you
said - "please write my code for me"

At least at my school (up until a few years ago when we
decided that copying code was "collaboration"), this method
of asking for homework help was considered academic misconduct.
 
T

Thomas Hawtin

i need to draw a empty triangle using only characters such as (*), the
thing is i need help figuring out the right side of the triangle.

public class Subclass {

public static void main (String args[]){

int counter=1;

Minor point, but it tends to be easier to start from 0 and work in a
half-closed interval, from 0 to an exclusive upper limit.
int base=15;

You might as well make base final, to show your intent not to change it.
while(counter<=(base/2)){

This could be a for loop.
System.out.print(" ");

System.out.print("\n ");


for(int i= counter; i<=((base/2)-1);i++){//prints n-1 spaces

You will probably find it easier to work out how many spaces you want
first. Then do a bog standard for (int i=0; i<num; ++i).
System.out.print(" ");

}

System.out.print("*");

for(int m=1; m<=((counter*2)-3); m++){//problem with this loop,
need help

i is out of scope here, so you don't have to play with the loop index names.

If you had worked out how many spaces you needed outside the loop, this
should be clearer. You can then print the number of spaces as an integer
to ease debugging.

Tom Hawtin
 
Z

zero

i need to draw a empty triangle using only characters such as (*),
the thing is i need help figuring out the right side of the triangle.

public class Subclass {

public static void main (String args[]){

int counter=1;

Minor point, but it tends to be easier to start from 0 and work in a
half-closed interval, from 0 to an exclusive upper limit.

Some beginner books suggest using a 1-based counter because that is more
"natural" and can avoid off-by-one errors. Sometimes these texts even give
arrays an extra element, and keep index 0 unused.

Personally I agree with you, mostly for performance reasons (which are not
that important for beginners) but also because the beginner will be more
accustomed to the 0-based counts, which are so prevalent in CS, from the
very start.
 
R

Roedy Green

Some beginner books suggest using a 1-based counter because that is more
"natural" and can avoid off-by-one errors. Sometimes these texts even give
arrays an extra element, and keep index 0 unused.

I think more because FORTRAN, PL/1 and Pascal were usually 1 based.

I don't think you should teach bad habits.

The sooner you start thinking of a loop as a gestalt the sooner you
will be able to write code quickly.
 
O

Oliver Wong

zero said:
Some beginner books suggest using a 1-based counter because that is more
"natural" and can avoid off-by-one errors. Sometimes these texts even
give
arrays an extra element, and keep index 0 unused.

This is, IMHO, bad. If you write code like this, the next person who
comes along will see your code, and wonder why you're not using index 0.
They won't know if it's okay to refactor the code to use 0 or not. Is 0 some
sort of special value that musn't be touched? Is this a bug and 0 should be
used? And if we start using 0, does that mean we should decrement the upper
bound, or leave it as it is?

Some languages use 0 based sequences (e.g. Java), and other languages
use 1 based sequences (e.g. XQuery). For my job, I have to switch back and
forth between these languages several times a day. Learn to adapt to
whatever the conventions are for the language you're working with.

- Oliver
 
Z

zero

This is, IMHO, bad. If you write code like this, the next person
who
comes along will see your code, and wonder why you're not using index
0. They won't know if it's okay to refactor the code to use 0 or not.
Is 0 some sort of special value that musn't be touched? Is this a bug
and 0 should be used? And if we start using 0, does that mean we
should decrement the upper bound, or leave it as it is?

Some languages use 0 based sequences (e.g. Java), and other
languages
use 1 based sequences (e.g. XQuery). For my job, I have to switch back
and forth between these languages several times a day. Learn to adapt
to whatever the conventions are for the language you're working with.

- Oliver

I don't know if I was clear enough about where I fit in. I too think using
1-based structures is Java is a bad idea. I was merely pointing out that
some beginner books suggest it. They have reasons for doing so, but IMO
those don't weigh up to the reasons for not doing it.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,023
Latest member
websitedesig25

Latest Threads

Top