JohnnyHunter
JohnnyHunter

Reputation: 388

Value of the Array elements is always zero

public int X1[]=new int[1001];
public int X2[]=new int[1001];
public int Xj;
public double R[]=new double[1001];    

public double[] Generate(int seed1,int seed2)
{
  X1[0]=seed1;
  X2[0]=seed2;

 for(int j=0;j<2;j++)
  {
     X1[j+1]=(40014*X1[j])%(2147483563);
     X2[j+1]=(40629*X2[j])%2147483399;
   System.out.println(X1[j+1]+" "+X2[j+1]);
     Xj=Math.abs(X1[j+1]-X2[j+1]);
     Xj=Xj%2147483562;
  System.out.println(Xj+" "+j);
     if(Xj>0)
       { R[j]=Xj/2147483563;}

     else if(Xj==0)
       { R[j]=2147483562/2147483563;}


  System.out.println(R[j]+" "+j);

}

In the above code when i try to print the elements of R[],it just prints 0.Can someone tell me whats wrong?I've put i out.println statements as a way of debugging the code.However they print the desired value.

Upvotes: 1

Views: 139

Answers (1)

&#211;scar L&#243;pez
&#211;scar L&#243;pez

Reputation: 236004

It's because you're performing integer divisions. For example, change this:

Xj/2147483563

... to this:

Xj/2147483563.0

Do the same with all the other divisions. Notice that in Java, int/int will always return an int. To get a result with decimals, one of the two operands (or both) must be a decimal number.

Upvotes: 10

Related Questions