Results 1 to 3 of 3
  1. #1
    napoleon is offline Member
    Join Date
    Mar 2010
    Posts
    1
    Rep Power
    0

    Default 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];
    }

  2. #2
    SreerajSarma is offline Member
    Join Date
    Mar 2010
    Posts
    6
    Rep Power
    0

    Default

    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
    Java 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
    }
    you can get the value of M back by coding like this
    Java 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;
    now you can easily convert this to java
    there may be other possible ways also but you can consider this

  3. #3
    toadaly is offline Senior Member
    Join Date
    Jan 2009
    Posts
    671
    Rep Power
    6

    Default

    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:

    Java Code:
    class IntWrapper {
      int M;
    }
    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.

Similar Threads

  1. Replies: 8
    Last Post: 07-30-2010, 05:47 PM
  2. Troubles,convert C/C++ Code to Java
    By cristianll in forum New To Java
    Replies: 1
    Last Post: 11-15-2009, 02:30 AM
  3. Convert java code to midlet code
    By coldvoice05 in forum New To Java
    Replies: 1
    Last Post: 08-12-2009, 11:14 AM
  4. Convert java code to midlet code
    By coldvoice05 in forum Advanced Java
    Replies: 1
    Last Post: 08-09-2009, 01:21 PM
  5. Convert bar code to number
    By samatha_kudi in forum Advanced Java
    Replies: 1
    Last Post: 07-17-2009, 02:56 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •