• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Convert C code in Java

 
Mayur Soneta
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Guys I have a code in C where I have made...but I want to do it in Java....
Unfortunately I dont know much about java..I would request someone can help me..


here is the C code:

#include <stdio.h>
#include <sys/time.h>
//#define PRINT

int main ()
{
/** initialization of variables start and end to zero */
hrtime_t start=0, end=0;
/** Declaring variables and an array */
int *a, n, i, k;
/**Initializing the array with three elements */
int input[]={5,6,7,8,9,10,11,12,13};
void permute (int *, int, int );
void print(int *, int, int);
void swap(int *,int *);

for ( n = 0; n < 8; n++ )
{
/** allocating memory for the output array */
a = (int *) malloc (sizeof(int)*input[n]);
/** Initialize the array to consecutive numbers starting with 1 */

/** Initializing the loop with i and a[0] and loop until the end of input array */
for ( i = 0, a[0] = 1; i < input[n]; a[i] = ++i ) ;
start = gethrtime();
/**calling the permute function */
permute(a ,0 ,input[n]);
end = gethrtime();
printf("\n Time taken to calculate permutation of %d numbers: %lld msecs\n",input[n],( end-start ) / 1000000 );
/**Releasing the memory allocated */
free(a);
}

return ( 0 );
}


void print(int *a,int k,int n)
{
int i=0;
for(i=0;i<n;i++)
printf("%d ",a[i]);
printf("\n");
}


void permute(int *a, int k, int n)
{

int i = 0;
if(k==n)
{
/**Prints only if it is defined. */
#ifdef PRINT
print(a,k,n);
#endif
return;
}

for(i=k;i<n;i++)
{
/** swap the values in a[i] and a[k] */
swap(&a[i],&a[k]);
permute(a,k+1,n);
/** swap the values in a[i] and a[k] */
swap(&a[i],&a[k]);
}
}

void swap(int *a,int *b)
{
if ( a == b ) return;
*a ^= *b ^= *a ^= *b;
}

***************************************************************
The above code works great....
I want to convert this code in java ..
Guys can anyone do that for me.. please ...
 
fred rosenberger
lowercase baba
Bartender
Posts: 12186
34
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In this thread you are asking someone to "PLEASE GIVE (you) THE SOLUTION".

Now you are asking for someone to do this for you.

We're really not about doing other people's work for them. You can certainly try Rent-A-Coder and possibly pay someone there do do it.

Here, we are about helping you LEARN java. If you know C, then picking up Java shouldn't be too hard. Get a good book, and start writing code. We're happy to answer questions, but again, we do not do people's work for them.
 
Anand Hariharan
Rancher
Posts: 272
C++ Debian VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Mayur Soneta:
Guys I have a code in C where I have made...but I want to do it in Java....
Unfortunately I dont know much about java..I would request someone can help me..


<snip>


The above code works great....
I want to convert this code in java ..
Guys can anyone do that for me.. please ...


If the C code works, it is only by accident. It would not compile on my compiler, it exhibits undefined behaviour and tries to be "cute" to an extent that it just won't work.

Moving to Java would let you avoid those pitfalls. The boiler plate code is slightly different, you can fill in the meat as you please:



Just as you have been already advised, show us that you have put an effort, and the Gurus here would be more than happy to guide you in the right direction.

good luck,
- Anand
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic