Can't figure out the Java equivalent of "passing by reference" in C++ to solve this, and the best method to solve by -
this question has answer here:
i took cs-1 course in c++ , i'm @ new university , use java here. i've been learning new syntax , doing old c++ labs , i've hit wall pass-by-reference one.
my understanding far java pass-by-value how can achieve goal of problem?
write program tells coins give out amount of change 1 cent 497 cents. since wouldn’t use pennies, need round cents near 5 or 10’s. example, if amount 368 cents, rounded number 370 cents , if amount 367 cents, rounded number 365 cents. change 1 toonie (two dollar coin), 1 loonie, 2 quarters, 2 dimes 370 cents. use coin denominations of 2 dollars (toonie), 1 dollar (loonie), 25 cents (quarters), 10 cents (dimes), , 5 cents (nickels). program use following function:
void computecoin(int coinvalue, int &number, int &amountleft);
note function needs return 2 values must use reference variables.
for example, suppose value of variable amountleft 370 cents. then, after following call, value of number 1 , value of amountleft 170 (because if take 1 toonie 370 cents, leaves 170 cents):
computecoin(200, number, amountleft);
print value of number coin name before make following call:
computecoin(100, number, amountleft);
and on. include loop lets user repeat computation new input values until user enter sentinel value stop program.
in c++ use reference variables , value of coin changed. far i've learned objects way use references, don't know how go or if there better way. i've included c++ code here:
#include <iostream> using namespace std; void computecoin(int coinvalue, int & number, int & amountleft); int main() { int number, change, amountleft, remainder; { cout << "enter change (-1 end): "; cin >> change; while (change>497) { cout << "the change should less 497. try again.\n\n"; cout << "enter change (-1 end): "; cin >> change; } if (change != -1) { // round near 5 or 10's remainder = change % 5; if (remainder <= 2) amountleft = change - remainder; else amountleft = change + 5 - remainder; if (amountleft == 0) cout << "no change."; else { cout << amountleft << " cents can given as\n"; // compute number of toonies computecoin(200, number, amountleft); if (number>0) cout << number << " toonie(s) "; // compute number of loonies computecoin(100, number, amountleft); if (number>0) cout << number << " loonie(s) "; // compute number of quarters computecoin(25, number, amountleft); if (number>0) cout << number << " quarter(s) "; // compute number of dimes computecoin(10, number, amountleft); if (number>0) cout << number << " dime(s) "; // compute number of nickels computecoin(5, number, amountleft); if (number>0) cout << number << " nickel(s) "; } cout << endl << endl; } } while (change != -1); cout << "\ngoodbye\n"; return 0; } void computecoin(int coinvalue, int &number, int &amountleft) { number = amountleft / coinvalue; amountleft %= coinvalue; }
you can (and should) return object result joop eggen indicated in comments.
an alternative works (technically work, should not used) pass in mutable object this:
class mutableinteger { private int value; public void setvalue(final int val) { value = val; } public int getvalue() { return value; } }
then pass in instance of instead of int parameter method.
basically struct in c. again, suggest returning result in specific case though.
Comments
Post a Comment