Help needed on this !!!!

This is a discussion on Help needed on this !!!! within the C Programming forums, part of the General Programming Boards category; I need help on this.can someone help me tell me what the code is talking about and how to tackle ...

  1. #1
    Registered User
    Join Date
    Aug 2011
    Posts
    59

    Question Help needed on this !!!!

    I need help on this.can someone help me tell me what the code is talking about and how to tackle this problem ...the code is suppose to output this .............

    [OUTPUT]

    Sample outputs
    $ a.out
    Enter a number at most equal to 1023: 0
    Cycling through {}
    yields {}.
    $ a.out
    Enter a number at most equal to 1023: 4
    Cycling through {2}
    yields {3}.
    $ a.out
    Enter a number at most equal to 1023: 512
    Cycling through {9}
    yields {0}.
    $ a.out
    Enter a number at most equal to 1023: 5
    Cycling through {0, 2}
    yields {1, 3}.
    $ a.out
    Enter a number at most equal to 1023: 530
    Cycling through {1, 4, 9}
    yields {0, 2, 5}.
    $ a.out
    Enter a number at most equal to 1023: 85
    Cycling through {0, 2, 4, 6}
    yields {1, 3, 5, 7}.
    $ a.out
    Enter a number at most equal to 1023: 408
    Cycling through {3, 4, 7, 8}
    yields {4, 5, 8, 9}.
    $ a.out
    Enter a number at most equal to 1023: 682
    Cycling through {1, 3, 5, 7, 9}
    yields {0, 2, 4, 6, 8}.


    [/OUTPUT]


    This is the code .
    Code:
    #include <stdbool.h>
    #include <stdio.h>
    #include <stdlib.h>
    
    int cycle(int);
    void display_set_coded_by(int);
    
    int main(void) {
        int n;
        printf("Enter a number at most equal to 1023: ");
        scanf("%d", &n);
        printf("Cycling through ");
        display_set_coded_by(n);
        printf("\n         yields ");
        display_set_coded_by(cycle(n));
        puts(".");
        return EXIT_SUCCESS;
    }
    
    void display_set_coded_by(int n) {
        printf("{");
        bool digits_found = false;
        for (int i = 0; i < 10; ++i)
            if (n & 1 << i) {
                printf("%d, ", i);
                digits_found = true;
            }
        if (digits_found)
            printf("\b\b");
        putchar('}');
    }
    
    int cycle(int n) {
        /* ... REPLACE THIS COMMENT WITH YOUR CODE ..

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by code_lover View Post
    I need help on this.can someone help me tell me what the code is talking about and how to tackle this problem ...the code is suppose to output this .............
    Code:
    int cycle(int n) {
        /* ... REPLACE THIS COMMENT WITH YOUR CODE ..
    That part is the code they are talking about.


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

  3. #3
    Registered User
    Join Date
    Aug 2011
    Posts
    59
    Thx for looking at the code,i understand thats the area i need to modify.

    i need help understand this ....

    Code:
    
    void display_set_coded_by(int n) {
        printf("{");
        bool digits_found = false;
        for (int i = 0; i < 10; ++i)
            if (n & 1 << i) {
                printf("%d, ", i);
                digits_found = true;
            }
        if (digits_found)
            printf("\b\b");
        putchar('}');

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Hope is the first step on the road to disappointment.

  5. #5
    Registered User
    Join Date
    Aug 2011
    Posts
    59
    I have look at that !!!.
    i don,t understand how this function
    Code:
      display_set_coded_by(cycle(n))
    is displaying a that yield output.
    please help me explain this in a way i will understand ...
    thx

  6. #6
    Registered User
    Join Date
    May 2009
    Posts
    2,765
    Till you figure out the right thing to do just return n inside cycle.
    Code:
    int cycle(int n) {
       return n;
    }
    Input this series of numbers (just one each time)
    1,2,4,8,16,32,64,128,256,512

    What is the output?

    You should notice a pattern and having read how bit-wise operators work should understand the next step.
    Edit: if you do not see a pattern write down the input numbers in binary form and it should be easier to see the pattern.

    Note: remember "Enter a number at most equal to 1023" means that the largest value allowed returned by cycle is 1023.

    Tim S.
    Last edited by stahta01; 08-07-2011 at 12:37 AM.

  7. #7
    Registered User
    Join Date
    Aug 2011
    Posts
    59
    I have tried reading bit-wise operators but don,t still get it.
    I have written down all these series of number down (1,2,4,8,16,32,64,128,256,512) but i don,t understand much...the only thing i understood with the binary forms of those numbers are their 1s keep on shifting left side 1.
    please help me on this.

  8. #8
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    So are they supposed to be reverse engineering the output to figure out what the function does (in which case it does us absolutely no good to help you), or do you actually have a description of your problem?


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

  9. #9
    Registered User
    Join Date
    Aug 2011
    Posts
    59
    i appreciate your efforts in helping me ...is not that easy and clear because i m very very new to programming and i need to fix this a early as possible to help me continue with my project.

  10. #10
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    So answer my question. Did they give you a description of the project, or are you supposed to work out on your own what the function does given a specific input, based purely on the output you see?


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

  11. #11
    Registered User
    Join Date
    Aug 2011
    Posts
    59
    i was only given that output and the code to modify it.its very confusing to me.

  12. #12
    Registered User
    Join Date
    Jan 2010
    Posts
    412
    Code:
    Enter a number at most equal to 1023: 85
    Cycling through {0, 2, 4, 6}
             yields {1, 3, 5, 7}.
    Look at the number 85 as binary..
    What does it have in common with the first line of output?
    Use that to try and figure out what the second line of output means.
    Use all of the above to figure out how to turn 85 to (that number). Hint: think about what the function name cycle() might mean in this context.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help needed!
    By kdzl in forum C Programming
    Replies: 9
    Last Post: 05-30-2010, 04:59 PM
  2. some help needed.
    By epidemic in forum C++ Programming
    Replies: 16
    Last Post: 01-09-2007, 02:56 PM
  3. Web Bot Needed ( will pay ) Plz Look
    By tribalflames69 in forum Projects and Job Recruitment
    Replies: 1
    Last Post: 05-04-2005, 12:37 PM
  4. Help needed
    By adrian_ang in forum C++ Programming
    Replies: 2
    Last Post: 03-26-2005, 02:49 AM
  5. Help Needed Please!!!!!
    By jon_morrow in forum C++ Programming
    Replies: 7
    Last Post: 03-22-2002, 11:55 AM

Tags for this Thread


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