P: n/a

again, i am looking for some good advice
:)
 PROGRAMME 
/* Steve Summit's C programming
assignment 3, exercise 5
STATEMENT:
Write a program to print the first 7 positive integers and their
factorials.
(The factorial of 1 is 1, the factorial of 2 is 1 * 2 = 2, the
factorial of 3
is 1 * 2 * 3 = 6, the factorial of 4 is 1 * 2 * 3 * 4 = 24, etc.)
[Extra credit: why did I only ask for the first 7?]
*/
#include <stdio.h>
int facto(int i);
int main()
{
int i;
for(i = 1; i <= 7; ++i)
printf("factorial of %d is: %d\n", i, facto(i));
return 0;
}
int facto(int i)
{
int fact_i;
fact_i = 1;
while (i 0)
{
fact_i *= i;
i;
}
return fact_i;
}
/* [Extra credit: why did I only ask for the first 7?]
because, the guarnteed minimum size of an interger, which is 16 bits,
can hold values upto only 32,767
and factorial(8) crosses that limit
*/
 OUTPUT 
[arch@voodo stevesummit]$ gcc std=c99 pedantic Wall Wextra
assign3_ex5.c
[arch@voodo stevesummit]$ ./a.out
factorial of 1 is: 1
factorial of 2 is: 2
factorial of 3 is: 6
factorial of 4 is: 24
factorial of 5 is: 120
factorial of 6 is: 720
factorial of 7 is: 5040
[arch@voodo stevesummit]$  
Share this Question
P: n/a

/* Steve Summit's C programming
assignment 3, exercise 5
STATEMENT:
Write a program to print the first 7 positive integers and their
factorials.
(The factorial of 1 is 1, the factorial of 2 is 1 * 2 = 2, the
factorial of 3
is 1 * 2 * 3 = 6, the factorial of 4 is 1 * 2 * 3 * 4 = 24, etc.)
[Extra credit: why did I only ask for the first 7?]
Answer: Because you knew that most students would program with
integers.
Also, because it was written long ago, many compilers had 16
bit
integers as default size for int. Hence, to prevent the
students
from freaking out, you made it simple for them.
*/
#include <stdlib.h>
#include <assert.h>
static const double Factorials[] = {
1.0,
1.0,
2.0,
6.0,
2.4e+001,
1.2e+002,
7.2e+002,
5.04e+003,
4.032e+004,
3.6288e+005,
3.6288e+006,
3.99168e+007,
4.790016e+008,
6.2270208e+009,
8.71782912e+010,
1.307674368e+012,
2.0922789888e+013,
3.55687428096e+014,
6.402373705728e+015,
1.21645100408832e+017,
2.43290200817664e+018,
5.109094217170944e+019,
1.1240007277776077e+021,
2.5852016738884978e+022,
6.2044840173323941e+023,
1.5511210043330986e+025,
4.0329146112660565e+026,
1.0888869450418352e+028,
3.0488834461171384e+029,
8.8417619937397008e+030,
2.6525285981219103e+032,
8.2228386541779224e+033,
2.6313083693369352e+035,
8.6833176188118859e+036,
2.9523279903960412e+038,
1.0333147966386144e+040,
3.7199332678990118e+041,
1.3763753091226343e+043,
5.2302261746660104e+044,
2.0397882081197442e+046,
8.1591528324789768e+047,
3.3452526613163803e+049,
1.4050061177528798e+051,
6.0415263063373834e+052,
2.6582715747884485e+054,
1.1962222086548019e+056,
5.5026221598120885e+057,
2.5862324151116818e+059,
1.2413915592536073e+061,
6.0828186403426752e+062,
3.0414093201713376e+064,
1.5511187532873822e+066,
8.0658175170943877e+067,
4.2748832840600255e+069,
2.3084369733924138e+071,
1.2696403353658276e+073,
7.1099858780486348e+074,
4.0526919504877221e+076,
2.3505613312828789e+078,
1.3868311854568986e+080,
8.3209871127413916e+081,
5.0758021387722484e+083,
3.1469973260387939e+085,
1.9826083154044401e+087,
1.2688693218588417e+089,
8.2476505920824715e+090,
5.4434493907744307e+092,
3.6471110918188683e+094,
2.4800355424368305e+096,
1.711224524281413e+098,
1.197857166996989e+100,
8.5047858856786218e+101,
6.1234458376886077e+103,
4.4701154615126834e+105,
3.3078854415193856e+107,
2.4809140811395391e+109,
1.8854947016660498e+111,
1.4518309202828584e+113,
1.1324281178206295e+115,
8.9461821307829729e+116,
7.1569457046263779e+118,
5.7971260207473655e+120,
4.7536433370128398e+122,
3.9455239697206569e+124,
3.314240134565352e+126,
2.8171041143805494e+128,
2.4227095383672724e+130,
2.1077572983795269e+132,
1.8548264225739836e+134,
1.6507955160908452e+136,
1.4857159644817607e+138,
1.3520015276784023e+140,
1.24384140546413e+142,
1.1567725070816409e+144,
1.0873661566567424e+146,
1.0329978488239052e+148,
9.916779348709491e+149,
9.6192759682482062e+151,
9.426890448883242e+153,
9.3326215443944096e+155,
9.3326215443944102e+157,
9.4259477598383536e+159,
9.6144667150351211e+161,
9.9029007164861754e+163,
1.0299016745145622e+166,
1.0813967582402903e+168,
1.1462805637347078e+170,
1.2265202031961373e+172,
1.3246418194518284e+174,
1.4438595832024928e+176,
1.5882455415227421e+178,
1.7629525510902437e+180,
1.9745068572210728e+182,
2.2311927486598123e+184,
2.5435597334721862e+186,
2.9250936934930141e+188,
3.3931086844518965e+190,
3.969937160808719e+192,
4.6845258497542883e+194,
5.5745857612076033e+196,
6.6895029134491239e+198,
8.09429852527344e+200,
9.8750442008335976e+202,
1.2146304367025325e+205,
1.5061417415111404e+207,
1.8826771768889254e+209,
2.3721732428800459e+211,
3.0126600184576582e+213,
3.8562048236258025e+215,
4.9745042224772855e+217,
6.4668554892204716e+219,
8.4715806908788174e+221,
1.1182486511960039e+224,
1.4872707060906852e+226,
1.9929427461615181e+228,
2.6904727073180495e+230,
3.6590428819525472e+232,
5.0128887482749898e+234,
6.9177864726194859e+236,
9.6157231969410859e+238,
1.346201247571752e+241,
1.8981437590761701e+243,
2.6953641378881614e+245,
3.8543707171800706e+247,
5.5502938327393013e+249,
8.0479260574719866e+251,
1.1749972043909099e+254,
1.7272458904546376e+256,
2.5563239178728637e+258,
3.8089226376305671e+260,
5.7133839564458505e+262,
8.6272097742332346e+264,
1.3113358856834518e+267,
2.0063439050956811e+269,
3.0897696138473489e+271,
4.7891429014633912e+273,
7.4710629262828905e+275,
1.1729568794264138e+278,
1.8532718694937338e+280,
2.9467022724950369e+282,
4.714723635992059e+284,
7.5907050539472148e+286,
1.2296942187394488e+289,
2.0044015765453015e+291,
3.2872185855342945e+293,
5.423910666131586e+295,
9.0036917057784329e+297,
1.5036165148649983e+300,
2.5260757449731969e+302,
4.2690680090047027e+304,
7.257415615307994e+306
};
double factorial(unsigned n)
{
double f = 1;
static const size_t fact_arr_dim = sizeof Factorials / sizeof
Factorials[0];
assert(n < fact_arr_dim);
if (n < fact_arr_dim) {
f = Factorials[n];
}
return f;
}
#ifdef UNIT_TEST
#include <stdio.h>
int main(void)
{
unsigned index;
for (index = 1; index < 8; index++) {
printf("%u! = %.0f\n", index, factorial(index));
}
return EXIT_SUCCESS;
}
#endif
/*
dcorbit@DCORBIT64 /c/tmp
$ gcc std=c99 Wall pedantic Wextra DUNIT_TEST fac.c
dcorbit@DCORBIT64 /c/tmp
$ ./a
1! = 1
2! = 2
3! = 6
4! = 24
5! = 120
6! = 720
7! = 5040
dcorbit@DCORBIT64 /c/tmp
*/  
P: n/a

arnuld wrote:
again, i am looking for some good advice
:)
 PROGRAMME 
/* Steve Summit's C programming
assignment 3, exercise 5
STATEMENT:
Write a program to print the first 7 positive integers and their
factorials.
(The factorial of 1 is 1, the factorial of 2 is 1 * 2 = 2, the
factorial of 3
is 1 * 2 * 3 = 6, the factorial of 4 is 1 * 2 * 3 * 4 = 24, etc.)
[Extra credit: why did I only ask for the first 7?]
*/
#include <stdio.h>
int facto(int i);
int main()
{
int i;
for(i = 1; i <= 7; ++i)
printf("factorial of %d is: %d\n", i, facto(i));
return 0;
}
OK, it's very readable. But you don't take advantage that when i=3,
you've already calculated most of the result when you calculated i=2.
>
int facto(int i)
{
int fact_i;
fact_i = 1;
while (i 0)
{
fact_i *= i;
i;
}
return fact_i;
}
int i, n;
for (i=1, n=1; i<=7; i++) {
n *= i;
printf("factorial of %d is: %d\n", i, n);
}
>
/* [Extra credit: why did I only ask for the first 7?]
because, the guarnteed minimum size of an interger, which is 16 bits,
can hold values upto only 32,767
and factorial(8) crosses that limit
*/
 OUTPUT 
[arch@voodo stevesummit]$ gcc std=c99 pedantic Wall Wextra
assign3_ex5.c
[arch@voodo stevesummit]$ ./a.out
factorial of 1 is: 1
factorial of 2 is: 2
factorial of 3 is: 6
factorial of 4 is: 24
factorial of 5 is: 120
factorial of 6 is: 720
factorial of 7 is: 5040
[arch@voodo stevesummit]$  
P: n/a

arnuld wrote:
again, i am looking for some good advice
STATEMENT:
Write a program to print the first 7 positive integers and their
factorials.
(The factorial of 1 is 1, the factorial of 2 is 1 * 2 = 2, the
factorial of 3
is 1 * 2 * 3 = 6, the factorial of 4 is 1 * 2 * 3 * 4 = 24, etc.)
[Extra credit: why did I only ask for the first 7?]
*/
#include <stdio.h>
int facto(int i);
int main()
{
int i;
for(i = 1; i <= 7; ++i)
printf("factorial of %d is: %d\n", i, facto(i));
return 0;
}
int facto(int i)
{
int fact_i;
fact_i = 1;
while (i 0)
{
fact_i *= i;
i;
}
return fact_i;
}
The code is, IMO, well written, since it is easily readable and works
correctly.
My stylistic suggestions (no right way here) are
1. Use a more descriptive name for the function. This makes it easier
to understand. I suggest factorial for your example.
2. Within a short function (about 30 lines of body or less), such as
facto, I typically use short names for local variables, in this case f
for fact_i. If the variable were global, or the function long, I would
use a more descriptive name.
3. While I may have use more comment lines, I tend to otherwise use less
vertical space in my code, so that I can see more in a glance or within
my editor window. That needs to be balanced with readability. My own
style would be
/* Function: factorial
** Description: Return factorial of specified integer.
*/
int /* factorial of i */
factorial (
int i /* argument, 1..7 */
) {
int f = 1; /* interim product */
while (i 0) {
f *= i;
i;
}
return f;
}
I might squeeze more and write
while (i 0) f *= i;
but that can admittedly be harder to read.
I use a comment block preceding each function. Each declaration has a
comment. The parameters are commented within the declaration. The
comments include units (such as milliseconds) and limits. I use K&R
bracing to save vertical space, which many people dislike.
Your style will probably change as you gain more experience. Use what
works to support your personal needs and those of your employer/client,
as appropriate.

Thad  
P: n/a

On Mar 17, 6:20 pm, Jason Curl <j.m.c...@gmx.dewrote:
OK, it's very readable.
thanks
But you don't take advantage that when i=3,
you've already calculated most of the result when you calculated i=2.
int i, n;
for (i=1, n=1; i<=7; i++) {
n *= i;
printf("factorial of %d is: %d\n", i, n);
}
that looks better than my version. i will use this
:)   This discussion thread is closed Replies have been disabled for this discussion.   Question stats  viewed: 1915
 replies: 4
 date asked: Mar 17 '07
