![]() |
| | #1 |
| Registered User Join Date: Oct 2004
Posts: 13
| Urgent homework help for NEWBIE Our assignment: Use pass by references and values to write a program that convert miliseconds into hours: minutes : second format. My problem: lack of understanding what pass by value and reference is. Help?: any suggestion to the program or explaination to what "pass by references/values" are would be greatly appricated. Last edited by Kazasan; 10-11-2004 at 02:48 PM. |
| Kazasan is offline | |
| | #2 |
| Registered User Join Date: Sep 2004
Posts: 29
| A Sample Has something to do with the asmperand in this program it passes by referance values... Code:
#include <iostream>
using namespace std;
float sum (float &int1, float &int2); //function prototype sum
float minus (float &int1, float &int2); //function prototype minus
float mult (float &int1, float &int2); //function prototype mult
float divide (float &int1, float &int2); //function prototype divide
int main() //function main
{
float a =0;
float b =0;
int choice =0;
cout.setf(ios::showpoint); //always show decimal
cout.setf(ios::fixed);
cout.precision(2); //set output to two digits
do {
cout << "Welcome to the calculator please make a choice:""\n\n";
cout << "(1) Add two numbers.""\n";
cout << "(2) Subtract two numbers.""\n";
cout << "(3) Mutiply two numbers.""\n";
cout << "(4) Divide two numbers.""\n";
cout << "(5) Exit.""\n\n";
cin >> choice;
switch (choice)
{
case 1:
cout << "Enter numbers to add. ";
cin >> a >> b;
cout << "The product of your two numbers is "<< sum(a,b) <<"\n";
break;
case 2:
cout << "Enter numbers to subtract. ";
cin >> a >> b;
cout << "The subtraction of your two numbers is "<< minus(a,b) <<"\n";
break;
case 3:
cout << "Enter numbers to multiply. ";
cin >> a >> b;
cout << "The multpication of your two numbers is "<< mult(a,b) <<"\n";
break;
case 4:
cout << "Enter numbers to divide. ";
cin >> a >> b;
cout << "The division of your two numbers is "<< divide(a,b) <<"\n";
break;
default:
break;
}
}while (choice!=5);
return 0;
} // end main
//---------------------------------------------------------------------------------------------------------
// Define the sum function.
float sum(float &x, float &y) // This function returns the sum of its arguments.
{return (x + y);}
float minus(float &x, float &y) // This function returns the subtraction of its arguments.
{return (x - y);}
float mult(float &x, float &y) // This function returns the mutlipication of its arguments.
{return (x * y);}
float divide(float &x, float &y) // This function returns the division of its arguments.
{return (x / y);}
|
| dAzed is offline | |
| | #3 |
| Lead Moderator Join Date: Aug 1998
Posts: 2,568
| Show us what you know or explain how you are confused. People won't write your program for you, but if you show us what you have or where you're tripping up, they'll be glad to point you in the right direction. Kermi3 Lead Mod |
| kermi3 is offline | |
| | #4 |
| Registered User Join Date: Oct 2004
Posts: 13
| could you explain what the "&" means? |
| Kazasan is offline | |
| | #5 | |
| Registered User Join Date: Sep 2004
Posts: 720
| Quote:
i certainly hope that you intentionally gave a bad answer to a bad question...... i'm not sure which was worse.... | |
| misplaced is offline | |
| | #6 |
| Registered User Join Date: Sep 2004
Posts: 720
| why don't you look up what the & means... then tell us what you think it means.... then we can tell you what it means |
| misplaced is offline | |
| | #7 |
| Registered User Join Date: Aug 2004
Posts: 307
| Passing by reference (I think) means that you're passing the memory address of a variable to a function or another variable (eg: pointer). |
| Lithorien is offline | |
| | #8 |
| Registered User Join Date: Jul 2003
Posts: 1,088
| The '&' has different meanings in different contexts. For your assignment, it sounds like you can focus on its meaning when used to indicate the type of a variable as being a reference. Code: void squareIt(int& value)
{
value = value * value;
}
int main()
{
int var = 3;
squareIt(var);
// var is now equal to 9.
}
Code: int doubleIt(int value)
{
value = value + value;
return value;
}
int main()
{
int var = 3;
int result = doubleIt(var);
// var is still 3.
// result is equal to 6.
}
As a general rule (not for this assignment, obviously) you pass simple built-in types like int, double and char by value. If you want to return a result, you return it rather than using a reference unless you have to return more than one value or unless you make it extra clear that your function will change the value of the variable. When you use more complicate classes you pass by const reference so that you don't make a copy of data unnecessarily. If you want to actually modify the value of the object, then you pass by non-const reference, again being sure that it is clear that you will be modifying the object. |
| jlou is offline | |
| | #9 | |
| Registered User Join Date: Oct 2004
Posts: 13
| Quote:
my hope is to finish my homework, but my goal is to understand what im trying to do. Thanks so much for all of your help. #include <iostream> #include <iomanip> using namespace std; militohours(int msec, int hours) militominutes(int msec, int minutes) militoseconds(int msec, int seconds) int main () { cout << "please enter time (interger) " << endl; cin >> msec; cout << "The time is (hours) " << hours; cout << minutes; cout << seconds << endl; return 0; } | |
| Kazasan is offline | |
| | #10 | |
| Registered User Join Date: Oct 2004
Posts: 13
| Quote:
^i tried what i thought would work up there, any suggestion to how i should go about the functions? im really not sure what i would need to do for the pass by values/reference part. Last edited by Kazasan; 10-11-2004 at 03:44 PM. | |
| Kazasan is offline | |
| | #11 |
| Registered User Join Date: Mar 2004
Posts: 113
| ok heres my try to explain you and sorry about my poor english... basically you can pass variables to functions in 2 ways : by value and by reference. when you declare a variable ie: int myArray[5000]; and then you compile your program, the compiler "puts" that value in a block of memory. if you are using a function that receives that array(myArray) it can receive a "copy" of that variable(passing by value) or it can receive the location of that variable(passing by reference)that actually means : tell to the compiler that retrieves that value from the piece of memory xxx. ok know you say whats the difference or whats is better? when you pass a varible by value a copy of that variable is created and that copy is passed to the function. when you pass a variable by reference you are not passing a copy of that variable you are telling the program to retrieve that data from the piece of memory xxx. if you see, passing by value is less optimal. imagine that u r passing a big object like an array of 5000 ints by value, the compiler creates a new object of 200,000 bytes . pluss the 200,000 bytes already reservated for the original object. but if you pass that object by ref. you only use 200,000 bytes of memory for the already created object. how you use it? ok heres an example Code: #include <iostream>
using std::cout;
using std::endl;
void byValue(int);//passing by value
void byRef(int &);//passing by ref
int main()
{
int myVar = 100;
cout << "now displaying the value of myVar passing by value:\n";
byValue(myVar);
cout << "\nnow displaying the value of myVar passing by reference:\n";
byRef(myVar);
cout << endl;
return 0;
}
//know we will display the value of the variable by value(a copy remember?)
void byValue(int localVar)
{
//localVar is a copy of myVar
cout << localVar;
}
//know displaying the value passed by reference
void byRef(int &localVar)//notice the "&" before localVar
{
//here you r using the "original variable not a copy, so this function is best
//in performance
cout << localVar;
}
heres another simple example: int var = 100; int &var2 = var; //passing a reference of var to var2 cout << var2;//printing var2 that actually contains the memory direction of var. hope it helps you, because i cant find better words to explain it because my english sucks |
| terracota is offline | |
| | #12 |
| Registered User Join Date: Oct 2004
Posts: 13
| ^GREAT explanation. your english is better than mine, if you fix "know" with "now". THANKS THANKS |
| Kazasan is offline | |
| | #13 |
| Registered User Join Date: Oct 2004
Posts: 13
| Thanks guys for all of your time. this is the best thread i've been to. Once again THANK YOU. |
| Kazasan is offline | |
![]() |
| Thread Tools | |
| Display Modes | |
|
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| need urgent Homework help | zakimasmoudi | C Programming | 2 | 11-11-2007 03:54 PM |
| Urgent - Help - Homework Question | Sway2 | C++ Programming | 12 | 10-04-2002 01:35 PM |
| Urgent Maths Homework :( | (TNT) | A Brief History of Cprogramming.com | 5 | 01-09-2002 09:01 PM |
| Homework | kermi3 | A Brief History of Cprogramming.com | 11 | 11-03-2001 04:39 PM |
| Homework | kermi3 | C Programming | 10 | 09-27-2001 04:49 PM |