Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Looping Algorithm

Tags:

java

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();
}
}
like image 504
alexkirkland Avatar asked Dec 19 '12 14:12

alexkirkland


People also ask

What is looping and example?

In computer programming, a loop is a sequence of instruction s that is continually repeated until a certain condition is reached. Typically, a certain process is done, such as getting an item of data and changing it, and then some condition is checked such as whether a counter has reached a prescribed number.

Do algorithms need loops?

Iteration provides code reusability and simplifies steps of problem-solving. In data structure and algorithms, several problem-solving approaches are based on iteration. So a good grasp of loop fundamentals is essential for mastering these approaches.

What are the 3 types of loops in programming?

Loops are control structures used to repeat a given section of code a certain number of times or until a particular condition is met. Visual Basic has three main types of loops: for.. next loops, do loops and while loops.

Do While loop algorithm examples?

Example 2: do...while loop The do...while loop executes at least once i.e. the first iteration runs without checking the condition. The condition is checked only after the first iteration has been executed. do { printf("Enter a number: "); scanf("%lf", &number); sum += number; } while(number != 0.0);


3 Answers

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.

like image 112
Peter Lawrey Avatar answered Sep 29 '22 22:09

Peter Lawrey


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.

like image 27
Clark Kent Avatar answered Sep 29 '22 22:09

Clark Kent


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();
        }
    }
}
like image 25
ihsoy ih Avatar answered Sep 29 '22 22:09

ihsoy ih