Asterisk Star pattern help


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 10 of 10

Thread: Asterisk Star pattern help

  1. #1
    Join Date
    Oct 2006
    Posts
    11

    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 04:26 PM.

  2. #2
    Join Date
    Jul 2005
    Location
    SW MO, USA
    Posts
    299
    How are you doing it? Do you have code or are you just typing them in?

  3. #3
    Join Date
    Oct 2006
    Posts
    11
    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

  4. #4
    Join Date
    Jul 2005
    Location
    SW MO, USA
    Posts
    299
    How are you doing with the rest?

  5. #5
    Join Date
    May 2004
    Posts
    40

    Post 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 01:24 PM. Reason: clarify

  6. #6
    Join Date
    Nov 2008
    Posts
    3
    ^^^ 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

  7. #7
    Join Date
    Nov 2008
    Posts
    3
    ^^ my bad.... the star pattern didn't stay the way I typed it in.... It's a small diamond consisting of 4 rows.....

  8. #8
    Join Date
    Nov 2008
    Posts
    3
    anybody?

  9. #9
    Join Date
    Nov 2008
    Posts
    1

    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&#37;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 09:09 AM. Reason: Added Code Tags

  10. #10
    Join Date
    May 2004
    Posts
    40

    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 01:20 PM. Reason: correction

Similar Threads

  1. Design pattern
    By sradha in forum C++
    Replies: 1
    Last Post: 12-12-2007, 11:38 AM
  2. Correct Design Pattern for Order Form
    By Need2CSharp in forum Architecture and Design
    Replies: 1
    Last Post: 07-28-2007, 09:50 AM
  3. Java Singleton Pattern Tutorial
    By hemanthjava in forum Java
    Replies: 2
    Last Post: 11-14-2006, 11:57 PM
  4. how to list resources in a package?
    By stoughto in forum Java
    Replies: 2
    Last Post: 06-11-2006, 10:56 AM
  5. Replies: 5
    Last Post: 01-17-2002, 09: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
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center