I need tips on what should i use on this problem
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.
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).The task is ill defined
This seems to work, although it may not be the best way to do it.
(BTW, is anyone else having trouble with "Reply With Quote"? It doesn't usually work for me anymore.)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; }
The whole problem with the world is that fools and fanatics are always so certain of themselves, but wiser people so full of doubts. - Bertrand Russell
To avoid copying of codes , I dont want any replies on this thread .
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):
Last edited by flp1969; 07-17-2021 at 01:39 PM.
The whole problem with the world is that fools and fanatics are always so certain of themselves, but wiser people so full of doubts. - Bertrand Russell
Last edited by flp1969; 07-19-2021 at 02:16 AM.
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 avariable toCode:boolbefore the loop, and then setCode:bool multiply = true;inside the loop to alternate between values. Use that value in an if/else statement to either multiply or divide.Code:multiply = not multiply;
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.
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:
For odd x, you can just remove the leading x term to get: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!
Both are susceptible to overflow when the final answer might not, though, so single loop is probably best.Code:y = x * 2^[(x-1)/2] [(x-1)/2]! / (x-1)!
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:
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.Code:for (int denom = x-2; denom > 1; --denom) while (x-- > 1)
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.