Results 1 to 3 of 3

Thread: Need help to convert c++ to java code program

  1. #1
    Join Date
    Nov 2010
    Posts
    7

    Need help to convert c++ to java code program

    Please help me to convert C++ program in Java code...
    I am putting C++ code below

    Code:
    /*
     * solution for "Challenge 2" from cplus.about.com
     * by programming begginer nDray...
     *
     * i only took the challenge to practice a bit of
     * recursion, since i thought it would be a good solution
     * for the given problem.
     *
     */
    
    #include <stdio.h> // for input/output operations
    
    #ifndef hr_timer
    #include "hr_time.h"
    #define hr_timer
    #endif
    
    char map[50][88]; // the map... the input file will be stored here
    int  island_area[20]; // array to store the various island's area
    int  island;      // variable to store the number of islands found
    int  area;        // temporary variable to store the + count for each island
    
    
    // find a piece of land to start from.
    int find(int* row, int* col)
    {
        int i, j;
        for(i = 2; i < 83; i++)
        {
            for(j = 0; j < 50; j++)
            {
                if(map[j][i] == '+')
                {
                    *row = j;
                    *col = i;
                    return 1;
                }
            }
        }
        return 0;
    }
    
    
    // look for more land, given a piece
    int count(int row, int col)
    {
        // if it's either out of bounds or on sea, it is not land
        if(row > 49 || col < 3 || col > 82 || map[row][col] != '+')
        {
            return 0;
        }
        // else, we found another bit, and will keep looking
        else
        {
            map[row][col] = '-';
            area++;
    
            if(count(row + 1, col    )) return 1; // S
            if(count(row + 1, col + 1)) return 1; // SE
            if(count(row    , col + 1)) return 1; // E
            if(count(row - 1, col + 1)) return 1; // NE
            if(count(row - 1, col    )) return 1; // N 
            if(count(row - 1, col - 1)) return 1; // NW
            if(count(row    , col - 1)) return 1; // W
            if(count(row + 1, col - 1)) return 1; // SW
        }
    
        // if there is none around, we looked everywhere....
        return 0;
    }
    
    
    int main()
    {
        int index;
        FILE * input;
        int row, col;
    
        stopWatch tick;
        
    
        // open the input (text)file for reading operations
        input = fopen("E:\\PC2\\map.txt", "rt");
    	startTimer(&tick);
        /* lets assume there's no problem
    
        if(!input)
        {
            printf("error opening input file");
            getint();
            return (1);
        }*/
    
        // read the lines from the input file to a
        // bidimensional array (interpreted as a map)
        for(index = 0; index < 50; index++)
        {
            fgets(map[index], 88, input);
        }
    
        // i'm pretty sure this should be the only part being clocked,
        // as this is actually where i start solving the problem itself
    
        while(find(&row, &col)) // while there's a piece of land not scanned
        {
            area = 0;
            count(row, col);              // i'll start counting the + in it
            island_area[++island] = area; // and store the count
        }
    
        printf("There are %d Continents\n", island);
        for(index = 1; index < island + 1; index++)
        {
            printf("Continent %d has %d .\n", index, island_area[index]);
        }
    
        stopTimer(&tick);
        printf("took %12.10f secs\n", getElapsedTime(&tick));
           
        // close the input file, as it is no longer needed
        fclose(input);
    
        getchar();
        return (0);
    }
    Please anyone convert this C++ program into Java as soon as possible.

    Thanks in advance

  2. #2
    Join Date
    Dec 2007
    Posts
    1,736

    Re: Need help to convert c++ to java code program

    I think that C++ to java conversion is not possible because C++ has a lot of features Java doesn't have. Like operator overloading, friends and multiple inheritance (this is just from the top of my head, I am sure there is more). Java to C++ conversion is also a mess because Java too have a lot of features/classes C++ doesn't have. Like a GUI library and data/time classes, just to name a few.

  3. #3
    Join Date
    Nov 2010
    Posts
    7

    Re: Need help to convert c++ to java code program

    Ohh , this is not c++ by the way its C . Sorry

    Do you know how to convert C to java ?

Similar Threads

  1. Convert C++ Program to Java Code!! Please help
    By jessesaini in forum Software Development
    Replies: 1
    Last Post: 24-04-2012, 12:24 AM
  2. pls convert this c++ to java code
    By xSim21 in forum Software Development
    Replies: 4
    Last Post: 26-03-2012, 06:32 PM
  3. Convert C++ code into JAVA
    By Macario in forum Software Development
    Replies: 6
    Last Post: 13-11-2011, 07:36 PM
  4. convert C++ program in Java code
    By vaibhavsri in forum Software Development
    Replies: 7
    Last Post: 16-07-2010, 10:08 AM
  5. How can I convert my java program to an .exe file?
    By Baran in forum Software Development
    Replies: 3
    Last Post: 02-03-2009, 07:04 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,711,713,031.60325 seconds with 17 queries