Thread: Looping and generally awkward scenarios

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User
    Join Date
    Nov 2013
    Posts
    4

    Looping and generally awkward scenarios

    Hey guys, I have an exam in C programming I'm a little stuck on loops...

    I have to make a program that multiplies the loop variable by 2 then takes away 1... easy, that was a for loop no problem

    for (x=3; x<=65; x=x*2-1)

    that should print out 3 5 9 17 33 65

    however, my lecturer has given the task for it to reverse, but not to display the number 17 on the way back down using an if statement...
    however, this time it must be a do while loop... This is what I have so far.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    
    int main()
    {
        int x = 129;
    
    
        do
        {
            x = (x/2) + 1;
            
            printf("%d \n", x);
    
    
        }
        while (x >= 4);
    
    
        return 0;
    }

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    I'm not sure what you want to know. Put an if statement inside your loop to determine whether or not you actually do the print statement, and you appear to be done.

  3. #3
    Registered User
    Join Date
    Nov 2013
    Posts
    4
    I should of been more specific, how do I tell the program not to display the number 17?

    I did try if(x=17){printf(" ");
    however that just made the program continually loop 17, when I changed = to == it just put a space in front of the 17. I've only started programming a month ago.

  4. #4
    Registered User MacNilly's Avatar
    Join Date
    Oct 2005
    Location
    CA, USA
    Posts
    466
    Maybe you should have a look at the continue statement...

  5. #5
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    So you only want to print when x isn't 17....................

  6. #6
    Registered User
    Join Date
    Nov 2013
    Posts
    4
    No I want it to print 65, 33, 9, 5, 3... Heres the scenario

    · Demonstrates a ‘for’ loop that produces the output: 3, 5, 9, 17, 33, 65. (Hint: the loop variable value is first doubled, then reduced by 1)
    · Demonstrates a ‘do’ – ‘while’ loop that does exactly the same, just the other way round (Hint: first add 1 to the loop variable, then divide by 2). However, the 17 must not be printed out, i.e. the output is: 65, 33, 9, 5, 3.

  7. #7
    Registered User
    Join Date
    Nov 2013
    Posts
    4
    I've managed to work it out now
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    
    int main()
    {
        int x = 129;
    
    
        do
        {
            x = (x/2) + 1;
            //if statement to find the number you want to play with
            if(x == 17){
                //skips the number you've chosen and continues to run the loop.WHITELOOPSppt page 32    
                continue;
            }
    
    
            printf("%d \n", x);
    
    
        }
        while (x >= 4);
    
    
        return 0;
    }
    suddenly clicked >.<

  8. #8
    misoturbutc Hodor's Avatar
    Join Date
    Nov 2013
    Posts
    1,787
    Great work coming up with a solution, well done! There's nothing wrong with your solution, so keep this in mind when you read my comments below.

    The continue statement is a very useful and often used feature of the C language. Amongst other things it can make code easier to read and write. It falls under the category of statement called "jump statements" (goto, continue, break and return). You've demonstrated that you know how it works so I won't continue (haha) explaining it further.

    Generally I think that code should be as clear as possible and continue often helps achieve this. I don't think, however, that your use of it is clearly the most obvious and clear way to implement your loop (actually, the condition not to print the number 17). As I mentioned it's not incorrect but I personally don't like it for this scenario. I probably would have used tabstop's suggestion (see code below). To me, that's the most obvious way to implement your while loop; i.e. I think the use of continue in this exercise is "pointless".

    That said, you've learned a new statement and programming method so it was a useful exercise!

    Cheers

    Code:
    #include <stdio.h>
    #include <stdlib.h>
     
     
    int main(void)
    {
        int x = 129;
     
        do
        {
            x = (x/2) + 1;
            
            if(x != 17) {   /* Only print numbers that are not 17 */
                printf("%d \n", x);
            }
        }
        while (x >= 4);
         
        return 0;
    }
    Edit: I also would have used a while loop rather than a do/while loop but I see you were asked to use do while, so not much can be done about that
    Last edited by Hodor; 11-15-2013 at 07:18 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Generally Declaring Array question
    By cougarsmustangs in forum C Programming
    Replies: 1
    Last Post: 03-28-2013, 06:53 PM
  2. qprint.h or printing generally
    By r_james14 in forum C++ Programming
    Replies: 1
    Last Post: 07-10-2012, 02:12 PM
  3. Awkward problem in learning c++
    By TheUnknownFacto in forum C++ Programming
    Replies: 6
    Last Post: 05-17-2007, 01:43 PM
  4. randomized version generally doesn't work
    By talz13 in forum C++ Programming
    Replies: 7
    Last Post: 11-08-2003, 12:39 PM
  5. scenarios for adventure game
    By hostensteffa in forum Game Programming
    Replies: 13
    Last Post: 05-29-2002, 07:58 AM