Results 1 to 9 of 9
Thread: Arrays ?
 03082014, 06:38 PM #1Member
 Join Date
 Mar 2014
 Posts
 16
 Rep Power
 0
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
 03082014, 06:43 PM #2Senior Member
 Join Date
 Jan 2013
 Location
 Northern Virginia, United States
 Posts
 5,154
 Rep Power
 9
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,
JimThe Java^{TM} Tutorials  SSCCE  Java Naming Conventions
Poor planning on your part does not constitute an emergency on my part
 03082014, 06:44 PM #3Member
 Join Date
 Mar 2014
 Posts
 16
 Rep Power
 0
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
 03082014, 06:45 PM #4Member
 Join Date
 Mar 2014
 Posts
 16
 Rep Power
 0
Re: Arrays ?
No i need to keep it as a double actually
 03082014, 06:53 PM #5Senior Member
 Join Date
 Jan 2013
 Location
 Northern Virginia, United States
 Posts
 5,154
 Rep Power
 9
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,
JimLast edited by jim829; 03082014 at 06:56 PM.
The Java^{TM} Tutorials  SSCCE  Java Naming Conventions
Poor planning on your part does not constitute an emergency on my part
 03082014, 07:00 PM #6
 Join Date
 Sep 2008
 Location
 Voorschoten, the Netherlands
 Posts
 14,232
 Blog Entries
 7
 Rep Power
 24
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*(n1)!, i.e. if you want to compute n! and you have computed (n1)! already,all you have to do is multiply the previous result by n. So, if you have to compute the nth entry in your array (n!), all you do is take the previous entry in your array (n1)! 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,
JosThe only person who got everything done by Friday was Robinson Crusoe.
 03082014, 08:52 PM #7Member
 Join Date
 Mar 2014
 Posts
 16
 Rep Power
 0
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*(i1));
a[0]=1;
System.out.println(a[i]);
}
}
}
 03082014, 09:41 PM #8
 Join Date
 Sep 2008
 Location
 Voorschoten, the Netherlands
 Posts
 14,232
 Blog Entries
 7
 Rep Power
 24
Re: Arrays ?
Suppose you know that the i1th element of your array contains (i1)!; it is easy then to compute the ith element:
Java Code:a[i]= i*a[i1];
Java Code:a[0]= 1;
Java Code:a[0]= 1; for (int i= 1; i <= n; i++) a[i]= i*a[i1];
kind regards,
JosThe only person who got everything done by Friday was Robinson Crusoe.
 03082014, 10:29 PM #9Member
 Join Date
 Mar 2014
 Posts
 16
 Rep Power
 0
Similar Threads

Copying Single Arrays to 2D Arrays
By jmscarlet9 in forum New To JavaReplies: 7Last Post: 04022012, 11:17 PM 
Casting Enum Type arrays to object type arrays
By nmvictor in forum Advanced JavaReplies: 4Last Post: 02172012, 01:49 PM 
arrays and multidimensional arrays
By belfast09 in forum New To JavaReplies: 5Last Post: 06142011, 01:28 PM 
store array of arrays in array of arrays
By joost_m in forum New To JavaReplies: 4Last Post: 04192010, 10:32 AM 
Arrays.sort... why sorting all arrays in class?
By innspiron in forum New To JavaReplies: 6Last Post: 03232010, 02:40 AM
Bookmarks