Connecting Tech Pros Worldwide Help | Site Map

(part 4) Han from China answers your C questions,

George Orwell
Guest
 
Posts: n/a
#1: Oct 30 '08
Why is it wrong??(About n!)
Quote:
upyzl said:
I want to calculate n!,and my C code is as follows:
This is off-topic for this newsgroup. ISO C makes
no assurances that your target processor (if there is
a processor) can calculate n! in a reasonable length
of time. It's best not to make any assumptions.

That said, I'll try my best to help you out.

<snip>
Quote:
system("pause");
Not every system has a 'pause' command. To increase the
portability of your program /somewhat/, perhaps add
pause() after your system() call so that on systems
with the typical pause() function, you can send your program
a signal to resume. Actually, you may want to investigate
possible flow-control mechanisms.

Now, it's probable that someone has designed a far more
efficient factorial function than your one, and perhaps
they've even supplied you with a 'factorial' command. Since
using such a 'factorial' command directly from the command
line would deprive you of valuable programming experience,
I recommend building up a little command string and then
calling system(). Something like this:

snprintf(command, sizeof command, "factorial %lu", n);
system(command);

That way, you get the benefit of the efficient factorial
function, which may have even been implemented with a built-in
gamma function, a great bonus if ever you want to extend
your code out of N.

Yours,
Han from China

Il mittente di questo messaggio|The sender address of this
non corrisponde ad un utente |message is not related to a real
reale ma all'indirizzo fittizio|person but to a fake address of an
di un sistema anonimizzatore |anonymous system
Per maggiori informazioni |For more info
https://www.mixmaster.it

Kelsey Bjarnason
Guest
 
Posts: n/a
#2: Oct 31 '08

re: (part 4) Han from China answers your C questions,


[snips]

On Thu, 30 Oct 2008 15:46:04 +0100, George Orwell wrote:
Quote:
Why is it wrong??(About n!)
Quote:
>upyzl said:
>I want to calculate n!,and my C code is as follows:
>
This is off-topic for this newsgroup. ISO C makes no assurances that
your target processor (if there is a processor) can calculate n! in a
reasonable length of time. It's best not to make any assumptions.
ISO C makes no assurances that the simplest "Hello, world" program will
ever actually produce an output and terminate; the system's I/O buffers
could wind up in a locked state and never allow the output.

If the lack of performance guarantees is a reason not to calculate n!, it
is also reason not to do *any* coding in C. Nor C++, Perl, Haskell or
any other language.
Quote:
Quote:
>system("pause");
>
Not every system has a 'pause' command. To increase the portability of
your program /somewhat/, perhaps add pause() after your system() call so
that on systems with the typical pause() function, you can send your
program a signal to resume. Actually, you may want to investigate
possible flow-control mechanisms.
Why not simply use puts( "Press enter to continue" ); getchar(); ?
Quote:
experience, I recommend building up a little command string and then
calling system(). Something like this:
>
snprintf(command, sizeof command, "factorial %lu", n);
system(command);
What this has to do with C, however...

Closed Thread