Thread: Urgent homework help for NEWBIE

  1. #1
    Registered User
    Join Date
    Oct 2004
    Posts
    13

    Urgent homework help for NEWBIE

    We are learning pass by references and values in our engineer 233 class.
    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.

  2. #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);}

  3. #3
    Lead Moderator kermi3's Avatar
    Join Date
    Aug 1998
    Posts
    2,595
    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

    If you're new to the boards, welcome and reading this will help you get started.
    Information on code tags may be found here

    - Sandlot is the highest form of sport.

  4. #4
    Registered User
    Join Date
    Oct 2004
    Posts
    13
    could you explain what the "&" means?

  5. #5
    Registered User
    Join Date
    Sep 2004
    Posts
    719
    Quote Originally Posted by dAzed
    Has something to do with the asmperand in this program it passes by referance values...

    i certainly hope that you intentionally gave a bad answer to a bad question......

    i'm not sure which was worse....

  6. #6
    Registered User
    Join Date
    Sep 2004
    Posts
    719
    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

  7. #7
    Registered User
    Join Date
    Aug 2004
    Location
    San Diego, CA
    Posts
    313
    Passing by reference (I think) means that you're passing the memory address of a variable to a function or another variable (eg: pointer).

  8. #8
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,090
    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.
    }
    In the above example, the function squareIt takes a single argument. That argument is an integer reference. When the function is called, the variable var is passed by reference to the function. This means that any changes made to the argument in squareIt automatically happens to var as well.
    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.
    }
    In this example, the variable is passed by value to doubleIt. This means that a copy of the value is made for the function to use. So any changes you make to value do not affect var.

    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.

  9. #9
    Registered User
    Join Date
    Oct 2004
    Posts
    13
    Quote Originally Posted by kermi3
    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
    I agree with you, so hmm you guys can call me an idiot if u want, but this is about as much as i could do. I dont know how to go about intergrating the pass by references and values into the functions, and what are they suppose to do.
    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;
    }

  10. #10
    Registered User
    Join Date
    Oct 2004
    Posts
    13
    Quote Originally Posted by jlou
    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.
    }
    In the above example, the function squareIt takes a single argument. That argument is an integer reference. When the function is called, the variable var is passed by reference to the function. This means that any changes made to the argument in squareIt automatically happens to var as well.
    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.
    }
    In this example, the variable is passed by value to doubleIt. This means that a copy of the value is made for the function to use. So any changes you make to value do not affect var.

    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.
    Thanks so much, it made the concept alot clearer now. Yet i still don't know what i should do with my program.
    ^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.

  11. #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;
    }
    as you see to pass a variable by reference you must use the "&" before it.
    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

  12. #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

  13. #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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. need urgent Homework help
    By zakimasmoudi in forum C Programming
    Replies: 2
    Last Post: 11-11-2007, 03:54 PM
  2. Urgent - Help - Homework Question
    By Sway2 in forum C++ Programming
    Replies: 12
    Last Post: 10-04-2002, 01:35 PM
  3. Urgent Maths Homework :(
    By (TNT) in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 01-09-2002, 09:01 PM
  4. Homework
    By kermi3 in forum A Brief History of Cprogramming.com
    Replies: 11
    Last Post: 11-03-2001, 04:39 PM
  5. Homework
    By kermi3 in forum C Programming
    Replies: 10
    Last Post: 09-27-2001, 04:49 PM