Jaya create a star pattern [closed] - java

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I tried to made a star pattern but I did not know how to do it, I know how to made start pattern when the start or the spaces are continuous, but when it is a start and spaces like the pattern that I show below, I do not how to do it.
* *
* *
*
* *
* *

You need to find the relationship between the corners of the cross/star. Take this star for example of size 5.
0 1 2 3 4
0 * *
1 * *
2 *
3 * *
4 * *
In a cross in the diagonal from (0,0) to (4,4), the indices are the same (in the code this means row == col).
Also, you can notice that in the diagonal from (0,4) to (4,0) indices always sum up to 4, which is size - 1 (in the code this is row + col == size - 1).
Therefore in the code, you will need to loop through rows and then through columns. Each time you have to check if the above conditions are fulfilled.
Code:
class Main {
public static void main(String[] args) {
printCross(5); // Vertical size of cross
}
public static void printCross(int size) {
for (int row = 0; row < size; row++) {
for (int col = 0; col < size; col++) {
if (row == col || row + col == size - 1) {
System.out.print('*');
} else {
System.out.print(" ");
}
}
System.out.println();
}
}
}

Related

Creating Shapes with for loops in Java [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
I'm a newbie to Java and I need to print out shapes using for loops.
1)
*
***
*****
***
*
2)
*****
****
***
**
*
*
**
***
****
******
I really would appreciate your help!
Thanks in advance!
To do this, first you need to be able to describe the pattern.
If you can reliably draw the "next" shape for the integer input, then you know the pattern.
Then you think of which parts of the pattern are counted items (3 stars, then a blank, for example). Once you identify the counted items, you make a relationship between that count and the number one typed in.
Finally, for each counted item you write a for loop that, within it's block, contains code to output one item. The control structure of the for loop should make sure that the count is exercised the correct number of times.
For each item where the count is 1, you may omit the for loop and simple print the item out directly.
--- Examples --
*
***
*****
*******
*********
***********
Thought process:
The first line is 5 spaces then a star.
The second line is 4 spaces then 3 stars.
The third line is 3 spaces then 5 starts.
I see a pattern, the first line is (5 spaces - the line number)
for (int spaces = 0; spaces < 5 - line_number; spaces++) {
print(" ");
}
That other pattern, was (counting stars) 1, 3, 5, 7, 9.
The pattern is one of differences 3-1 = 2, 5-3 = 2, 7-5=2, etc.
I need to start with one star, and add two stars per line.
for (int stars = 1; stars < 1 + 2 * line_number; stars++) {
print("*");
}
Now I need a loop for the lines
for (int line_number = 0; line_number < 5; line_number++) {
print("\n");
}
Now I'll put it all together
for (int line_number = 0; line_number < 5; line_number++) {
for (int spaces = 0; spaces < 5 - line_number; spaces++) {
print(" ");
}
for (int stars = 1; stars < 1 + 2 * line_number; stars++) {
print("*");
}
print("\n");
}

