Results 1 to 9 of 9

Thread: Arrays ?

  1. #1
    earthkid123 is offline Member
    Join Date
    Mar 2014
    Posts
    16
    Rep Power
    0

    Default Arrays ?

    I have to write a program that computes factorials up to 50
    public static void main(String[] args)
    {
    int [] a= new int [100];
    for(int n=0; n<=50; n++)
    {
    double factorial = 1;
    for (int multiplier=1; multiplier<=50; multiplier++)
    {
    factorial = factorial * multiplier;
    System.out.println(multiplier + "!" + factorial);
    }
    }
    }
    }
    // I have this as my code so far I just don't know how to incorporate the array in there which i need to do as well as I dont get why all the output comes out for every single thing . IT will write 1 factorial and display all the factorials up to 50 and do the same pattern over and over again

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,784
    Rep Power
    5

    Default Re: Arrays ?

    I'm still not certain what you want to do. You have an int array but are calculating double factorials (which will soon be very inaccurate). The reason it is repeating is because of your outer loop. Just use the multiplier to save the value in the array (but change the array to doubles).

    And you may want to investigate the BigInteger class for this problem.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  3. #3
    earthkid123 is offline Member
    Join Date
    Mar 2014
    Posts
    16
    Rep Power
    0

    Default Re: Arrays ?

    we were not yet taught the BigInteger Im confused on where to put the int array I was going to change my double to an int factorial

  4. #4
    earthkid123 is offline Member
    Join Date
    Mar 2014
    Posts
    16
    Rep Power
    0

    Default Re: Arrays ?

    No i need to keep it as a double actually

  5. #5
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,784
    Rep Power
    5

    Default Re: Arrays ?

    Get rid of the outer loop. You don't need it. Save each factorial in the 'a' array indexed by 'multiplier'. I also recommend you size the array properly and the use a.length as the upper bounds in your for loop (instead of a discrete value of 50).

    You can then use another loop below the first one to verify that the values are stored properly by printing them out.

    Edit: and don't forget about 0! which is equal to 1 by definition. So you can just preset the a[0] to 1.

    Regards,
    Jim
    Last edited by jim829; 03-08-2014 at 05:56 PM.
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  6. #6
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,658
    Blog Entries
    7
    Rep Power
    21

    Default Re: Arrays ?

    Forget about numerical overflow for the moment (it'll hit you later and it'll hit hard and mean ;-) If you want to calculate the fifth entry in your array, you compute 1*2*3*4*5, if you want to calculate the sixth entry, you compute 1*2*3*4*5*6 etc. etc. You're doing most of the work you've done already again! A small observation n! == n*(n-1)!, i.e. if you want to compute n! and you have computed (n-1)! already,all you have to do is multiply the previous result by n. So, if you have to compute the n-th entry in your array (n!), all you do is take the previous entry in your array (n-1)! and multiply it by n. Only the zeroth entry doesn't have a previous entry, but you know what it is: 0! == 1. A bit of clever looping does the rest.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  7. #7
    earthkid123 is offline Member
    Join Date
    Mar 2014
    Posts
    16
    Rep Power
    0

    Default Re: Arrays ?

    I now have this but I don't know how to get the algorithm working for factorials

    public static void main(String[] args)
    {
    double [] a= new double [51];

    double factorial = 1.0;
    for (int multiplier=0; multiplier<=50; multiplier++)
    {
    a[multiplier] = factorial * multiplier;
    }
    for(int i=0; i<=50;i++)
    {

    a[i]=a[i]*(i*(i-1));
    a[0]=1;
    System.out.println(a[i]);

    }
    }
    }

  8. #8
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,658
    Blog Entries
    7
    Rep Power
    21

    Default Re: Arrays ?

    Suppose you know that the i-1-th element of your array contains (i-1)!; it is easy then to compute the i-th element:

    Java Code:
    a[i]= i*a[i-1];
    You also know that 0! == 1, so:

    Java Code:
    a[0]= 1;
    Combining those two notions and adding a loop makes this:

    Java Code:
    a[0]= 1;
    for (int i= 1; i <= n; i++)
       a[i]= i*a[i-1];
    This technique is called mathematical induction and it's easy when you get the hang of it.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  9. #9
    earthkid123 is offline Member
    Join Date
    Mar 2014
    Posts
    16
    Rep Power
    0

    Default Re: Arrays ?

    Thank you so much for helping I really appreciate it

Similar Threads

  1. Copying Single Arrays to 2-D Arrays
    By jmscarlet9 in forum New To Java
    Replies: 7
    Last Post: 04-02-2012, 11:17 PM
  2. Casting Enum Type arrays to object type arrays
    By nmvictor in forum Advanced Java
    Replies: 4
    Last Post: 02-17-2012, 12:49 PM
  3. arrays and multidimensional arrays
    By belfast09 in forum New To Java
    Replies: 5
    Last Post: 06-14-2011, 01:28 PM
  4. store array of arrays in array of arrays
    By joost_m in forum New To Java
    Replies: 4
    Last Post: 04-19-2010, 10:32 AM
  5. Arrays.sort... why sorting all arrays in class?
    By innspiron in forum New To Java
    Replies: 6
    Last Post: 03-23-2010, 01:40 AM

Posting Permissions

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