-
Asterisk Star pattern help
I already know how to do make the patterns
**********
*********
********
*******
******
*****
****
***
**
*
and
*
**
***
****
*****
******
*******
********
*********
**********
But im having trouble with patterns like these
http://img184.imageshack.us/my.php?i...6919698zu1.jpg
Last edited by MindGAckt; 10-16-2008 at 03:26 PM.
-
How are you doing it? Do you have code or are you just typing them in?
-
Code:
while (maxRows <= 0)
{
System.out.printf("\nYou did not enter a POSITIVE number. You entered %d!\n", maxRows);
System.out.print("Enter a positive number for the number of rows: ");
maxRows = stdIn.nextInt();
}
System.out.println();
System.out.println("Book example\n");
for (int row = 1; row <= maxRows; row++)
{
starCnt = row;
for (int star = 1; star <= starCnt; star++)
System.out.print ("*");
System.out.println();
}
System.out.println();
System.out.println("Example A\n");
for (int row = maxRows; row >= 0; row--)
{
starCnt = row;
for (int star = 1; star <= starCnt; star++)
System.out.print ("*");
System.out.println();
}
This is what i have for the first two
-
How are you doing with the rest?
-
Asterisk Star pattern help
For these exercises, you need to work out the algorithm before you start thinking about code.
For the diamond shape, I noticed that if the maximum number of rows is even, the width will be max rows - 1. Then look at the pattern (for max row = 10):
Row - Spaces - Stars
1 ----- 4 ----- 1
2 ----- 3 ----- 3
3 ----- 2 ----- 5
4 ----- 1 ----- 7
5 ----- 0 ----- 9
6 ----- 0 ----- 9
7 ----- 1 ----- 7
8 ----- 2 ----- 5
9 ----- 3 ----- 3
10 ----- 4 ----- 1
This looks to me like it would be a loop counting by 2s (1, 3, 5, 7 ...). The loop counter would be the number of stars, the number of spaces is calculated. I have 2 loops, one counting up to max rows / 2, and one counting back down.
These loops would have other loops inside it (nested loops) to print the stars and asterisks.
The triangle is easier, it has a pattern like this (again for max row = 10):
Row - Spaces - Stars
0 ----- 0 ----- 10
1 ----- 1 ----- 9
2 ----- 2 ----- 8
3 ----- 3 ----- 7
4 ----- 4 ----- 6
etc.
You need a loop that will go max rows times, then nested loops to print spaces and stars. The number of spaces and stars are calculated based on what row you're printing.
I hope I'm being clear. I'm trying to give you hints without giving away everything. It appears this exercise is about loop control and nested loops. Don't be afraid to experiment and see what you get as a result. Java compile/executes are pretty quick for code this size.
Good luck,
alan
Last edited by ajhampson; 11-24-2008 at 12:24 PM.
Reason: clarify
-
^^^ I'm having an issue with the same type of problem and I've read over what you wrote, but I think I'm getting my numbers jumbled up. Here is the pattern that I'm working on....
*
***
*****
*******
Here is my code....
final int MAX_ROWS = 4;
for (int row = 1; row <= MAX_ROWS; row++)
{
for (int space = 1; space <= row; space++)
{
for (int star = 1; star <= space; star++)
{
System.out.print ("*");
}
System.out.println();
}
}
}
}
FYI - I'm a true beginner so if you can keep it in lamins terms then that would be great
-
^^ my bad.... the star pattern didn't stay the way I typed it in.... It's a small diamond consisting of 4 rows.....
-
-
some functions for diff forms
Some functions allowing you such forms:
Code:
//StarsForms.java
public class StarsForms{
public static void main(String argv[]){
leftTriangle(1, 5, 0);
System.out.println("--------------------------------");
leftTriangle(4, 1, 0);
System.out.println("--------------------------------");
rightTriangle(2, 5, 0);
System.out.println("--------------------------------");
rightTriangle(5, 2, 0);
System.out.println("--------------------------------");
centerTriangle(1, 5, 0);
System.out.println("--------------------------------");
centerTriangle(1,-4, 1);
System.out.println("--------------------------------");
diamond(1, 9, 5);
System.out.println("--------------------------------");
diamond(2, 10, 5);
}
public static void leftTriangle(int firstrow, int lastrow, int trans){
if(firstrow < 0 || lastrow < 0)
return;
if(firstrow < lastrow){
for (int i = firstrow; i <= lastrow; i++) {
printNchars(trans, " ");
printNcharsln(i, "*");
}
}else{
for (int i = firstrow; i >=lastrow ; i--) {
printNchars(trans, " ");
printNcharsln(i, "*");
}
}
}
public static void rightTriangle(int firstrow, int lastrow, int trans){
if(firstrow < 0 || lastrow < 0)
return;
if(firstrow < lastrow){
for (int i = firstrow; i <= lastrow; i++) {
printNchars(trans, " ");
printNchars(lastrow-i, " ");
printNcharsln(i, "*");
}
}else{
for (int i = firstrow; i >=lastrow ; i--) {
printNchars(trans, " ");
printNchars(firstrow-i, " ");
printNcharsln(i, "*");
}
}
}
public static void centerTriangle(int minStars, int rows, int trans){
if(minStars < 0 || rows == 0)
return;
if(0 < rows){
for (int i = 0; i < rows; i++) {
printNchars(trans, " ");
printNchars(rows-i-1, " ");
printNcharsln((i*2)+minStars, "*");
}
}else{
rows = -rows;
for (int i = rows-1; i >= 0; i--) {
printNchars(trans, " ");
printNchars(rows-i-1, " ");
printNcharsln((i*2)+minStars, "*");
}
}
}
public static void diamond(int minStars, int rows, int trans){
if(minStars < 0 || rows == 0)
return;
centerTriangle(minStars, (rows+1)/2, 0 + trans);
centerTriangle(minStars,-rows/2, rows%2 + trans);
}
public static void printNchars(int n, String c){
for(int i = 0; i<n; i++)
System.out.print(c);
}
public static void printNcharsln(int n, String c){
for(int i = 0; i<n; i++)
System.out.print(c);
System.out.println();
}
}
I hope it can help you
Last edited by Hack; 11-21-2008 at 08:09 AM.
Reason: Added Code Tags
-
Printing stars
lilcoop_69
^^^ I'm having an issue with the same type of problem and I've read over what you wrote, but I think I'm getting my numbers jumbled up. Here is the pattern that I'm working on....
The problem is that you've nested all your loops. The one to print spaces and the one to print stars need to be separate:
Code:
loop to do rows
{
loop to print spaces
loop to print stars
}
Sorry you were left hanging, I haven't checked this thread in a while and I apparently didn't set it to mail me when the thread changed. Post again if you have other questions. BTW, if you put CODE tags around your code, it will help us when looking at it.
enbee772
Some functions allowing you such forms:
enbee - as a general rule, you shouldn't post homework solutions here. We try to guide people and answer specific questions, but not give them the answers. This helps new developers to think for themselves and learn to solve a variety of problems. It's great that you want to help, but you need to think about giving pointers vs. giving answers.
Thanks,
alan
Last edited by ajhampson; 11-24-2008 at 12:20 PM.
Reason: correction
Similar Threads
-
Replies: 1
Last Post: 12-12-2007, 10:38 AM
-
By Need2CSharp in forum Architecture and Design
Replies: 1
Last Post: 07-28-2007, 08:50 AM
-
By hemanthjava in forum Java
Replies: 2
Last Post: 11-14-2006, 10:57 PM
-
By stoughto in forum Java
Replies: 2
Last Post: 06-11-2006, 09:56 AM
-
By Jeff Lacoste in forum .NET
Replies: 5
Last Post: 01-17-2002, 08:24 PM
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
Forum Rules
|
Top DevX Stories
Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL
|
Bookmarks