my C++ code works, but doesn't work, sort of.


Results 1 to 4 of 4

Thread: my C++ code works, but doesn't work, sort of.

  1. #1
    Join Date
    Feb 2001
    Location
    East Central Illinois, USA
    Posts
    523

    my C++ code works, but doesn't work, sort of.

    My C++ class assignment (this is just one of six parts) compiles, builds, and runs. The output is only half correct. Can you help me find the error. I've spent about 8 hours trying to debug this, without success.
    The first array prints out correctly. The second array is supposed to be rotated 90 degrees(the horizontal values become the vertical values, rotated to the right), but all I get is zeros. The error that eludes me is either in the rotateArray function, or the second printArray function.
    Attached Files Attached Files
    If it ain't broke, TWEAK IT

    Registered Linux user # 170078

  2. #2
    Join Date
    Sep 2002
    Posts
    767
    hi,
    i think your receiving array array2 goes out of scope
    and can't be returned from the sort method..
    i guess i'd make that method void and declare array2
    with the keyword static from the start to make the array
    persistent and available to the entire class.
    2.6 P4 w/1Gig RAM, Asus P4P 800, nvidia Ti-4200
    Tried Fedora Core 1 but performance sucked
    Built LFS 5.0 performance 100% better
    Finally compiled Kernel and Nvidia driver with Intel icc compiler, now it runs like it should have in the first place!

  3. #3
    Join Date
    Apr 2001
    Location
    Norway
    Posts
    344
    Code:
    arrayTwo[3][3] = rotateArrayOne(arrayOne);
    ...
    int rotateArrayOne(int array1[][3]);
    It's amazing the program doesn't crash at this point though.
    Your routine returns an int (a single integer value) which you then assign to a value outside of the scope of arrayTwo (remember that arrays are zero-based).

    Try changing the code to:
    Code:
    rotateArrayOne(arrayOne,arrayTwo);
    ...
    int rotateArrayOne(int array1[][3], int array2[][3]);
    And remove the array2 declaration in the function.

  4. #4
    Join Date
    Feb 2001
    Location
    East Central Illinois, USA
    Posts
    523
    Thanks, truls, that was indeed the problem. I found it while working on another part of the assignment, which also involves arrays. Fixed it there, then went back to this one and fixed it too. Works like a charm. If I'd not found it on my own, your suggestion would have fixed it for me. Thanks again.
    If it ain't broke, TWEAK IT

    Registered Linux user # 170078

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •