Results 1 to 3 of 3
Thread: Convert C++ code to Java
- 03-06-2010, 08:26 PM #1
Member
- Join Date
- Mar 2010
- Posts
- 1
- Rep Power
- 0
Convert C++ code to Java
I have to convert below C++ code to Java. Everything is simple, except the &M reference from C++! How can I use in Java?
Java Code:#include <iostream> using namespace std; const int MAX = 100; void MinCoinGreedy(int n, int c[], int m, int nc[], int &M); int main() { int n, c[MAX], m, nc[MAX], M; cout << "Amount: "; cin >> n; cout << "No. coin: "; cin >> m; cout << "\nCoins \n"; for(int i = 0; i < m; i++) { cout << "c[" << i << "] : "; cin >> c[i]; nc[i] = 0; } MinCoinGreedy(n, c, m, nc, M); cout << endl; for(int i = 0; i < m; i++) cout << c[i] << ' ' << nc[i] << endl; cout << "\n\nM = " << M << endl; return 0; } void MinCoinGreedy(int n, int c[], int m, int nc[], int &M) { for(int i = m-1; i >= 0; i--) while(n >= c[i]) { nc[i]++; n -= c[i]; } M = 0; for(int i = 0; i < m; i++) M += nc[i]; }
- 03-07-2010, 05:22 AM #2
Member
- Join Date
- Mar 2010
- Posts
- 6
- Rep Power
- 0
Instead of passing M as (pass by reference), you can pass M
as pass by value
convert the function prototype to
Java Code:void MinCoinGreedy(int n, int c[], int m, int nc[], int &M); to int MinCoinGreedy(int n, int c[], int m, int nc[], int M);
the function can be changed like this
you can get the value of M back by coding like thisJava Code:int MinCoinGreedy(int n, int c[], int m, int nc[], int M) { for(int i = m-1; i >= 0; i--) while(n >= c[i]) { nc[i]++; n -= c[i]; } M = 0; for(int i = 0; i < m; i++) M += nc[i]; return M; //note the change here }
now you can easily convert this to javaJava Code:int mini=MinCoinGreedy(n, c, m, nc, M); cout << endl; for(int i = 0; i < m; i++) cout << c[i] << ' ' << nc[i] << endl; cout << "\n\nM = " << mini << endl;
there may be other possible ways also but you can consider this
- 03-07-2010, 07:44 AM #3
Senior Member
- Join Date
- Jan 2009
- Posts
- 671
- Rep Power
- 5
To pass an intrinsic type into a java method and have it altered in the caller's space, you have to wrap it in a mutable object.
For example:
Now you can pass an IntWrapper in place of the 'int'. The 'Integer' class won't work, because it's immutable. Of course, side effects are discouraged (which is what is happening in your c++ program). A cleaner approach would be to pass M in as an int, and have your method return the new value of M.Java Code:class IntWrapper { int M; }
Similar Threads
-
Convert avi, mpeg, wmv media files to .flv files in java code
By vinay1497 in forum New To JavaReplies: 8Last Post: 07-30-2010, 05:47 PM -
Troubles,convert C/C++ Code to Java
By cristianll in forum New To JavaReplies: 1Last Post: 11-15-2009, 02:30 AM -
Convert java code to midlet code
By coldvoice05 in forum New To JavaReplies: 1Last Post: 08-12-2009, 11:14 AM -
Convert java code to midlet code
By coldvoice05 in forum Advanced JavaReplies: 1Last Post: 08-09-2009, 01:21 PM -
Convert bar code to number
By samatha_kudi in forum Advanced JavaReplies: 1Last Post: 07-17-2009, 02:56 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks