Thread: Just for fun: "Expecting" function

  1. #1
    Registered User
    Join Date
    Nov 2017
    Posts
    29

    Just for fun: "Expecting" function

    Hello, coding aficionados! I've dabbled a bit in C during my academics and want to have some fun with writing a short function that sums up pregnancy. I'd then like to take that code snippet and slap it on a t-shirt. I'm not that great with C so that's why I'm here!

    General concept
    • Have a delay function signifying 39 weeks or end date
    • IF ELSE statement for girl/boy/unknown which leads to (especially fun if multiples)
    • Names if they are picked out then
    • Having them in the return line


    I'm thinking it doesn't need to be more than 10 lines, but I'm certainly open to interpretations. I will happily credit the user that knocks my socks off.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    As you say, this is for fun, so you don't have to take it so seriously, hence you just need to make it recognisably C. For example:
    Code:
    struct baby *expecting(void) {
        delay(23587200);
        size_t n = 1 + random(MAX_CHILDREN);
        struct baby *babies = malloc(n * sizeof(*babies));
        for (size_t i = 0; i < n; i++) {
            switch (random(3)) {
            case 0:
                babies[i].sex = GIRL;
                strcpy(babies[i].name, GIRL_NAMES[random(GIRL_NAMES_COUNT)]);
                break;
            case 1:
                babies[i].sex = BOY;
                strcpy(babies[i].name, BOY_NAMES[random(BOY_NAMES_COUNT)]);
                break;
            }
        }
        return babies;
    }
    The use of void as the parameter list, the use of struct baby rather than just baby, along with the malloc call, all these signal to a reader who knows C that this is likely to be a C (rather than say, C++) function, despite neither delay nor random being part of standard C. Sure, in real code we should check the return value of malloc and be wary about whether this use of strcpy might allow for buffer overflow vulnerability at some point, but that will just clutter the code snippet.

    I would just omit the name thing because it doesn't add any value to this unless you have a small set of names in mind so that you can declare the names for flavour. But then you may run into the minefield of diversity in name selection. The sex vs gender thing is another minefield: I picked "sex" because it is shorter and omitted "unknown" because this is just an example, but you're going to have to navigate the sex vs gender minefield yourself, e.g., what is an "unknown" sex? Is it intersex? What is an "unknown" gender? Are the genders of all babies unknown, or are they always provisionally known due to the interpretation of people around them, etc? Consequently, you may find it easier for T-shirt purposes to omit this and say, just loop to pick random names from a pre-defined list that is not shown or something.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Nov 2017
    Posts
    29
    Quote Originally Posted by laserlight View Post
    I would just omit the name thing because it doesn't add any value to this unless you have a small set of names in mind so that you can declare the names for flavour. But then you may run into the minefield of diversity in name selection. The sex vs gender thing is another minefield: I picked "sex" because it is shorter and omitted "unknown" because this is just an example, but you're going to have to navigate the sex vs gender minefield yourself, e.g., what is an "unknown" sex? Is it intersex? What is an "unknown" gender? Are the genders of all babies unknown, or are they always provisionally known due to the interpretation of people around them, etc? Consequently, you may find it easier for T-shirt purposes to omit this and say, just loop to pick random names from a pre-defined list that is not shown or something.
    Thanks for the help! As you mentioned, it's not formal C so being able to recognize the basic format was the idea. I know some couples like to be surprised with the gender reveal which was the "unknown" factor, but that' doesn't really work here. Regarding the names, an arbitrary list pretty well represents it. My partner and I compiled four to five boy and girl names (from much larger lists) to pick from before finding out the gender. Well done, laserlight!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 12-08-2014, 08:12 PM
  2. Replies: 3
    Last Post: 03-27-2008, 11:44 PM
  3. Replies: 8
    Last Post: 03-10-2008, 11:57 AM
  4. "itoa"-"_itoa" , "inp"-"_inp", Why some functions have "
    By L.O.K. in forum Windows Programming
    Replies: 5
    Last Post: 12-08-2002, 08:25 AM
  5. "CWnd"-"HWnd","CBitmap"-"HBitmap"...., What is mean by "
    By L.O.K. in forum Windows Programming
    Replies: 2
    Last Post: 12-04-2002, 07:59 AM

Tags for this Thread