Remove one element in each row from a solved Sudoku puzzle [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
I am trying to create a method that will remove one random element in each row from a solved Sudoku matrix that I have generated. How can I achieved this?
Solved Sudoku Generator:
public static void generate_Sud (int n, int [][] S){ // Generates Sudoku method
for (int r=0; r<=n-1; r++){
int startNum = (int) (Math.sqrt(n) * (r % Math.sqrt(n)) + (r/Math.sqrt(n)));
for (int c=0; c<=n-1; c++){
S[r][c] = ((startNum + c) % n) + 1;
}
}
}
Sudoku Matrix Generated
for a 3x3 Sodoku:
for(int i = 0; i < 3; i++){
int randomPosition = (int) Math.floor(Math.random() * 3) //Random number between 0 and 2
S[i][randomPosition] = -1; // -1 or whatever you use to represent blank
}

Java printing patterns [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I'm new in Java.
I have next code:
public static void main(String[] args) {
for(int k = 10; k > 0; k--)
{
for(int l=0; l < k-1; l++)
{
System.out.print(' ');
}
for(int n=10; n > k-1; n--)
{
System.out.print('*');
}
System.out.println();
}
}
It prints this:
*
**
***
****
*****
******
*******
********
*********
**********
But I want to print it with empty inside like this:
*
**
* *
* *
* *
* *
* *
* *
* *
**********
Can anyone to explane me how to do it.
I understand that this is not a place where solve homework tasks. But can somebody tell me the algorithm for solving the problem in words.
I do not need a ready solution because I want to understand and solve it by myself. So how I can put spaces inside?
Hint: In the second loop you should print a '*' for the first and last values of n, not all of them. For the other positions, print a ' '.
Hint: in the second inner for loop, you could have an if statement where you check for n's greatest and lowest values, and k's lowest value, then print a '*', otherwise print a ' '. Like so:
for(int n = 10; n > k - 1; n--){
if(n == 10 || n == k || k == 1){
System.out.print('*');
} else {
System.out.print(' ');
}
}

How to create an histogram of asteriks from a Java array of number repetitions? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
I have an array with repetitions of numbers and I need to show them in an "histogram" made by "*". The histogram should looks like this:
*
* *
* * *
* * * *
* * * * * * * *
* * * * * * * * * *
* * * * * * * * * *
* * * * * * * * * *
1 2 3 4 5 6 7 8 9 10
I have this array
int[] repetition = new int[] {4,6,4,4,6,5,7,4,3,3};
and I was able to print it horizontally like this:
1*****
2******
3****
4****
5******
6*****
7*******
8****
9***
10***
How can I create the vertical histogram?
First you compute the maximum height of the histogram.
max=0;
for(int i: repetition)
if(i>max)
max=i;
Then, you print like this, from top to bottom:
for(j=max;j>=1;j--){ //For each possible height of the histogram.
for(k=0;k<repetition.length;k++) //Check height of each element
if(repetition[k]>=j)
System.out.print("*"); //Print * if the k-th element has at least a height j.
else
System.out.print(" "); //Else, do print blank space
System.out.println(); //Newline after every row.
}
PS: This is just an idea, not a complete working code, and it works only for positive height values!
It sounds like you have code to "bin" the data, effectively creating the histogram. Add some code to track the maximum count across all the bins. So, in your example above, the maximum would be 8 (from bin 7).
Then, set a threshold, starting at that maximum, and counting down to one. On each iteration, print lines with asterisks in the columns corresponding to the bins that meet or exceed the threshold. So on the first line, only column 7 would get an asterisk, because it's the only bin that meets the current threshold of 8. The next line (threshold 7), columns 5 and 7 would get asterisks. And so on.
Hopefully that's enough help to write the code yourself.
Integer is used to use the Collections max function.
public static void main(String[] args) {
Integer[] repetition = new Integer[] { 4, 6, 4, 4, 6, 5, 7, 4, 3, 3 };
int maxValue = Collections.max(Arrays.asList(repetition));
System.out.println("Maximum: " + maxValue);
for (int i = maxValue; i > 0; i--) {
for (int j = 0; j < repetition.length; j++) {
if (repetition[j] >= i) {
System.out.print(" * ");
} else {
System.out.print(" ");
}
}
System.out.println();
}
for (int j = 0; j < repetition.length; j++) {
System.out.print(" " + (j + 1) + " ");
}
}
I am not going to give you any code, but I can give you an idea to start off of.
For the vertical histogram, even though it looks like it is being printed top to bottom, it's not; you just can't print vertically. Using a loop (the highest value in the array being the starting value, 0 being the sentinel value, and decrementing by 1 every iteration) where every iteration represents a horizontal line on the histogram, you should determine which x-label's asterisk(s) need to be printed on that line, and for those labels that don't have an asterisk on that line, place a space there instead (for formatting).Then, create another loop and use it to list all the x-labels at the bottom.
Hope this helps!
public static final void printHistogram(int[] coll) {
int max = coll[0];
for (int i : coll)
max = i > max ? i : max;
boolean[][] array = new boolean[max][coll.length];
for (int i = coll.length - 1; i >= 0; i--) {
for (int j = 0; j < coll[i]; j++) {
array[j][i] = true;
}
}
for (int i = array.length - 1; i >= 0; i--) {
boolean[] booleans = array[i];
for (boolean b : booleans) {
System.out.print(b ? '*' : ' ');
}
System.out.println();
}
for (int i = 1; i <= array.length; i++) {
System.out.print(i);
}
}
This works as expected.

Need help reducing triple for loop to increase efficiency

for (int i = 0; i < 3; ++i) {
for (int k = 0; k < 7; ++k) {
for (int h = i; h < 4 + i; ++h) {
result = state.getAt(k, h);
if (result == 1) {
++firstpl;
}
if (result == 2) {
++secondpl;
}
if (firstpl > 0 && secondpl > 0) {
break;
}
//y = k;
}
if (firstpl == 0 && secondpl == 0) {
break;
} else if (firstpl > secondpl) {
score += firstpl * firstpl;
//if(state.getHeightAt(y)-3 < 3) score += 3+firstpl*2;
} else {
score -= secondpl * secondpl;
//if(state.getHeightAt(y)-3 < 3) score -= 3+secondpl*2;
}
firstpl = 0;
secondpl = 0;
}
}
basically I have a 7 by 6 grid. I am going through 7 columns and looking at every 4 consecutive blocks vertically. Since there is 6 blocks upward. There is 3 four consecutive block for each column. State.getAt(k,h) takes in a x and y and returns a value.
I don't think you can improve on this, unless you can figure out an alternative representation for this "state" that allows this computation to be performed incrementally.
And since you have failed to properly explained what the state or the calculation actually mean, it is difficult for anyone but you to figure out whether an alternative approach is even feasible. (And I for one am not going to attempt to reverse engineer the meaning from your code.)
OK. For Connect4, the win / lose is a line of 4 checkers horizontally, vertically or diagonally in the 7x6 grid. So what you could do is represent the score-state as an array of counters, corresponding to each of the columns, rows and diagonals in which a winning line could be made. (7 + 5 + 4 + 4 = 20 of them => 20 counters) Then construct a static mapping from an (x,y) position to the indexes of lines that pass through that. When you add a checker at point (x,y) you look up the counters and increment them. When you remove a checker ... decrement.
I'm not sure how that relates to your existing scoring function ... but then I don't see how that function relates to a strategy that would win the game. Either way, you could potentially use the approach above to calculate scores incrementally.

Categories

Resources