Thread: From Java to C

  1. #1
    Registered User soawperL25's Avatar
    Join Date
    Dec 2013
    Location
    Middle-Earth
    Posts
    18

    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.

  2. #2
    Registered User
    Join Date
    May 2009
    Posts
    3,863
    Post your compiler errors.

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

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  3. #3
    Registered User soawperL25's Avatar
    Join Date
    Dec 2013
    Location
    Middle-Earth
    Posts
    18
    Quote Originally Posted by stahta01 View Post
    Post your compiler errors.

    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. #4
    Registered User
    Join Date
    Jun 2011
    Posts
    4,509
    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. #5
    Registered User soawperL25's Avatar
    Join Date
    Dec 2013
    Location
    Middle-Earth
    Posts
    18
    Quote Originally Posted by Matticus View Post
    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. #6
    Registered User
    Join Date
    Jun 2011
    Posts
    4,509
    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. #7
    Registered User soawperL25's Avatar
    Join Date
    Dec 2013
    Location
    Middle-Earth
    Posts
    18
    Quote Originally Posted by Matticus View Post
    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. #8
    Registered User
    Join Date
    Jun 2011
    Posts
    4,509
    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.

  9. #9
    Registered User
    Join Date
    May 2009
    Posts
    3,863
    Please read this link it in many languages.
    How To Ask Questions The Smart Way

    Try searching for a copy in your language.
    Because without you giving us the information needed; we can not help you.

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. for those who also know java please help
    By -EquinoX- in forum C Programming
    Replies: 2
    Last Post: 03-17-2008, 09:40 PM
  2. Multiple Java files for one Java project
    By doubleanti in forum Windows Programming
    Replies: 2
    Last Post: 11-22-2004, 02:06 AM
  3. new to java
    By xddxogm3 in forum Tech Board
    Replies: 11
    Last Post: 06-07-2004, 02:25 PM
  4. Maybe about JAVA?
    By deduct in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 05-14-2003, 04:38 AM
  5. help with java
    By qwertiop in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 09-10-2002, 05:08 PM