Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 9 of 9
  1. #1
    New to the CF scene
    Join Date
    Jun 2011
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Exclamation Pass-by-Reference and Pass-by-Value

    Hi, im new to c++ and just finished a course, i passed, but noticed i struggled with functions, and i was wondering if you can help me with this exercise that a friend suggested

    Write a user friendly program that converts a time from milliseconds to hours : minutes : seconds format. The program should contain:


    a. An input function that prompts the user for a time expressed as an integer number of milliseconds. Use a value-returning function for this.


    b. A function that computes the time in hours, minutes and seconds from the number of milliseconds.

    Use a void function with one pass-by-value parameter for the number of milliseconds and three pass-by-reference parameters for hours, minutes and seconds respectively.


    c. An output function that displays the properly formatted time as hh:mm:ss.sss. This should be a void function that accepts hours, minutes and seconds as arguments.

  • #2
    Rockstar Coder
    Join Date
    Jun 2002
    Location
    USA
    Posts
    9,074
    Thanks
    1
    Thanked 328 Times in 324 Posts
    Can you be more specific as to what your question is? Are you not sure what the difference is between by value and by reference parameters are?
    OracleGuy

  • #3
    New to the CF scene
    Join Date
    Jun 2011
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by oracleguy View Post
    Can you be more specific as to what your question is? Are you not sure what the difference is between by value and by reference parameters are?
    I am unsure, and also I don't know how to set up and solve this exercise, what I want is someone to solve this for me and explain the pass by reference/value used in this program

  • #4
    Regular Coder bobleny's Avatar
    Join Date
    May 2007
    Posts
    258
    Thanks
    3
    Thanked 11 Times in 11 Posts
    Wait, your saying you passed a course on C++ and you not only don't know the difference between the reference and value parameters, you also don't know how to write a basic function???

    I try my hardest to give people the benefit of the doubt, but come on, even you have to admit this doesn't sound right...


    That doesn't mean I'm not willing to help you out though, but I need to see from you your best attempt at writing this program first.
    --www.firemelt.net--
    * No good deed goes unpunished.
    * Cheer up, the worst has yet to come...

  • #5
    New to the CF scene
    Join Date
    Jun 2011
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts
    It was a really beginner type course I barely passed, I have tried to write the program however what I have done comes up with errors and it's become really frustrating and I realized I didn't know how to setup the functions I am asked to setup in the function, so I eventually turned to the Internet

  • #6
    Regular Coder bobleny's Avatar
    Join Date
    May 2007
    Posts
    258
    Thanks
    3
    Thanked 11 Times in 11 Posts
    I still want to see your best attempt at writing the four functions, errors or no errors...
    --www.firemelt.net--
    * No good deed goes unpunished.
    * Cheer up, the worst has yet to come...

  • #7
    New to the CF scene
    Join Date
    Jun 2011
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts
    void calcTime(milliseconds);
    {
    double seconds = milliseconds / 1000.0;
    double minutes = seconds / 60.0;
    double hours = minutes / 60.0;
    }

    this is supposed to be the function to calculate, and i have no idea how to put together the other functions

  • #8
    Rockstar Coder
    Join Date
    Jun 2002
    Location
    USA
    Posts
    9,074
    Thanks
    1
    Thanked 328 Times in 324 Posts
    Ok, so do you remember how to get user input? You need to do that for part A. The function needs to return the number the user entered so it would have an int as a return type and take no parameters. Inside that function you need to print out a prompt asking for the number of milliseconds. Then you need to read what they type into a variable and return it.

    Try writing a function that did that does that, you'll need to use cin and cout here.
    OracleGuy

  • #9
    Regular Coder bobleny's Avatar
    Join Date
    May 2007
    Posts
    258
    Thanks
    3
    Thanked 11 Times in 11 Posts
    Quote Originally Posted by phae36 View Post
    void calcTime(milliseconds);
    {
    double seconds = milliseconds / 1000.0;
    double minutes = seconds / 60.0;
    double hours = minutes / 60.0;
    }

    this is supposed to be the function to calculate, and i have no idea how to put together the other functions
    Well, that's a start. Not overly impressed with the effort though...

    A common mistake for beginners is to put a semicolon after the function declaration.

    Your other problem is you failed to declare milliseconds, that is milliseconds has no data type, so the program doesn't know what to do with it. This will give a fairly self explanatory compile error.

    Other than that, your function will work just fine. If you would like to make milliseconds a pass by reference instead of value, throw an ampersand (&) in front of it.

    This appears to be a very good guide on functions in C++.
    http://www.cplusplus.com/doc/tutorial/functions/

    This one explains the difference between reference and value and how to use them. Basically, when you pass a variable by value, you are sending a copy of the variable. When you pass a variable by reference you are sending the variable, or more specifically, I believe you are passing the address of the variable.
    http://www.cplusplus.com/doc/tutorial/functions2/

    Read those two pages, and write the other three functions. If you are having an issue with the insides of the functions, ignore them for now. Just declare the functions and get them to compile. Then worry about the insides.
    --www.firemelt.net--
    * No good deed goes unpunished.
    * Cheer up, the worst has yet to come...


  •  

    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •