Go Back   CodingForums.com > :: Computing & Sciences > Computer Programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 06-08-2011, 04:35 AM   PM User | #1
phae36
New to the CF scene

 
Join Date: Jun 2011
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
phae36 is an unknown quantity at this point
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.
phae36 is offline   Reply With Quote
Old 06-08-2011, 05:57 AM   PM User | #2
oracleguy
Rockstar Coder


 
Join Date: Jun 2002
Location: USA
Posts: 9,043
Thanks: 1
Thanked 322 Times in 318 Posts
oracleguy is a jewel in the roughoracleguy is a jewel in the roughoracleguy is a jewel in the rough
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
oracleguy is offline   Reply With Quote
Old 06-08-2011, 06:03 AM   PM User | #3
phae36
New to the CF scene

 
Join Date: Jun 2011
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
phae36 is an unknown quantity at this point
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
phae36 is offline   Reply With Quote
Old 06-08-2011, 07:19 PM   PM User | #4
bobleny
Regular Coder

 
bobleny's Avatar
 
Join Date: May 2007
Posts: 256
Thanks: 3
Thanked 11 Times in 11 Posts
bobleny is on a distinguished road
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...
Get Firefox!
bobleny is offline   Reply With Quote
Old 06-08-2011, 07:33 PM   PM User | #5
phae36
New to the CF scene

 
Join Date: Jun 2011
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
phae36 is an unknown quantity at this point
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
phae36 is offline   Reply With Quote
Old 06-08-2011, 07:44 PM   PM User | #6
bobleny
Regular Coder

 
bobleny's Avatar
 
Join Date: May 2007
Posts: 256
Thanks: 3
Thanked 11 Times in 11 Posts
bobleny is on a distinguished road
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...
Get Firefox!
bobleny is offline   Reply With Quote
Old 06-08-2011, 08:02 PM   PM User | #7
phae36
New to the CF scene

 
Join Date: Jun 2011
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
phae36 is an unknown quantity at this point
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
phae36 is offline   Reply With Quote
Old 06-08-2011, 10:48 PM   PM User | #8
oracleguy
Rockstar Coder


 
Join Date: Jun 2002
Location: USA
Posts: 9,043
Thanks: 1
Thanked 322 Times in 318 Posts
oracleguy is a jewel in the roughoracleguy is a jewel in the roughoracleguy is a jewel in the rough
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
oracleguy is offline   Reply With Quote
Old 06-09-2011, 03:06 AM   PM User | #9
bobleny
Regular Coder

 
bobleny's Avatar
 
Join Date: May 2007
Posts: 256
Thanks: 3
Thanked 11 Times in 11 Posts
bobleny is on a distinguished road
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...
Get Firefox!
bobleny is offline   Reply With Quote
Reply

Bookmarks

Tags
c++, function, functions, pass, reference

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 12:05 AM.


Advertisement
Log in to turn off these ads.