...

View Full Version : Passing array with its length to a method



adil_bashir
02-12-2012, 02:42 PM
please check the following piece of code and give the correct code as my program is showing some error. here is the code.

public class passingArray
{
public int getCipherText(int[] c, int len)
{
int cipher = 0;
int e = 7;
int n = 130;
cipher = (int)((Math.pow(c[len],e)) % n);
return cipher;
}
}



public class array1
{
public static void main(String[] ar)
{
int m[] = {2,4,5};
int l = m.length;
passingArray a = new passingArray();
int ct = a.getCipherText(m[], l);
System.out.print(ct);
}
}

alykins
02-12-2012, 11:37 PM
int ct = a.getCipherText(m[], l);

needs to be


int ct = a.getCipherText(m[].length, l);


and then there is issue with


cipher = (int)((Math.pow(c[len],e)) % n);




Math.pow(double a, double b);


you are passing ints not doubles

Fou-Lu
02-13-2012, 02:40 AM
There's only actually two changes that need to be made. The call is wrong, it should be int ct = a.getCipherText(m, l);. The other is cipher = (int)((Math.pow(c[len - 1],e)) % n); since Java is 0 based for its arrays.
int will be implicitly converted during Math.pow to double.

alykins
02-13-2012, 03:36 PM
int will be implicitly converted during Math.pow to double.

That I did not know :p

Fou-Lu
02-13-2012, 03:40 PM
Yep! But only the primitives.
Wait I better test that. . .


Nope, it works with Integer as well.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum