# Thread: From Java to C

1. ## From Java to C

I got a code written in Java. But, I gave up writing code in Java. The program written is supposed to find the maximum subsequence sum. It's originally like this.

Code:
```private static int maxSumRec (int [] a, int left, int right)
{
if(left == right)
if(a[left > 0])
return arr[left];
else
return 0;
int center = (left + right)/2;
int maxLeftSum = maxSumRec(a, left, center);
int maxRightSum = maxSumRec(a, center + 1, right);

int maxLeftBorderSum = 0, leftBorderSum = 0;
for(int i = center; i>=left; i--)
{
leftBorderSum += arr[i];
if(leftBorderSum > maxLeftBorderSum)
maxLeftBorderSum = leftBorderSum;
}

int maxRightBorderSum =0, rightBorderSum = 0;
for(int i = center+1; i<=right ; i++)
{
rightBorderSum += arr[i];
if(rightBorderSum > maxRightBorderSum)
maxRightBorderSum = rightBorderSum;
}

return max3(maxLeftSum, maxRightSum, maxLeftBorderSum + maxRightBorderSum);
}

public static int maxSubSum3(int []a)
{
return maxSumRec(a, 0, a.length-1);
}```
I turned it into C, add some elements (to generate random numbers and change some variables' names), and becomes like this

Code:
```int maxSumRec (val, left, right)
{
int x;
long int arr[val];
srand ( time(NULL) );
for(x=0; x<val; x++)
{
arr[x] = rand() % 20 * -1 + 10;
printf("%d\t", arr[x]);
}
if(left == right)
if(arr[left > 0])
return arr[left];
else
return 0;
int center = (left + right)/2;
int maxLeftSum = maxSumRec(val, left, center);
int maxRightSum = maxSumRec(val, center + 1, right);

int maxLeftBorderSum = 0, leftBorderSum = 0, i;
for(i = center; i>=left; i--)
{
leftBorderSum += arr[i];
if(leftBorderSum > maxLeftBorderSum)
maxLeftBorderSum = leftBorderSum;
}

int maxRightBorderSum =0, rightBorderSum = 0;
for(i = center+1; i<=right ; i++)
{
rightBorderSum += arr[i];
if(rightBorderSum > maxRightBorderSum)
maxRightBorderSum = rightBorderSum;
}

return max3(maxLeftSum, maxRightSum, maxLeftBorderSum + maxRightBorderSum);
}```

It fails to compile. What have I done wrong? And I keep wondering why in the original code there is left and right variables and their values are never assigned. My c compiler (I use codeblocks) keeps telling me that. Idk why. My friend who keeps it in Java says it is fine but he cannot explain how his program works. What *is* left and right actually? I got the code from book and my teacher didn't explain anything.

Can someone help me?

Thanks. Move "srand ( time(NULL) );" to the main function till you find a better spot for it.

Tim S. 3. Originally Posted by stahta01 Move "srand ( time(NULL) );" to the main function till you find a better spot for it.

Tim S.
This is what my compiler says to the line in which I try to call the function above.

error: expected expression before 'int'
error: too few arguments to function maxSumRec.

But, I think I have put the srand() function in the right place :/ 4. This is what my compiler says to the line in which I try to call the function above.
In that case, we'd have to see the calling code. Your best bet might be to create a simple, complete, compilable example in which you call the function, so that we can see what might be going wrong.

Code:
`int maxSumRec (val, left, right)`
Your function definition is missing the data types for the arguments. 5. Originally Posted by Matticus In that case, we'd have to see the calling code. Your best bet might be to create a simple, complete, compilable example in which you call the function, so that we can see what you might be doing wrong.

Code:
`int maxSumRec (val, left, right)`
Your function definition is missing the data types for the arguments.
I have tried to include the data types, and the program still does not work. I believe it's because the right and left variables are not assigned yet. But I may be wrong too. 6. Well, unless you provide a simplified example that demonstrates the problem, as requested above, then there is little we can do other than guess what the problem is. 7. Originally Posted by Matticus Well, unless you provide a simplified example that demonstrates the problem, as requested above, then there is little we can do other than guess what the problem is.
Honestly, I don't really get what you mean (I am still learning english too by the way).

I just found out a forum which has similar problem with mine Max SubSequence using recursion. But, when I put the code in my compiler (and add the main function too as well as change some lines because I am using c), it does not work. I know it's c++ but I don't see it really matters here. I still wonder why 'left' and 'right' are never assigned a value. Do you know why? 8. Honestly, I don't really get what you mean (I am still learning english too by the way).
In post #3, you mentioned compiling code that contains that function. Post that code, along with any warnings/errors you receive.

I just found out a forum which has similar problem with mine Max SubSequence using recursion. But, when I put the code in my compiler (and add the main function too as well as change some lines because I am using c), it does not work. I know it's c++ but I don't see it really matters here.
You seem fond of finding existing code and modifying it to suit your needs. Based on your questions so far, it would seem you're relatively inexperienced with programming. Though there's nothing wrong with being inexperienced, it would be in your best interest to master the basics by writing code yourself, rather than trying to modify code you can't figure out in the first place.

I still wonder why 'left' and 'right' are never assigned a value. Do you know why?
They are "assigned" values by the caller - the code that calls the function provides the arguments that give those variables their values.  Popular pages Recent additions 