2 Replies - 427 Views - Last Post: 06 June 2014 - 09:21 PM Rate Topic: -----

#1 proprsk8  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 39
  • Joined: 10-November 13

Replacing old values with new values in an array

Posted 24 May 2014 - 09:28 AM

Hi, I need help trying to figure out how to replace the values in my array with the results of my function factorial.
public static void main(String[] args) {
//this is my main function:
  int[] array = {5,4,3,2,1};
  int i = 0;
  System.out.print("results: ");
  
  for (i = 0; i < array.length; i++){
     
    System.out.print(factorial(array[i]));

  }
}

//Factorial function
	public static int fact(int i){
		if (i == 0){
			return 1;
		}
		else{
			return i*factorial(i-1);
		}
	}



So, what I'm trying to do is change the contents of the array "array" into their factorial value. So, they should be replaced with {120,24,6,2,1}. then add those using linear sum but that's a different story.

This post has been edited by proprsk8: 24 May 2014 - 09:30 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Replacing old values with new values in an array

#2 astonecipher  Icon User is offline

  • Major DIC Head
  • member icon

Reputation: 680
  • View blog
  • Posts: 2,982
  • Joined: 03-December 12

Re: Replacing old values with new values in an array

Posted 24 May 2014 - 09:50 AM

Something like array[i] = fact(array[i])?
Was This Post Helpful? 1
  • +
  • -

#3 jacobTheMechanic  Icon User is offline

  • D.I.C Head

Reputation: 14
  • View blog
  • Posts: 89
  • Joined: 18-September 12

Re: Replacing old values with new values in an array

Posted 06 June 2014 - 09:21 PM

replace this:

 
    System.out.print(factorial(array[i]));



with this:
 
    System.out.print(fact(array[i]));



replace this:
return i*factorial(i-1);



with this:

return i*fact(i-1);



pretty sure this will work.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1