alexkirkland
alexkirkland

Reputation: 83

Looping Algorithm

How do I make this:

*******
-*****-
--***--
---*---
--***--
-*****-
*******

The following is my code that I have written to try to accomplish the above, but it is not working as expected:

    public static void stars(/*int jmlBaris*/) {
    for ( int i = 7; i >= 1; i-=2) {
        for (int j = 1; j <= i; j++) {

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

    for (int i = 1; i <= 7; i+=2) {
        for (int j = 1; j <= i; j++){
            System.out.print("*");
            }
        System.out.println("");
    }
}
public static void main(String[] args) {
    stars();
}
}

Upvotes: 8

Views: 675

Answers (8)

Peter Lawrey
Peter Lawrey

Reputation: 533500

This is how I might write it.

// three loops
public static void stars(int size) {
    for (int y = 0; y < size; y++) {
        for (int i = 0; i < y && i < size - y - 1; i++)
            System.out.print(' ');
        for (int i = Math.min(y, size - y - 1); i < Math.max(y + 1, size - y); i++)
            System.out.print('*');
        System.out.println();
    }
}

or

// two loops
public static void stars(int size) {
    for (int y = 0; y < size; y++) {
        for (int x = 0; x < size; x++)
            System.out.print(
                    (x >= y && x < size - y) ||
                            (x >= size - y - 1 && x <= y) ? '*' : ' ');
        System.out.println();
    }
}

or

// one loop
public static void stars(int size) {
    for (int i = 0; i < size * size; i++) {
        int y = i / size, x = i % size;
        System.out.print(
                (x >= y && x < size - y) ||
                        (x >= size - y - 1 && x <= y) ? '*' : ' ');
        if (x == size - 1)
            System.out.println();
    }
}

Note: Whether this uses one, two or three loops, the time complexity is O(N^2). A simple way to determine this is the number of stars produced is O(N^2) no matter how it is done.

Upvotes: 9

irreputable
irreputable

Reputation: 45433

    int N = 7;
    for (int y=0; y<N; y++)
    {
        for (int x=0; x<N; x++)
            System.out.print( (y-x)*(N-y-x-1)<=0 ? '*' : '-');
        System.out.println();
    }

or, more symmetrically,

    int n = 3;
    for (int y=-n; y<=n; y++)
    {
        for (int x=-n; x<=n; x++)
            System.out.print( y*y>=x*x ? '*' : '-');
        System.out.println();
    }

Upvotes: 0

rmist
rmist

Reputation: 483

For a beginner in algorithms I would recommend you to break down the structure in sub-parts and then try to solve the pattern.

For this specific pattern it could be broken down into several triangles. Each triangle is then solved by different for loops as shown in the image below.

dividing the pattern into substructures

public static void printPattern(int num) {
    // this loop generates first 4 lines
    for (int i = 0; i < num / 2 + 1; i++) {
        // draws the red triangle of '-'
        for (int j = 0; j < i; j++) {
            System.out.print("-");
        }
        // draws the green triangle of '*'
        for (int j = i; j < num / 2 + 1; j++) {
            System.out.print("*");
        }
        // draws the blue triangle of '*'
        for (int j = i + 1; j < num / 2 + 1; j++) {
            System.out.print("*");
        }
        // draws the orange triangle of '-'
        for (int j = 0; j < i; j++) {
            System.out.print("-");
        }
        System.out.println();
    }

    /* this loop generates last 3 lines */
    for (int i = 0; i < num / 2; i++) {
        // draws the green triangle of '-'
        for (int j = i + 1; j < num / 2; j++) {
            System.out.print("-");
        }
        // draws the red triangle of '*'
        for (int j = 0; j < i + 2; j++) {
            System.out.print("*");
        }
        // draws the orange triangle of '*'
        for (int j = 0; j < i + 1; j++) {
            System.out.print("*");
        }
        // draws the blue triangle of '-'
        for (int j = i + 1; j < num / 2; j++) {
            System.out.print("-");
        }
        System.out.println();
    }
}

Using similar technique you could generate any pattern.

Upvotes: 1

Reinstar
Reinstar

Reputation: 146

You have little missing to put space on your code. I don't care about right space, who can see that? But left space is very important!!

Try this:

public static void stars(/*int jmlBaris*/) {
    for ( int i = 7; i >= 1; i-=2) {
        for (int k = 0; k < ((7-i) / 2); k++){ /* Missing Here */
            System.out.print(" "); /* Missing Here */
        } /* Missing Here */

        for (int j = 1; j <= i; j++) {
            System.out.print("*");
        }

        System.out.println("");
    }

    for (int i = 1; i <= 7; i+=2) {
        for (int k = 0; k < ((7-i) / 2); k++){ /* Missing Here */
            System.out.print(" "); /* Missing Here */
        } /* Missing Here */

        for (int j = 1; j <= i; j++){
            System.out.print("*");
        }

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

Upvotes: 0

PatrickW
PatrickW

Reputation: 31

    public static void stars(/*int jmlBaris*/){
    String starstr = "*";
    String blank = "_";
    int spaceBlank;;
    for(int i=7; i>=1;i-=2){
        spaceBlank = (7-i)*.5;
        String starrep = StringUtils.repeat(starstr, i);
        String blankrep = StrinUtils.repeat(blank, spacesBlank);
        system.out.println(blankrep + starrep + blankrep);
    }
    for(int j=3 j<=7; j+=2){
        spaceBlank = (7-j)*.5;
        starrep = StringUtils.repeat(starstr, j);
         String blankrep = StrinUtils.repeat(blank, spacesBlank);
        system.out.println(blankrep + starrep  + blankrep);
    }
}    
public static void main(String[] args){
    stars();
}

Upvotes: 0

ihsoy ih
ihsoy ih

Reputation: 1018

Try something like this code I compiled on IDEOne (it seems to work, though): http://ideone.com/9xZ1YB

class Main
{
    public static void main(String[] args)
    {
        stars();
    }

    static void stars()
    {
        final int MAX_WIDTH = 7;

        for (int i = 0; i < 7; ++i)
        {
            int width;

            if (i < 3) width = MAX_WIDTH - i * 2;
            else if (i > 3) width = (i - 3) * 2 + 1;
            else width = 1;

            // Before spaces

            for (int j = 0; j < (MAX_WIDTH - width) / 2; ++j)
            {
                System.out.print(" ");
            }

            // Stars

            for (int j = 0; j < width; ++j)
            {
                System.out.print("*");
            }

            // After spaces

            for (int j = 0; j < (MAX_WIDTH - width) / 2; ++j)
            {
                System.out.print(" ");
            }

            System.out.println();
        }
    }
}

Upvotes: 2

Clark Kent
Clark Kent

Reputation: 1176

I would do something like this with substrings.

String a = "*******";  //7 stars
String blank = "        "; //7 spaces
int j = 7;
for (int i = 0; i < 7; i++) {
    if (i > j){
        System.out.print(blank.substring(0,i));
        System.out.println(a.substring(i,j));
        }
    else{
        System.out.print(blank.substring(0,j));
        System.out.println(a.substring(j,i));
        }
    j--;
}
System.out.println(a);

**Previous edit wouldn't have worked. Changes made.

This works.

Upvotes: 4

Artem Sobolev
Artem Sobolev

Reputation: 6069

If I understood you right, your problem is to print indent in lines 2-7.

Imagine same problem with asterisk symbol replaced by 'x' and whitespace replaced by '-'. Then you need to draw

xxxxxxx
-xxxxx-
--xxx--
---x---
--xxx--
-xxxxx-
xxxxxxx

That means you should output 0, 1, 2 space(s) before asterisks in first, second, thrid strings respectively. I let details for you to figure them out.

Upvotes: 0

Related Questions