Friday, July 31, 2009

Need help with this C++ Program?

//table of factorials


#include %26lt;cmath%26gt;


#include %26lt;iostream%26gt;


using namespace std;


int main () {


int n;


cout %26lt;%26lt; "Enter a number: "%26lt;%26lt;endl;


cin %26gt;%26gt; n;


int factorial;


{


return n == 0 ? 1 : n * factorial(n-1);


}


return 0;


}

Need help with this C++ Program?
One way os to recast what you have as a function instead of a main, then call it in a loop, printing out the number and the result of the factorial function each pass through the loop.





A more sophisticated way would be to write a new main consisting of a loop, printing, on each pass through the loop, the number and its factorial (hint: factorial( n ) == factorial( n-1 ) * n).





Good luck. You'll get there.
Reply:just change this main funtion into user functiona and call this function each time from main
Reply:A table? not just a row or column? You could use a loop of some sort, either a 'while' or a 'for', your choice. If you want to list the factorials in ascending order as you build them, have a separate variable that you use to count up to your input number, otherwise, you can use your input number and count down. You can also use the recursion you have here and just have to decide what order to print in. Instead of doing your return on the same line as your calculation, you could do a print after your calculation just before returning, this would print your numbers in ascending order.
Reply:write a new main consisting of a loop, printing, on each pass through the loop, the number and its factorial (hint: factorial( n ) == factorial( n-1 ) * n).
Reply:You should study C++ more because I see a lot of mistakes and poor understanding.





* factorial is not initialized if that was supposed to be a variable.


* you can not have functions inside functions.


* You're returning control and the value to the OS.


* Where are you going to output your results?


* factorial is a variable, but it is being used like a function.


No comments:

Post a Comment