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
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.
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
returnfalse;
else // otherwise, the remainder of a / 2 is 0, meaning a is even (or a is zero)
returntrue;
}
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:
#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.
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...
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.