This week's book giveaways are in the Java EE and JavaScript forums.
We're giving away four copies each of The Java EE 7 Tutorial Volume 1 or Volume 2(winners choice) and jQuery UI in Action and have the authors on-line!
See this thread and this one for details.
The moose likes Beginning Java and the fly likes Convert C code in Java Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Convert C code in Java" Watch "Convert C code in Java" New topic
Author

Convert C code in Java

Mayur Soneta
Greenhorn

Joined: Jan 04, 2006
Posts: 11
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

Joined: Oct 02, 2003
Posts: 11254
    
  16

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.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Anand Hariharan
Rancher

Joined: Aug 22, 2006
Posts: 257

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


"Perfection is achieved, not when there is nothing more to add, but when there is nothing left to take away." -- Antoine de Saint-Exupery
 
jQuery in Action, 2nd edition
 
subject: Convert C code in Java