Turing to C++ Program Problem

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • orangemonkey
    New Member
    • Apr 2007
    • 16

    Turing to C++ Program Problem

    Hi guys. I'm practicing for the CCC, a programming competition. I got stuck on a problem, so I copied the problem's answer from turing into C++. When I run my program in C++ though, I get a different answer from the one in turing. I've been spending hours trying to find the difference between the turing answer and my C++ answer, but to no avail. So basically, I'm asking what's the difference between the turing and C++ program?

    Correct Answer in Turing
    Code:
    function between (a, b, c : int) : boolean
        result a <= b and b <= c
    end between
    
    % this recursively tries every route to the end (7000)
    procedure findPath (distance : int)
        if distance = 7000 then
            % made it to the end!
            % One more way to get there
            ways := ways + 1
            put ways
        
        else
            % recursively try all motels that you can get to
            for i : 1 .. size
    put i
                if between (minn, motel (i) - distance, maax) then
                    findPath (motel (i))
                end if
            end for
        end if
    end findPath
    My wrong answer in C++.
    Code:
    int truck(int distance)
    {if (distance == 7000)
    { pos=pos+1; cout<< pos << "pos"; }
    else
    for (int i=1;i<=motels.size();i++)
    { 
    cout << i;
    
    if(minn <= motels[i] - distance && maxx >= motels[i] - distance)
    {truck(motels[i]);}
    
     }
  • Banfa
    Recognized Expert Expert
    • Feb 2006
    • 9067

    #2
    What is the type of motels?

    Have you forgotten that C/C++ arrays index from 0?

    Comment

    • orangemonkey
      New Member
      • Apr 2007
      • 16

      #3
      Thanks for the advice!

      I think I've found the problem and it's different from what I described here, it's actually a different problem. The 2 codes are exactly the same, but I think Dev C++ Bloodshed has problems with recursion. I tracked the values of recursion for the turing and C++ one, they are the same numbers except the C++ starts getting different values after 20 numbers or so and stops early. I've heard of related problems with recursion where there are too much numbers for the IDE to keep track of, some stack/heap problem or the like. So, the code are exactly the same, and it's not my fault :). I'm going to a new IDE and the problem will be solved.

      Comment

      Working...