Thread: Need help on functions

  1. #1
    Registered User
    Join Date
    Apr 2003
    Posts
    82

    Need help on functions

    I need to write functions, you guys know any websites that can give me some ideas of what to do? Or any help will do

    A function named B2U. There is one argument, a char* which is a C-style string containing 0's and 1's representing a bit vector. Assume the longest bit vector contains 32 bits. For example, the bit vector might be "11010101". The value returned by the function is an unsigned long int whose value corresponds to the bit vector.

    A function named B2S. There is one argument, a char* which is a C-style string containing 0's and 1's representing a bit vector. Assume the longest bit vector contains 32 bits. For example, the bit vector might be "11010101". The value returned by the function is a signed long int whose value corresponds to the bit vector.

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Here is a start. You may want to check out the FAQ.
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    unsigned long int B2U(char *bits)
    {
       return strtoul(bits, NULL, 2);
    }
    
    signed long int B2S(char *bits)
    {
       return strtol(bits, NULL, 2);
    }
    
    int main(void)
    {
       char v[] = "11010101";
       printf("B2U(\"%s\") = %lu\n", v, B2U(v));
       printf("B2S(\"%s\") = %ld\n", v, B2S(v));
       return 0;
    }
    
    /* my output
    B2U("11010101") = 213
    B2S("11010101") = 213
    */
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  3. #3
    Registered User
    Join Date
    Apr 2003
    Posts
    82
    oh yea..functions should be implemented using only arithmetic or logical operations. You should not use any library functions (e.g. sprintf, atoi, etc.) which is harder..=*(

  4. #4
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    This is not a homework service.
    http://cboard.cprogramming.com/annou...p?s=&forumid=4

    Now, it's your turn to post an attempt.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  5. #5
    Registered User
    Join Date
    Apr 2003
    Posts
    82
    As I said in the first place "I need to write functions, you guys know any websites that can give me some ideas of what to do? Or any help will do" I didn't ask you guys to do it, I just want some info..like examples or websites on how to attempt this problem..since my teacher gave us none.

  6. #6
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Have you tried a search?

    Your original question asked how to write a function. I showed a basic shell from which to begin.

    oh yea..functions should be implemented using only arithmetic or logical operations. You should not use any library functions (e.g. sprintf, atoi, etc.) which is harder..=*(
    This type of "request" implies you want it done for you.


    Are you saying you have no idea how to convert the text representation of a digit into a number?
    Code:
    char digit = '4';
    int number = digit - '0';
    Or that you don't understand binary arithmetic?

    I'm not trying to be argumentative or condescending. I'm just trying to prod you into thinking a little bit about the problem you are trying to solve.

    If you can be a little more specific about what you don't understand, replies can address these issues much better. "I don't know anything" tends to mean "do it all". Unless you can disprove this assumption by demonstrating -- by posting an attempt -- that you understand some things, your posts are very likely to be interpreted this way.

    And if you choose not to post any attempts, learn to fine-tune your use of search engines.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  7. #7
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    >> ...since my teacher gave us none.

    Go get a good C programming book, and work through each chapter, one by one. Self-taught programming. What a concept!
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  8. #8
    Registered User
    Join Date
    Apr 2003
    Posts
    82
    Originally posted by Sebastiani
    >> ...since my teacher gave us none.

    Go get a good C programming book, and work through each chapter, one by one. Self-taught programming. What a concept!
    Sorry, but Im not taking a C class, im taking Computer Organization - Software. I only need to use C for this homework. So why waste money and time?

  9. #9
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,164
    The routines will be identical except for the return value.

    First zero the return value.

    Take the characters one at a time from the left. Convert them into binary by subtracting the ASCII value of '0' into a temp variable.

    Then shift the return value left by 1 bit using the << operator. OR the temp variable into the return value using the | operator.

    Continue for each character in the array.
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Void Functions Help
    By bethanne41 in forum C++ Programming
    Replies: 1
    Last Post: 05-09-2005, 05:30 PM
  2. Functions and Classes - What did I do wrong?
    By redmage in forum C++ Programming
    Replies: 5
    Last Post: 04-11-2005, 11:50 AM
  3. calling functions within functions
    By edd1986 in forum C Programming
    Replies: 3
    Last Post: 03-29-2005, 03:35 AM
  4. Factory Functions HOWTO
    By GuardianDevil in forum Windows Programming
    Replies: 1
    Last Post: 05-01-2004, 01:41 PM
  5. Shell functions on Win XP
    By geek@02 in forum Windows Programming
    Replies: 6
    Last Post: 04-19-2004, 05:39 AM