Check my program....i want a decrement of 10 for the value of K, how can i do this ?

public class arrey
 {
public static void main (String [] args)
 {
 int i, j, k = 100;
 int [][] TwoDArray;
 TwoDArray = new int [4][];
   TwoDArray [0] = new int [1];
   TwoDArray [1] = new int [2];
   TwoDArray [2] = new int [3];
   TwoDArray [3] = new int [4];

  for ( i=0; i<TwoDArray.length; i++ )
     {
     for ( j=0; j<=i; j++ )
     TwoDArray [i][j] = --k;
     }
   for ( i=0; i<TwoDArray.length; i++ )
     {
     for ( j=0; j<=i; j++ )
     System.out.print (TwoDArray [i][j] + " ");
     System.out.println ("");
     }
  }
 }

i replaced --k with k=k-10 and it works , but this is not a proper way to decrement and the first value become '90'
i want decrement of 10 starting from 100 to 0

That's the right way to decrement by 10. Just move it to a line after you use it, not before. ie

TwoDArray [j] = k;
k = k-10; ( or k -= 10; - shorter but ewaxctly the same)

Comments
thanks

Yes, To replace "--k" with "k=k-10" is a proper way to do so. The --k is only for the purpose of one decrement. The '--' is an unary operator. There is no other unary operator for the purpose of , such as two- or three- decrement. You have to use the expression "k = k-10;" to do the job.

Edited 6 Years Ago by tong1: n/a

Well, this works but the first value is '90' instead of '100'

public class arrey
 {
public static void main (String [] args)
 {
 int Row, Elements, k = 100;
 int [][] TwoDArray;
 TwoDArray = new int [4][];
   TwoDArray [0] = new int [1];
   TwoDArray [1] = new int [2];
   TwoDArray [2] = new int [3];
   TwoDArray [3] = new int [4];

  for ( Row=0; Row<TwoDArray.length; Row++ )
     {
     for ( Elements=0; Elements<=Row; Elements++ )
     TwoDArray [Row][Elements] = k-=10;
     }
for ( Row=0; Row<TwoDArray.length; Row++ )
     {
     for ( Elements=0; Elements<=Row; Elements++ )
     System.out.print (TwoDArray [Row][Elements] + " ");
     System.out.println ("");
     }
  }
 }

and, this won't work

public class arrey
{
public static void main (String [] args)
{
int Row, Elements, k = 100;
int [][] TwoDArray;
TwoDArray = new int [4][];
TwoDArray [0] = new int [1];
TwoDArray [1] = new int [2];
TwoDArray [2] = new int [3];
TwoDArray [3] = new int [4];

for ( Row=0; Row<TwoDArray.length; Row++ )
{
for ( Elements=0; Elements<=Row; Elements++ )
TwoDArray [Row][Elements] = k;
k-=10;
}
for ( Row=0; Row<TwoDArray.length; Row++ )
{
for ( Elements=0; Elements<=Row; Elements++ )
System.out.print (TwoDArray [Row][Elements] + " ");
System.out.println ("");
}
}
}

If you want the first value to be 100 the following loop would be proper in line 13 - 17.

for ( i=0; i<TwoDArray.length; i++ )
     for ( j=0; j<=i; j++ ) {     
     TwoDArray [i][j] = k;
     	k = k-10; // equivalent to "k -= 10;"
     }

In your last version, the location of the curly brace after the loop control: for ( Row=0; Row<TwoDArray.length; Row++ ) should simply move immediately after the loop control: for ( Elements=0; Elements<=Row; Elements++ )

Edited 6 Years Ago by tong1: n/a

Comments
thanks alot

when we are writing this

TwoDArray [i][j] = k;
     	k = k-10; // equivalent to "k -= 10;"

in braces how the first value becomes 100 and when we are not writing it in braces why it start from 90 ?

This article has been dead for over six months. Start a new discussion instead.