Can't get the loop to work

This is a discussion on Can't get the loop to work within the C Programming forums, part of the General Programming Boards category; Code: /* The Variables: n = the initial depth u = the height I can climb in 1 minute d ...

  1. #1
    Registered User
    Join Date
    Jun 2005
    Location
    Kenora Ontario
    Posts
    6

    I posted my logic, can anyone point me inthe right direction

    Code:
    /* The Variables:
           n = the initial depth
           u = the height I can climb in 1 minute
           d = the number of feet I slip back during rest
           assume d < u and n < 100
           a value of n = 0 indicates end of input
           Each input instance should generate a single line output to 
           indicate the number of minutes it would take to climb out of the shaft.
           Sample input
           10 2 1 
           Sample output 
           17 */
           
           
    #include <stdio.h>
    #include <stdlib.h>
    
    int main()
    {
      
      int u,d,n,c = 0;
      int t,r,m,firsttry = t, firstrest = r, minutes = m , counter = c;
     
      printf("Enter your parameters\n",n,u,d);
      scanf("%d %d %d", &n, &u, &d);
      getchar();
      
      if (n >100 )  // If you enter a number greater then 100
      printf("You are dead dude"); // Your DEAD
      
      for (n != 0; c >= 0; c = c++)// If n is not equal to 0, then loop till it is
        {                          // c is the counter for m 
         m = c ;
         t = n - u; // t is the attempts where depth minus climb = the new heigth
         r = t + d; // r is the distance I slip back while resting 
                    // where the try + the drop equals the new heigth         
                    //IF I put a print statement here we get the correct answer
            {      // on the first try, so there is a problem with the loop
           //printf("%d", r); gives me 99 which is correct for the first drop 
            getchar();
             if  (r <= 0)  // So if r is equal to 0, I am out of the shaft
             printf("It took %3.0f minutes", m); // display the counter m
             else          // OR ELSE
             n = r;        // r is given the value of n for the loop
             getchar();    
             }    
         } 
      return 0;
    }
    The problem i am having is to get the loop and the counter to work,I can't see where the looping problem exists,, should my if statement be changed to a while or a for statement or should i abondan both and use an array?.. If i were to use an array how would i initialize it.

    Too many questons , my head hurts..lol sorry, any direction on this will be welcome.

    Thanks a bunch...
    Last edited by pass_prime; 06-20-2005 at 02:22 PM.

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    First off, stop making new threads for the same exact topic. Second, you never change the value of c, other than to increment it. Your loop is going to take forever, until you overflow the + signed end of your variable, and it wraps over to a negative number.


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Jun 2005
    Location
    Kenora Ontario
    Posts
    6

    C counter

    The c is just a counter for the number of loops.. or when i wrote it in the code did i make it something else?..

    Is it possible that my logic is correct but i didn't use the right statements to make it work?

    Can someone point me int he right direction?
    Thanks

  4. #4
    Work in Progress..... Jaken Veina's Avatar
    Join Date
    Mar 2005
    Location
    Missouri. Go Imos Pizza!
    Posts
    256
    Also, I notice you're using a boolean expression for the initial statement in your loop.
    Code:
      for (n != 0; c >= 0; c = c++)// If n is not equal to 0, then loop till it is
        {                          // c is the counter for m
    You say if c is not equal to 0 in your comment, but its actually if c is greater than or equal to 0. Big difference, and that might be why the loop isn't working the way you want it to. What exactly is your logic behind those three statements? Forget the code, what should it be doing.
    Last edited by Jaken Veina; 06-20-2005 at 11:22 AM.
    Code:
    void function(void)
     {
      function();
     }

  5. #5
    Registered User linuxdude's Avatar
    Join Date
    Mar 2003
    Location
    Louisiana
    Posts
    926
    you can't put a comparison in your for loop. You need to put an
    Code:
    if(n!=0){
       for( ; c >= 0; c++){
         ...
      }
    }
    you also don't need the c=c++ it automatically increments the value of c

  6. #6
    Work in Progress..... Jaken Veina's Avatar
    Join Date
    Mar 2005
    Location
    Missouri. Go Imos Pizza!
    Posts
    256
    Or you could do like so, if that's what you meant to do.
    Code:
    for(; n != 0 && c >= 0; ++c)
    Code:
    void function(void)
     {
      function();
     }

  7. #7
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You still have the problem of "c" looping some 2 BILLION times. On the off chance you've got an old compiler (What am I saying? EVERYONE here uses old compilers.) and have 16bit ints, your loop will still execute some 32000 times...

    Quzah.
    Hope is the first step on the road to disappointment.

  8. #8
    Registered User
    Join Date
    Jun 2005
    Location
    Kenora Ontario
    Posts
    6

    The Logic

    The logic is that a person falls down an elevator shaft N number of feet, climbs up U number of feet/minute, then rests for one minute and slips back down D number of feet.

    So t = N - U which is the fall plus how far you climbed
    r = t + D which is the resultant of t + how far you slid back down

    now r is the new height you are at when you start climbing again. Therefore in reality the value in r is the new N. So N = r for the loop.

    now i used c strickly for a counter of how many loops were performed to get N to equal 0.

    My thinking is that if r <= 0 then display the value in c.
    If its not then N = r and start the loop over till it is.

    My logic seems ok, its how i coded my work, i am not sure i even started it right other then having the equations for t and r which work proper. I need a counter for the number of loops which is displayed as minutes. That is why i made the for loop (n != 0; c >= 0; c = c++) using c as the counter.

    I have really fallen down on this one, and can't seem to see what it is that i require. That is why i posted it here for direction on where to look. If my coding is started wrong for my logic i can accept that, this is all new to me, when i was in high school computers were punch cards.. so any help, or suggestion is really needed and greatly appreciated..

    Thanks..

  9. #9
    Anti-Poster
    Join Date
    Feb 2002
    Posts
    1,399
    <brief derailing>
    Quote Originally Posted by linuxdude
    you also don't need the c=c++ it automatically increments the value of c
    In fact, shouldn't the expression c=c++ cause undefined behavior, since you're modifying c twice between sequence points?
    </brief derailing>
    If I did your homework for you, then you might pass your class without learning how to write a program like this. Then you might graduate and get your degree without learning how to write a program like this. You might become a professional programmer without knowing how to write a program like this. Someday you might work on a project with me without knowing how to write a program like this. Then I would have to do you serious bodily harm. - Jack Klein

  10. #10
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    Code:
    c=c++;
    That code would set c to the value of c, and then increment c. You might as well just put

    Code:
    c++;
    In fact, shouldn't the expression c=c++ cause undefined behavior . . . ?
    No, I don't think so.

  11. #11
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    /* The Variables:
    n = the initial depth
    u = the height I can climb in 1 minute
    d = the number of feet I slip back during rest
    assume d < u and n < 100
    a value of n = 0 indicates end of input
    Each input instance should generate a single line output to
    indicate the number of minutes it would take to climb out of the shaft.
    Sample input
    10 2 1
    Sample output
    17 */
    So, like this?

    Code:
    int main() {
        int n = 10, u = 2, d = 1;
        int x = 0;
    
        for( ; n > 0; x ++) {
            n -= u;
            u += d;
        }
    
        printf("%i", x);
    
        return 0;
    }
    [edit]
    Of course the above code would crash if you have a negitive depth or you slip down more than you climb up;

    Code:
    int main() {
        int n = 10, u = 2, d = 1;
        int x = 0;
    
        if(n <= 0) printf("You're already out.");
        if(u <= d) printf("You'll never get out.");
        else {
            for( ; n > 0; x ++) {
                n -= u;
                u += d;
            }
    
            printf("It will take you %i minutes to escape.", x);
        }
    
        return 0;
    }
    Last edited by dwks; 06-20-2005 at 03:49 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. loop the loop - feeling foolish
    By estos in forum C Programming
    Replies: 2
    Last Post: 04-07-2007, 02:45 AM
  2. Replies: 1
    Last Post: 10-27-2006, 01:21 PM
  3. Replies: 26
    Last Post: 06-15-2005, 02:38 PM
  4. The Bludstayne Open Works License
    By frenchfry164 in forum A Brief History of Cprogramming.com
    Replies: 8
    Last Post: 11-26-2003, 10:05 AM
  5. fopen();
    By GanglyLamb in forum C Programming
    Replies: 8
    Last Post: 11-03-2002, 11:39 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21