Thread: Should i use for loop on this? sorry im a beginner

1. Should i use for loop on this? sorry im a beginner  2. I need tips on what should i use on this problem 3. The task is ill defined but it looks like they want you to go from whatever x down to 0. So yes you would need a loop. 4. The task is ill defined
It seems well-defined to me, although (x - (x - 1)) is a strange way to write 1. And the answer for x=5 is not 13.333334 but 13.3333333... (so no possibility of rounding up to a 4, really).

This seems to work, although it may not be the best way to do it.
Code:
#include <stdio.h>

int main() {
int x = 0;
printf("x: ");
scanf("%d", &x);

double y = x;
while (--x > 1) {
y *= x;
if (--x > 1) y /= x;
}

printf("%f\n", y);

return 0;
}
(BTW, is anyone else having trouble with "Reply With Quote"? It doesn't usually work for me anymore.) 5. Originally Posted by john.c It seems well-defined to me, although (x - (x - 1)) is a strange way to write 1. And the answer for x=5 is not 13.333334 but 13.3333333... (so no possibility of rounding up to a 4, really).

This seems to work, although it may not be the best way to do it.
Code:
#include <stdio.h>

int main() {
int x = 0;
printf("x: ");
scanf("%d", &x);

double y = x;
while (--x > 1) {
y *= x;
if (--x > 1) y /= x;
}

printf("%f\n", y);

return 0;
}
(BTW, is anyone else having trouble with "Reply With Quote"? It doesn't usually work for me anymore.)
Seems to work for me. 6. To avoid copying of codes , I dont want any replies on this thread . 7. You do notice that your function is not well behaved for some values of x, don't you? And it's worse: The denominator isn't continuous for a lot of values (if considering x integer, it is zero for a lot of values of x):  8. Originally Posted by kjaerbe To avoid copying of codes , I dont want any replies on this thread .
What are you babbling about? Don't tell people what they can and can't do. You're just a guest here.

(BTW, the "Reply With Quote" button worked this time, but it sometimes doesn't.) 9. Originally Posted by flp1969 You do notice that your function is not well behaved for some values of x, don't you?
x is supposed to be an integer, and presumably positive even though it doesn't specify. 10. Originally Posted by john.c x is supposed to be an integer, and presumably positive even though it doesn't specify.
Ahhhh... of course: I just don't know if double precision is sufficient to hold factorials of "big" numbers (like INT_MAX-2).

Testing here: 171 will cause overflow! 1755! overflows for long double.
170! =(approx) 7.257416e+306 11. Yes, use a loop. What else could you use?

You have a variable number of factors to multiply or divide in that expression, with no fixed upper limit on the number of (x-k) factors, and if/else and switch statements will only selectively skip over statements. To execute a statement multiple times, you need something else.

The first "something else" you'll learn about in a C class is a loop. Another "something else" idea you'll learn about later is recursion, and that could also be used...but usually that's not introduced until you've covered the basics of writing functions. There are other ideas as well (including the overly-maligned "goto" statement), but right now a loop is probably the only choice you've been taught about.

Before beginning to code, though, I think it's just as important to examine the in a computation. You have a sequence of factors (leaving out the operations) x, x-1, x-2, x-3 ... x-(x-1). Writing the first factor as (x-0), what's being subtracted each time are values from the sequence 0, 1, 2, ... x-1. That means that x-1 must be in that sequence someplace or else the computation never stops. So, x is a positive integer. The final factor is x-(x-1) = (x-x)+1 = 1, so every computation will look something like your example; a sequence of positive integers from x down to 1 being alternately multiplied and divided.

You can use a two-value variable to decide in the loop. If you've included <stdbool.h> at the top of your program, you can set a
Code:
bool
variable to
Code:
bool multiply = true;
before the loop, and then set
Code:
multiply = not multiply;
inside the loop to alternate between values. Use that value in an if/else statement to either multiply or divide.

In old-school C, an integer works just as well. Use "int multiply=1;" to set the value and "multiply = 1-multiply;" to toggle between values 1 and 0. 12. The pattern being repeated there is alternation of * and / throughout the computation (read the note), so that expression isn't quite it. If x is an even integer, then the result is:
Code:
y  =  x*(x-1)/(x-2)*(x-3)/ ... *3/2*1
=  [x(x-2)(x-4)...(2)] / [(x-1)(x-3)(x-5)...(1)]
=  [x(x-2)(x-4)...(2)]^2 / x!
=  2^(x/2) * (x/2)! / x!
For odd x, you can just remove the leading x term to get:
Code:
y   =  x * 2^[(x-1)/2] [(x-1)/2]! / (x-1)!
Both are susceptible to overflow when the final answer might not, though, so single loop is probably best. 13. You may choose to use either a for loop or a while loop on this problem.

Reasons to use a for loop include:

* it's a monotonic integer sequence
* it has a known starting and ending condition
* it may involve initialization of a range variable

Reasons to use a while loop include:

* the range variable may be initialized already
* the increment/update step may be merged with the ending condition

Consider the following:

Code:
for (int denom = x-2; denom > 1; --denom)

while (x-- > 1)
Depending on how you wrote the code leading up to the loop, either one of those loops might be the solution you want. It depends on your coding approach.

If you are designing your function around the loop, I would recommend you choose the for loop. It's a bit more clear and makes it obvious to both you and any teacher (or helper on the interwebs) what you intend to accomplish. Popular pages Recent additions beginner, loop 