c# - Factorial(s) of N numbers in for loop -


i working on problem codechef need calculate factorial of n numbers.

the user inputs number determines how many ints perform factorial calculation on , inputs numbers calculate.

my problem multiplication itself. example if have int == 5 result 20 (it calculate n last factorial only, not of them)

here problem exists:

for(int x = 0; x < _numberstoprocess.length; x++) {// loop throuigh array index     (int y = 1; y < _numberstoprocess[x]; y++) {// y equal less index x          _result[x] = _numberstoprocess[x] * y;// multiply x y add array     } } 

the outer loop defines how many calculations perform.

the inner loop calulates factorials iterating through each index of _numbertoprocess , multiplying every number less number calculated on.

the problem factorial calculation overwrites itself,

for example:

factorial of 5 result: 20 should 120 (it overwrites until reaches last multiplier)

so tried following:

_result[x] = _numberstoprocess[x] *= y; 

this same _numberstoprocess[x] = _numberstoprocess[x] * y;

but gives completley different result:

if again input 5 result in output of -1899959296.

i know can copy , paste other submissions want know why method not result in correct output.

here method in entirety:

int _numbers = int.parse(console.readline());// number of ints calculate         int[] _numberstoprocess = new int[_numbers];// array of inputs         int[] _result = new int[_numbers];         int = 0;          while(i < _numberstoprocess.length) {             _numberstoprocess[i] = int.parse(console.readline());             i++;         }          for(int x = 0; x < _numberstoprocess.length; x++) {// loop throuigh array index             (int y = 1; y < _numberstoprocess[x]; y++) {// y equal less index x                 _result[x] = _numberstoprocess[x] *= y;// multiply x y add array             }         }          (int n = 0; n < _result.length; n++) {// y equal less index x             console.writeline(_result[n]);// write console         }          console.readline(); 

int _numbers = int.parse(console.readline());// number of ints calculate     int[] _numberstoprocess = new int[_numbers];// array of inputs     int[] _result = new int[_numbers];     int = 0;      while(i < _numberstoprocess.length) {         _numberstoprocess[i] = int.parse(console.readline());         i++;     }      (int x = 0; x < _numberstoprocess.length; x++)         {// loop throuigh array index             int fact = 1;             (int y = 1; y <= _numberstoprocess[x]; y++)             {// y equal less index x                 fact = fact*y;             }             _result[x] = fact;         }       (int n = 0; n < _result.length; n++) {// y equal less index x         console.writeline(_result[n]);// write console     }      console.readline(); 

problem inner loop. here, always, overriding result array.

i.e y=5; inner loop executes 5 times.

iteration -1 :    y=1,   _numberstoprocess[5]=5   _result[x]=5    iteration -2 :    y=2,   _numberstoprocess[5]=10   _result[x]=10  iteration -3 :    y=3,   _numberstoprocess[5]=30   _result[x]=30  . . . . . 

thus goes 12 iteration _numbertoprocess[5] changing , stops once reaches less 0 i.e -1899959296.

iteration 12:   _numbertoprocess[5] = -1899959296. 

i.e you changing numbertoprocess everytime in inner loop.

you can verify adding

console.writeline(y); console.writeline(_numberstoprocess[x]); console.writeline(_result[x]); 

in inner loop.


Comments