#include <cstdlib>
#include <iostream>
usingnamespace std;
int main()
{
int total;
long sum=1;
int counter=1;
for (int i = 1; i < 16; i++)
{
sum= sum*counter;
counter++;
cout << i << "!Factoral" << "=" << sum << endl;
}
system("PAUSE");
return EXIT_SUCCESS;
}
now the code loops and gives multiplies the 'sum' by the counter, now i cant decide if it is asking me to sum all the values i.e 1! + 2! + 3! = 9 etc or just the sum of the 15th factoral = 1x2x3x4x...15 = 1307674368000 ( acording to the calculator ) but my program spits out
so my question is in two parts (a) would i add up the sum each time it loops and store it in an array ie total = sum[1]+sum[2] +sum[n](1+2+6+24...n) n how would i program that?
(b) why is my program giving a different result for the 15th factoral compared to the trusty calc.exe?
if i change the *counter to *i it will do the same thing so thank you for that :D
and can you explain why = sum[i-1]*counter ? sum[i] = sum[i-1] * counter;
why is it [i-1] when surley you want to +1 to move allong the array? sorry programming isnt my strong point, although it should be having a degree in computer games technology and its a programming heavy course lol
You have to change your cout line. You should now print sum[i], not sum. (Sum is, afaik, a pointer to the first element of the array, i.e. useless as output. If that is chinese to you, ignore it for now).
The sum line is read as this:
"Assign the current sum (sum[i]) the value of the previous sum (sum[i-1]) multiplied by the current counter number (i)". The 'i-1' doesn't mean "move back" or "move ahead"; it means "access the previous number in the array". Thus, if i = 6, sum[i-1] will access the fifth element in the array (i.e. the value of "5!") and calculate the result, then save it in sum[i].
ahh thank you :) makes a bit more sense,n i got why my output was showing the address of the pointer 'duh' silly me.
i have ran it in debug and put a watch on 'sum' and it looks like it works and i changed the 'cout' to 'sum[i]' still the problem is that the array is storing an 'int' and im getting the same values as i was in my first attempt :\
I'm not sure which types are supported, but try one of the bigger types found on the webpage I linked (e.g. long long). One of them might exist. Thing is, factorials increase in value drastically at each "+1" so no matter the variable type, you'll always hit the ceiling of what a variable can store pretty soon.
unsignedlonglong total = 0;
for (int i = 0; i < 16; ++i) { // Or int it = 1 if you want to skip the first
total += sum[i];
}
You can simply combine it with the previous loop as well by adding the total in that loop. Or, if you wish, you can keep the "total" in sum[]: just change the "sum[i] = sum[i-1]*i;" line to "sum[i] += sum[i-1]*i;". You won't have the separates any more though, so it might be worth keeping them and using the separate total.
1!Factoral=1
2!Factoral=2
3!Factoral=6
4!Factoral=24
5!Factoral=120
6!Factoral=720
7!Factoral=5040
8!Factoral=40320
9!Factoral=362880
10!Factoral=3628800
11!Factoral=39916800
12!Factoral=479001600
13!Factoral=6227020800
14!Factoral=87178291200
15!Factoral=1307674368000
total =1
total =3
total =9
total =33
total =153
total =873
total =5913
total =46233
total =409113
total =4037913
total =43954713
total =522956313
total =6749977113
total =93928268313
total =1401602636313