Pass-by-reference

For my C++ homework I am asked the following questions, all of which I am absolutely clueless about:

1.

Complete the mySum1 function that takes two integer parameters and returns their sum.

Sample calls to the function look like

int i = mySum1(4, 3);
int j = mySum1(4 + 4, 45);

The prototype declaration is

int mySum1(int num1, int num2);



2.

Complete the mySum2 function that has 3 parameters. The first two are integer parameters and the third is set to the sum if the first two by the function call.

[Edit: To receive full credit, this function must have a return type of void]

Here are some sample calls to the function


int i, j, k;
mySum2(4, 3, i); // after the call, i contains 7
mySum2(i, 10, j); // after the call, j contains 17
mySum2(i, j, k); // after the call, k contains 24




3.

Write a function that converts from meters to feet AND inches. You will need to use reference parameters. The first parameter will be the number of meters and the second parameter will be the number of feet and the third parameter will be the number of inches. The number of feet and inches will be set by the function call.
1 meter = 39.37007 inches

Here are some sample calls:

double f, i, f2, i2;
convert(11.4, f, i); // 37 feet and 4.82 inches
convert(30.4, f2, i2); // 99 feet and 8.85 inches



Any help????
I read this stuff but it's all gibberish to me. For some reason I absolutely cannot wrap my mind around it. I'm screwed because I'm in a class I can't drop.
You will have to buy a good book such as the C++ Primer and try to stay ahead of your classes.
I have a C++ textbook in front of me and I read these online forums and what not but I just don't understand what the heck any of it is talking about.
To distill that page down to what matters to you, here is an example that is along the lines of what you need to do:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>

// This function returns true if a is an even integer or zero, and false otherwise.
bool is_even( int a )
{
    if( a % 2 == 0 )  // if the remainder of a / 2 is 1, then a is odd
       return false;
    else                   // otherwise, the remainder of a / 2 is 0, meaning a is even (or a is zero)
      return true;
}

int main()
{
    if( is_even( 42 ) == true )
         std::cout << "42 is indeed even!\n";

    if( is_even( 37 + 35 ) == true )
        std::cout << "72 is indeed even also!\n";   
}


How it works: is_even( 42 ) passes one parameter -- the value 42 -- to the function is_even, which is declared to take one integer parameter (and the parameter is named "a"). The function returns either true or false. We check the result of the function call to is_even against true in both cases.

This is all you need to know to complete #1.

For #2 and #3, you need to use references. Here's the same program written using references to return values from the function:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>

// This function returns true if a is an even integer or zero, and false otherwise.
void is_even( int a, bool& answer )
{
    if( a % 2 == 0 )  // if the remainder of a / 2 is 1, then a is odd
       answer = false;
    else                   // otherwise, the remainder of a / 2 is 0, meaning a is even (or a is zero)
      answer = true;
}

int main()
{
    bool b;
    
    is_even( 42, b );
    if( b == true )
         std::cout << "42 is indeed even!\n";

    is_even( 37 + 35, b );
    if( b == true )
        std::cout << "72 is indeed even also!\n";   
}


This is all you need to know to do #2 and #3. The rest is pure math.
wait. why would I be using if/else statements for adding together numbers and converting measurements???
I know this is cheap of me to be asking, but pleaseeeee will someone just tell me the answers so I don't get a 0% on this assignment??? As soon as I see the stuff written out it all makes sense but without someone physically here explaining it to me beforehand I'm clueless...
I have a C++ textbook in front of me

Not "a textbook". A good book. The vast majority of C++ books are garbage, it's obvious you won't understand much if you use them.
if you can't do the assignment, what are you gonna do with the midterms and the final? who's gonna help you?
seriously, this is no joke.

here is a little help:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33

#include<iostream> //for I/O
using namespace std; //to use cout/cin without std:: tags.


int mySum1(int num1, int num2); //prototype

int main(){

	for(int k=0; k<100; k++)
		cout << "I-will-do-my-assignments-on-my-own\n";

	int i = mySum1(4,3);
	cout << "mySum1(4,3): " << i << endl;

	int j = mySum1(4+4, 45);
	cout << "mySum1(4+4, 45): " << j << endl;

	return 0; //end of program
}


//implementation
int mySum1(int num1, int num2){
	return num1 + num2;
}

/*
output:

mySum1(4,3): 7
mySum1(4+4, 45): 53
*/


this one is a good book for you, used ones $20:
http://www.amazon.com/Sams-Teach-Yourself-Days-5th/dp/0672327112/ref=sr_1_1?ie=UTF8&qid=1309950141&sr=8-1

fyi: I'm learning too. I spend at least 2hrs a day, reading books/forums etc., typing code, etc etc... don't leave things to the last moment.

by the way; I guess that (4+4, 45) did confuse you. since the prototype tells its (num1, num2), compiler handles that 4+4 for you. you don't need to worry about it. if you map it num1 = 4 + 4, compiler knows what to do with it. you don't have to write any code to handle + operator.
Last edited on
Topic archived. No new replies allowed.