Thread: Function help

  1. #1
    Registered User
    Join Date
    Jul 2014
    Posts
    8

    Function help

    Hey guys just wondering if someone can help me understand this question and give me a bit of guide line as to how to do it!


    Q.
    Write a function that takes a string value for the day of the week as input and returns 1 if it is a weekday (mon-fri), otherwise it returns 0. Use appropriate types for the variables

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    What exactly do you not understand about the question?
    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
    Jul 2014
    Posts
    8
    I don't understand the returns 1 if it is a weekday otherwise returns 0. And does the program user enter what day it is?

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Ameteur hour
    I don't understand the returns 1 if it is a weekday otherwise returns 0.
    That just means that somewhere in the body of the function, you will have:
    Code:
    return 1;
    and at another place:
    Code:
    return 0;
    Quote Originally Posted by Ameteur hour
    And does the program user enter what day it is?
    This is not stated, so presumably it is up to you how you want to test and demonstrate the usage of the function. If your function is named is_weekday, then I would expect these to work:
    Code:
    if (is_weekday("mon"))
    Code:
    char day[] = "mon";
    if (is_weekday(day))
    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

  5. #5
    Registered User
    Join Date
    Jul 2014
    Posts
    8
    So is what you're saying i do something like this in the function?

    Code:
    if(days_week("Monday"))
        {
            return 0;
        }
    
    
        if(days_week("Tuesday"))
        {
            return 0;
        }
    
    
        if(days_week("Wednesday"))
        {
            return 0;
        }
        if(days_week("Thursday"))
        {
            return 0;
        }
    
    
        if(days_week("Friday"))
        {
            return 0;
        }
    
    
        if(days_week("Saturday"))
        {
            return 1;
        }
    
    
        if(days_week("Sunday"))
        {
            return 1;
        }

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    What is days_week?
    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

  7. #7
    Registered User
    Join Date
    Jul 2014
    Posts
    8
    The name of the function.

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    No, if that was the name of the function, then that cannot also be in the body of the function unless you're doing recursion, which isn't appropriate here.
    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

  9. #9
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    also your logic is opposite to the requested by the question
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  10. #10
    Registered User
    Join Date
    Jul 2014
    Posts
    8
    That's because i don't understand the question :/

  11. #11
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Do you know how to call a function; if not then, you need to learn that before laserlight post #4 will make sense.

    Edit: You appear to have no idea of the difference between a function body and calling a function.

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  12. #12
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Ameteur hour
    That's because i don't understand the question :/
    Here is a question that I shall pose to myself, along similiar lines as to what was posed to you.

    Q.
    Write a function that prints "Hello world!" to standard output. Use an appropriate function from the standard library.

    A.
    Code:
    void print_greeting(void)
    {
        printf("Hello world!\n");
    }
    Does this make sense to you?
    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

  13. #13
    Registered User zub's Avatar
    Join Date
    May 2014
    Location
    Russia
    Posts
    104
    Quote Originally Posted by Ameteur hour View Post
    So is what you're saying i do something like this in the function?

    Code:
    if(days_week("Monday"))
        {
            return 0;
        }
    
    // ...
    
        if(days_week("Sunday"))
        {
            return 1;
        }
    Code:
    int weekend(const char* const day)
    {
        return day[0] == 'S';
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Function Prototype, Function Call, and Function definition
    By dmcarpenter in forum C Programming
    Replies: 9
    Last Post: 04-09-2013, 03:29 AM
  2. Replies: 11
    Last Post: 09-07-2012, 04:35 AM
  3. Replies: 13
    Last Post: 03-20-2012, 08:29 AM
  4. Print function: sending a function.. through a function?
    By scarlet00014 in forum C Programming
    Replies: 3
    Last Post: 11-05-2008, 05:03 PM
  5. Replies: 9
    Last Post: 01-02-2007, 04:22 PM

Tags for this Thread