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

  1. #1
    Registered User
    Join Date
    Jul 2021
    Posts
    3

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

    Should i use for loop on this? sorry im a beginner-321321435325325-jpg

  2. #2
    Registered User
    Join Date
    Jul 2021
    Posts
    3
    I need tips on what should i use on this problem

  3. #3
    Registered User MartinR's Avatar
    Join Date
    Dec 2013
    Posts
    200
    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. #4
    Registered User
    Join Date
    Dec 2017
    Posts
    1,136
    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.)
    The best argument against democracy is a five minute conversation with the average voter. - Churchill

  5. #5
    Registered User rstanley's Avatar
    Join Date
    Jun 2014
    Location
    New York, NY
    Posts
    850
    Quote Originally Posted by john.c View Post
    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. #6
    Registered User
    Join Date
    Jul 2021
    Posts
    3
    To avoid copying of codes , I dont want any replies on this thread .

  7. #7
    Registered User
    Join Date
    Feb 2019
    Posts
    882
    You do notice that your function is not well behaved for some values of x, don't you?
    Should i use for loop on this? sorry im a beginner-untitled-png
    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):
    Should i use for loop on this? sorry im a beginner-untitled-png
    Last edited by flp1969; 07-17-2021 at 01:39 PM.

  8. #8
    Registered User
    Join Date
    Dec 2017
    Posts
    1,136
    Quote Originally Posted by kjaerbe View Post
    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.)
    The best argument against democracy is a five minute conversation with the average voter. - Churchill

  9. #9
    Registered User
    Join Date
    Dec 2017
    Posts
    1,136
    Quote Originally Posted by flp1969 View Post
    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.
    The best argument against democracy is a five minute conversation with the average voter. - Churchill

  10. #10
    Registered User
    Join Date
    Feb 2019
    Posts
    882
    Quote Originally Posted by john.c View Post
    x is supposed to be an integer, and presumably positive even though it doesn't specify.
    Ahhhh... of course:
    Should i use for loop on this? sorry im a beginner-untitled-png
    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
    Last edited by flp1969; 07-19-2021 at 02:16 AM.

  11. #11
    Registered User
    Join Date
    Apr 2021
    Posts
    12
    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. #12
    Registered User
    Join Date
    Apr 2021
    Posts
    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. #13
    Registered User
    Join Date
    Apr 2021
    Posts
    45
    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 subscribe to a feed

Similar Threads

  1. while loop coding help - beginner
    By Brennan Zambo in forum C Programming
    Replies: 1
    Last Post: 06-12-2015, 11:32 AM
  2. Beginner. Need help with for loop
    By SkywardTaco in forum C++ Programming
    Replies: 4
    Last Post: 04-27-2015, 10:01 AM
  3. Help with While/Switch loop--beginner
    By Algebro in forum C Programming
    Replies: 3
    Last Post: 09-13-2013, 12:00 PM
  4. Beginner Problem with Loop
    By ToweyLeake in forum C++ Programming
    Replies: 4
    Last Post: 10-11-2011, 10:12 AM
  5. beginner loop help
    By helloalyssa in forum C++ Programming
    Replies: 3
    Last Post: 10-25-2010, 09:08 PM

Tags for this Thread