Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    New to the CF scene
    Join Date
    Feb 2012
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Passing array with its length to a method

    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);
    }
    }

  • #2
    Senior Coder alykins's Avatar
    Join Date
    Apr 2011
    Posts
    1,662
    Thanks
    41
    Thanked 189 Times in 188 Posts
    int ct = a.getCipherText(m[], l);
    needs to be
    Code:
    int ct = a.getCipherText(m[].length, l);
    and then there is issue with
    cipher = (int)((Math.pow(c[len],e)) % n);
    Code:
    Math.pow(double a, double b);
    you are passing ints not doubles

    I code C hash-tag .Net
    Reference: W3C W3CWiki .Net Lib
    Validate: html CSS
    Debug: Chrome FireFox IE

  • Users who have thanked alykins for this post:

    adil_bashir (02-14-2012)

  • #3
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,979
    Thanks
    4
    Thanked 2,659 Times in 2,628 Posts
    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.

  • #4
    Senior Coder alykins's Avatar
    Join Date
    Apr 2011
    Posts
    1,662
    Thanks
    41
    Thanked 189 Times in 188 Posts
    Quote Originally Posted by Fou-Lu View Post
    int will be implicitly converted during Math.pow to double.
    That I did not know

    I code C hash-tag .Net
    Reference: W3C W3CWiki .Net Lib
    Validate: html CSS
    Debug: Chrome FireFox IE

  • #5
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,979
    Thanks
    4
    Thanked 2,659 Times in 2,628 Posts
    Yep! But only the primitives.
    Wait I better test that. . .

    Edit:
    Nope, it works with Integer as well.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •