package MyPackage;
public class StarPattern {
public static void main(final String[] args) {
int row;
int column = 0;
for (row = 0; row >= 6; row++) {
System.out.println(" * ");
}
}
}
You have misunderstood the for statement declaration:
for (row = 0; row >= 6; row++) {
Formally, the row >= 6 is called the Expression. This is evaluated before the loop body (formally, the "Statement") is executed:
If it is evaluated to true, the loop body is executed; then the row++ is evaluated (the ForUpdate); then the iteration happens again.
If it evaluates to false, the loop body is not executed, and execution moves on to execute the next statement after the loop.
If row = 0, then row >= 6 is immediately false, so the loop body never executes.
I think you have perhaps assumed the other way round: that the loop stops once the Expression evaluates to true, but keeps going if it is false. I don't think there is a particular reason why it couldn't work this way; but this just isn't the way that loop semantics in Java (and many other languages) are defined.
So, simply invert the expression:
for (row = 0; row < 6; row++) {
// ^ Here
It should be like this
int row;
int column = 0;
for (row = 0; row < 6; row++) {
System.out.println(" * ");
}
You want to do the System.out every iteration that row is less than 6.
for (row = 0; row < 6; row++) {
System.out.println(" * ");
}
Related
I'm writing a method that traverses through a 2d array in row-major order and at the start of each row, I initialize a count variable to zero. In the inner loop, if a value is non-zero I increment the count variable. At the end of the row, if the count variable is not exactly equal to 1, return false. Ive been working on this for about 2 weeks and can't find my error. Please point me in the right direction.
** Don't mind the print statements I'm trying to see how much the count is and my code only seems to hit the second row of the array
public static boolean isGPM(int[][] matrix) {
int count =0;
for (int row = 0; row < matrix.length; row++) {
count =0;
for (int col = 0; col < matrix[row].length; col++) {
if (matrix[row][col] > 0) {
count++;
}
else {
return !gpm;
}
}
}
System.out.println(count);
return gpm;
}
If I understand you correctly, you only care about the per-row count. This should work:
int count = 0;
// Process each row...
for (int row = 0; row < matrix.length; row++) {
count = 0;
// Process each column...
for (int col = 0; col < matrix[row].length; col++) {
// Check the cell.
if (matrix[row][col] != 0) {
count++;
}
}
// Row processing complete. Check count.
if (count != 1) {
System.out.println(count);
return gpm;
}
}
This method is simple, there is 2D array, not a rectangle, the purpose is to check the values in each column whether they are increasing or not, if they are in an increasing order, return true, else return false.
The shape of the array is like the following, it is a Young Tableaux
{
[1,4,5,10,11],
[2,6,8],
[3,9,12],
[7]
}
The main properties of a young tableaux:
it consists of cells which are filled with integers, and arranged in
left-justified rows,
no row is longer than a preceding row,
from left to right in any row, and down any column the integers are increasing,
the set of integers used is {1, 2, . . . , n} where n is the number
of cells
How I solve it?
My approach is simple, first convert this 2D array into a rectangle matrix, if some position is empty, then filled it with 0.
Then check the column one by one, if found a error, then break, and return the result.
It works, I just wonder if there is a better apporach for this.
public static boolean columnValuesIncrease(int[][] t) {
//How many columns are there?
int columnCounts = t[0].length;
int rowCounts = t.length;
//create a rectangle matrix, fill 0 when outIndex
int[][] addZero = new int[rowCounts][columnCounts];
for (int row = 0; row < rowCounts; row++) {
for (int col = 0; col < t[0].length; col++) {
try {
addZero[row][col] = t[row][col];
} catch (IndexOutOfBoundsException e) {
addZero[row][col] = 0;
}
}
}
//Let's check the damn column!
boolean mark = true;
myLoop:
for (int col = 0; col < columnCounts; col++) {
for (int row = 0; row < rowCounts; row++) {
if (row + 1 < rowCounts && col + 1 < columnCounts) {
if (addZero[row + 1][col] != 0) {
mark = addZero[row][col] <
addZero[row + 1][col] ? true : false;
}
}
if (!mark) {
break myLoop;
}
}
}
return mark;
}
This approach takes a row. It considers 'this' row and the one after it. It considers N number of columns, where N is the minimum of the number of columns in this row and the row after. In math, if R is the number of rows in this 2D matrix, take some r1: r1 ∈ [0, R) and r2 = r1 + 1. Then, N = min{num_cols(r1), num_cols(r2)}.
In column n, where n ∈ [0, N], if the value at the column in the next row happens to be smaller than the value in the preceding row, it returns false. If everything else worked, it returns true.
public static boolean columnValuesIncrease(int[][] t) {
for(int i = 0 ; i < t.length - 1 ; i++)
for(int j = 0 ; j < Math.min(t[i].length, t[i+1].length) ; j++)
if(t[i][j] > t[i+1][j])
return false;
return true;
}
I'm trying to test each row in my array to see if they are even or odd. If the row is even i want to change the random value in the elements of the row to 0. If the row is odd i want to change the elements to 1. Ive been able to create the element and print it out but im stuck on how to test the rows. I know to test if a number is even you use (i % 2 == 0) but im not sure what coding i should use.
public static void main(String[] args) {
int[][] box;
box = new int[2][2];
int row;
int column;
for (row = 0; row < box.length; row++) {
for (column = 0; column < box[row].length; column++) {
box[row][column] = (int) (Math.random() * 100);
}
}
//where im having issues
// i get error 'bad operand types for binary operator'
if (box[row] % 2 == 0) {
for (row = 0; row< box.length;row++){
box[row][column] = [0][];
}
}
else{
for(row = 0; row < box.length; row++){
box[row][column] = [1][];
}
}
for (row = 0; row < box.length; row++) {
for (column = 0; column < box[row].length; column++) {
System.out.print(box[row][column] + " ");
}
System.out.println();
}
}
}
you can use Arrays built in function fill(int[],value) that takes two argument, First 1d array and second a value to fill
//also if you are checking is row is even or odd divide row not box[row]
if (row % 2 == 0) {
Arrays.fill(box[row],0);//set 0 to every element in this wor
}
else{
Arrays.fill(box[row],1);//set 1 to every element in this wor
}
private static boolean allNinePresent(int[][] array){
int total = 0;
for (int row = 0; **array.length**; row++){
for (int col = 0; **array[row].length**; col++){
int addEach = array[row][col];
total = addEach + total;
}
}
if (total == 45){
return true;
} else {
return false;
}
}
Shouldn't the array be an int? Why is it converting it from an int to boolean? How can I fix this.
As pointed in official tutorial for statement looks like
for (initialization; termination; increment) {
statement(s)
}
and
When the termination expression evaluates to false, the loop terminates.
In your case *array.length is used in place of termination, but in Java booleans can't be represented by integers, so something like if(1) is not valid. This means that you need to be more specific and use actual expression which can be evaluated to boolean (true or false), like
true
false
a<b
a>=b
That is why your loops should look more like
for (int row = 0; row<array.length; row++){
for (int col = 0; cor<array[row].length; col++){
BTW
if (total == 45){
return true;
} else {
return false;
}
can be rewritten to something simpler like
return total == 45;
for (int col = 0; array[row].length; col++){
second part accepts a boolean expression,
; array[row].length;
you are passing int
you need
row < array.length
and
col < array[row].length
The second part of the for loop isn't the stopping value, it's the condition under which the loop will continue looping. Use row < array.length and col < array[row].length.
public class MakeQuilt {
public static void main (String[] args){
char [][] myBlock = new char [4][5];
char [][] myQuilt = new char [12][4];
for(int row = 0; row < myBlock.length; row++){
for(int column = 0; column < myBlock[row].length; column++){
if(column == 0 || row == 3)
myBlock[row][column]='X';
else if(row == column || (row == 2 && column == 1))
myBlock[row][column]='+';
else
myBlock[row][column]='.';
}
}
displayPattern(myBlock);
displayPattern(myQuilt);
}
public static void displayPattern(char[][] myBlock){
for(int row = 0; row < myBlock.length; row++){
for(int column = 0; column < myBlock[row].length; column++){
System.out.print(myBlock[row][column]);
}
System.out.println();
}
System.out.println();
}
public static void fillQuilt(char[][] myQuilt){
for(int row = 0; row < myQuilt.length; row++){
for(int column = 0; column < myQuilt[row].length; column++){
myQuilt[row][column] =('?');
}
}
}
}
Can't seem to figure out why my char array myQuilt won't fill with question marks but instead is filled with nothing? (output shows a bunch of 0's). Not sure how to change the displayPattern method to output ?'s in the myQuilt array.
Before calling displayPattern, you have to fill quilt somewhere. i.e.
displayPattern(myBlock);
fillQuilt(myQuilt);
displayPattern(myQuilt);
Question: You define the fillQuilt(...) method where you would fill an array with question mark characters, but where do you ever call this method?
Answer: You don't (at least you don't show it), and if it's never called, it will never do its thing. The solution is to call the fillQuilt method, passing in myQuilt where you need it to do its actions: fillQuilt(myQuilt);. Understand that programming takes things very literally: they only do what you explicitly program them to do, nothing less, nothing more.
I can't see any call to your fillQuilt() method in your main.
Don't you have to call fillQuilt() somewhere before printing?