• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

help on printing letter X using *

 
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi guys,

i need a help on this assignment. the question is to require me to print a letter X onto screen using *.

but no matter how i try, the o/p is

**^^^
**^^^
***^^
**^*^
**^^*

any advice please?
-------------
/**
* Write a description of class ForEg3 here.
*
* @author (your name)
* @version (a version number or a date)
*/
class ForEg3
{
public static void main (String[]args)
{
for (int row=1; row <=5; row++)
{
for (int col=1; col <=5; col++)

if (row==col || (col <= (5-col)) )
System.out.print("*");
else System.out.print("^");
System.out.println();
}
}
}

---------
 
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
[FBR - let's try to be a little nicer]


Originally posted by xiao sean:
hi guys,

i need a help on this assignment. the question is to require me to print a letter X onto screen using *.

but no matter how i try, the o/p is

**^^^
**^^^
***^^
**^*^
**^^*

any advice please?
-------------
/**
* Write a description of class ForEg3 here.
*
* @author (your name)
* @version (a version number or a date)
*/
class ForEg3
{
public static void main (String[]args)
{
for (int row=1; row <=5; row++)
{
for (int col=1; col <=5; col++)

if (row==col || (col <= (5-col)) )
System.out.print("*");
else System.out.print("^");
System.out.println();
}
}
}

---------


[ August 22, 2007: Message edited by: Fred Rosenberger ]
 
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
We're happy to help, but of course, nobody is going to give you the solution. Have you tried puting some extra println's in there to see what the variables are at each step?

for starters, i'd put on just before your 'if' condition, to see what row and col are each time through.

you will probably have to add something else though, to be sure you get BOTH the print and the if-else inside the body of the loop....
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by xiao sean:
...i need a help on this assignment. the question is to require me to print a letter X onto screen using *...


Just to clarify, do you want the output to look something like this?

And do you want the '^' characters to show? Or are these supposed to be blank spaces?

(Joseph, you're right that we won't do people's homework for them. But xiao is only asking for "help on this assignment." And that's what we're here for. )
[ August 22, 2007: Message edited by: marc weber ]
 
xiao sean
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks for replying. yes, you are right.

i am trying to get the exact output you execute just now...
 
xiao sean
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i found a new solution. but it output more than what i expect

------
-------


output look like:

*^^^^*^
^*^^*^^
^^*^^^
^*^^*^^
*^^^*^

[edited to use code tags]
[ August 22, 2007: Message edited by: Fred Rosenberger ]
 
fred rosenberger
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please use code tags when posting your code. you can either highligh what you've pasted in, then click that little "code" button in the "instant UBB" section, or click the button first, then past your code between the XML-looking tags.

I think you have a problem with your logic. The first code sample you posted was close, but not quite there. Take a look at this code:


What i've done is put in a print statement that shows exactly what your if condition is each time through the loop. I put in a couple of printlns so that each row prints in a paragraph.

Try running this code, and look at the ouput. See if you can figure out what your problem is (although there may be more than one).
 
marc weber
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In your original code, your first condition (if row==col) is correct. By itself, this produces...

It's your second condition (if col<=(5-col)) that's causing problems.

In your new code, you got closer by using a lot of "if" statements. But look at the numbers used in these statements. Do you see a pattern (a relationship between row and col) that could be expressed in a single line?
 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi xiao sean,



I think, this solves your problem right?

Regards,
Sankar
[ August 22, 2007: Message edited by: marc weber ]
 
xiao sean
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Fred Rosenberger:
[What i've done is put in a print statement that shows exactly what your if condition is each time through the loop. I put in a couple of printlns so that each row prints in a paragraph.

Try running this code, and look at the ouput. See if you can figure out what your problem is (although there may be more than one).[/QB]





thanks for the reply, after i look through and compare the codes with 'X', actually all these codes doesnt match. for eg. if (5==1) able to fufill * but (1<= (5-1)) actually cant fufill the * if we place it into the 'X'.

if i think at this way is wrong, please enlighten me.
 
marc weber
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by sankar narayanan:
hi xiao sean,



I think, this solves your problem right?

Regards,
Sankar

[ August 22, 2007: Message edited by: marc weber ]


Sankar,

As Fred said above, "We're happy to help, but of course, nobody is going to give you the solution." Or as it says at the top of the beginner's forum, "We're all here to learn, so when responding to others, please focus on helping them discover their own solutions, instead of simply providing answers." Especially when this is homework.

Thanks!
 
marc weber
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by marc weber:
... It's your second condition (if col<=(5-col)) that's causing problems.

In your new code, you got closer by using a lot of "if" statements. But look at the numbers used in these statements. Do you see a pattern (a relationship between row and col) that could be expressed in a single line?


Hint: What's always true about row and col if...
(row == 1 && col == 5) or
(row == 2 && col == 4) or
(row == 4 && col == 2) or
(row == 5 && col == 1) ?
 
xiao sean
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by marc weber:
In your original code, your first condition (if row==col) is correct. By itself, this produces...

It's your second condition (if col<=(5-col)) that's causing problems.

In your new code, you got closer by using a lot of "if" statements. But look at the numbers used in these statements. Do you see a pattern (a relationship between row and col) that could be expressed in a single line?





hi, thanks for your reply and help. this really helps me alot. i'm able to execute the program.
 
xiao sean
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by sankar narayanan:
[QB]hi xiao sean,


I think, this solves your problem right?

Regards,
Sankar

QB]




Hi Sankar, thanks for your great help. but i prefer to debug on my own. really thanks alot.
 
marc weber
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by xiao sean:
...this really helps me alot. i'm able to execute the program.


Yeah, that's a cleaned up version of what you had earlier. But there's a much shorter way to say...

(row == 1 && col == 5) || (row == 2 && col == 4) || ( row == 4 && col == 2) || (row == 5 && col == 1)

In each of these cases, what's the relationship between row and col?
 
sankar narayanan
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hello Ranchers,

Ok. I found the mistake I have done. I am also here to learn.

Thanks for notifying.

Regards,
Sankar
 
fred rosenberger
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
when programming, you often have to look for patterns. here, try and find a relationship between

1 and 5
2 and 4
3 and 3
4 and 2
5 and 1

what do all these pairs of numbers have in common? you could say "well, they're all less than 6. but can you see any ADDITIONal ways they are related?
 
xiao sean
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
yes.i got what you mean. they actually form a 'X'. by crossing each other....

thanks
 
marc weber
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by xiao sean:
yes.i got what you mean. they actually form a 'X'. by crossing each other...


Yes, but... As Fred said, there's something additional to notice.
 
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here is the simple general solution. Just modify the value of SIZE variable in the code to print different size of X with *. But the value must be an odd number.
 
reply
    Bookmark Topic Watch Topic
  • New Topic