Please help me to convert C++ program in Java code...
I am putting C++ code below
Code:
#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
Bookmarks