Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Help me with code conversion

  1. #1
    Junior Member
    Join Date
    Nov 2012
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Help me with code conversion

    Hello!

    Could anybody help me with conversioning c++ code to java...I'm having a very hard time with pointers and memory allocations (memcpy) written in c++, which I want to write in java.I would be very grateful, if somebody took a couple of minutes and help me to fix this problem, which is bothering me for days.

    Here's the code:

    #include<iostream>
    using namespace std;

    class CMO
    {

    private:

    int m_cnt;


    public:
    /* Constructor/Destructor */

    CMO(int in[], int cnt)
    {
    m_in = new int[cnt];
    m_cnt = cnt;

    for(int i=0;i<m_cnt;i++)
    {
    m_in[i]=in[i];
    }
    }


    int Sort()
    {
    return mSort(0, m_cnt);
    }

    private:
    int mSort(int ind, int cnt)
    {
    int tmp;
    int left, right;

    if (cnt == 1) {
    return 0;
    }

    left = (cnt + 1) / 2;
    right = cnt / 2;

    if (cnt == 2) {
    mDisplay(ind, 1, 1);

    if (m_in[ind] > m_in[ind+1]) {
    tmp = m_in[ind];
    m_in[ind] = m_in[ind+1];
    m_in[ind+1] = tmp;
    }

    mDisplay(ind, 2, 0);

    } else {
    mDisplay(ind, left, right);

    mSort(ind, left);
    mSort(ind+left, right);

    mOrder(ind, left, right);

    mDisplay(ind, left + right, 0);
    }

    return 0;
    }

    int mOrder(int ind, int left, int right)
    {
    int i, a, b;
    int *tmp;

    tmp = new int[left+right];
    a = b = 0;

    for (i = 0; i < (left+right); ++i) {
    if ((m_in[ind+a] > m_in[ind+left+b] && b < right) || a >= left) {
    tmp[i] = m_in[ind+left+b];
    ++b;
    } else if (a < left) {
    tmp[i] = m_in[ind+a];
    ++a;
    }
    }

    memcpy(&m_in[ind], tmp, (left + right) * sizeof(int));





    delete [] tmp;

    return 0;
    }


    void mDisplay(int ind, int left, int right)
    {
    int i, j;

    for (i = 0; i < left; ++i) {
    std::cout << m_in[ind+i] << " ";
    }

    if (right == 0) {
    std::cout << std::endl;
    return;
    }

    std::cout << "| ";

    for (j = 0; j < right; ++j) {
    std::cout << m_in[ind+i+j] << " ";
    }
    std::cout << std::endl;
    }


    };


    int main(int argc, char *argv[])
    {
    int in[] = {8, 5, 6, 1, 7, 2, 0, 9};

    CMO cmo(in,8);

    cmo.Sort();

    system("PAUSE");
    return 0;
    }


  2. #2
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,676
    Thanks
    25
    Thanked 329 Times in 305 Posts

    Default Re: Help me with code conversion

    Please use code tags in the future.

    What do you mean by having trouble with writing memory allocation in java? You don't even have to worry about memory allocation when writing java code (unless you are talking about setting the size of an array, but that's technically something very different).
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

  3. #3
    Super Moderator pbrockway2's Avatar
    Join Date
    Jan 2012
    Posts
    987
    Thanks
    6
    Thanked 206 Times in 182 Posts

    Default Re: Help me with code conversion

    Begin with the code are trying to convert. (Ie, put Java aside temporarily to make the problem more tractable). The aim is understand what the code is doing. Then you will have something specific to implement in Java.

    A convenient way to express the behavior of the C++ code is to add comments before each method, saying what that method does. Pay attention to what each argument represents, to what is meant by any returned value, and to the side effects of the method (including what it might print).

    ---

    If the conversion is not the task you are trying to do - that is, if you are trying to do something and have come to believe that the C++ code is doing that thing - consider that that the attempt to convert the code may be a distraction. Especially if you are unfamiliar with the language the approach outlined above may involve you in more work than simply writing the Java code.

  4. #4
    Junior Member
    Join Date
    Nov 2012
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Help me with code conversion

    Hello, thank you for replying me quickly.

    I'm trying to get a substitute code done by pointers in c++ (which explicitly don't exist in java), by equivalent (non-pointer) code in java.

Similar Threads

  1. Replies: 9
    Last Post: August 30th, 2012, 03:25 PM
  2. conversion d'un fichier doc en un fichier access (code java)
    By Chourouk2012 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: August 7th, 2012, 01:51 PM
  3. File conversion
    By SrikanthSuresh in forum Member Introductions
    Replies: 1
    Last Post: August 2nd, 2012, 07:36 AM
  4. Help with conversion
    By TheEvilCouncil in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 2nd, 2012, 08:29 PM
  5. BASE CONVERSION
    By bgwilf in forum Algorithms & Recursion
    Replies: 6
    Last Post: November 13th, 2010, 12:02 PM