The reason I haven't written void, return and checking for bad characters etc. is that while I know the stuff, now it seemed redundant as I wasn't writing the whole program, just the function, and I guess that was the point of this little exercise Thomas gave me. (and I wouldn't use it in the program because it's not in main and there are a few things around it, I haven't told you the whole homework, it's just the stuff I don't get) I'll try to look at the pow function, see if I can come up with something. :)
Thanks! --- In [email protected], "David Hamill" <[EMAIL PROTECTED]> wrote: > > > > Here's my factorial program: > > int main() > > Should be int main(void), according to the C standards. What > you've written will work with most compilers, but strictly > speaking it's incorrect. > > > { > > int n; > > scanf("%d", &n); > > OK, but you should prompt the user with something like > "Enter a positive integer:" and check to see whether the > user supplies that. Always check your inputs, even if you > *know* they must be right: GIGO. What happens if someone > enters -3, or 1234567890? > > > int j = 1; > > int i; > > > > for(i = 1; i <= n; i++) > > { > > j = i*j; > > } > > OK, but a bit long-winded. How about this: > > int j = 1; > for ( ; n > 1; n--) > j *= n; > > Or maybe: > > int j = 1; > while (n-- > 1) > j *= n; > > This idea of taking an input variable and decrementing it > towards zero is quite common in C. BTW, you don't need to > multiply j by 1, it's a waste of time! > > I'm glad to see you've considered the corner case of 0! => > 1. :-) > > > printf("%d", j); > > } > > Main is supposed to return an int. The conventional value is > zero, signifying nothing has gone wrong. > > return 0; > > > I'm not quite sure how to > > write pow function, I will have to look into that. > > Try something along similar lines to the factorial function. > > David >
