Replacing old values with new values in an array

Page 1 of 1

2 Replies - 594 Views - Last Post: 06 June 2014 - 09:21 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=347506&amp;s=0f4fe2f5d5059d7cbfdcb4add02f1a43&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

#1 proprsk8

Reputation: 2
• Posts: 40
• 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

• Dishonored

Reputation: 1088
• Posts: 4,660
• 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])?

#3 jacobTheMechanic

Reputation: 14
• Posts: 97
• 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.