C: Why do the total sum of an index of an array is way off?

Discussion in 'Mac Programming' started by jsmwoolf, Sep 17, 2011.

  1. jsmwoolf, Sep 17, 2011
    Last edited: Sep 24, 2011

    jsmwoolf macrumors regular

    Joined:
    Aug 17, 2011
    #1
    I'm trying to solve a problem from a website called Project Euler. The thing is that I already have a way how to solve it, but the problem is that the array "Number[1000]" keeps producing weird numbers to the millions per index even though it's supposed to only track numbers that have a sum less than or equal to 1,000 and then stores them to a index minus one.

    Here's my code:
    Code:
    //Removed
    How come this is happening?
     
  2. dmi macrumors regular

    Joined:
    Dec 21, 2010
    #2
    the elements of Number are uninitialized. You want to start by setting them to 0
     
  3. jsmwoolf thread starter macrumors regular

    Joined:
    Aug 17, 2011
    #3
    Thanks, it now works. However, how come it gave random numbers? Was it because if you don't initialize it, the variable just grabs a random number from the memory?
     
  4. lee1210, Sep 17, 2011
    Last edited: Sep 17, 2011

    lee1210 macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
  5. reynaldom macrumors newbie

    Joined:
    Jun 22, 2010
    Location:
    Monterrey, MX
    #5
    yep, if you don't initialize those variables they'll just grab anything allocated in that space of memory.
     
  6. dmi macrumors regular

    Joined:
    Dec 21, 2010
    #6
    the method used to generate perimeters will never generate 30+40+50
     
  7. itickings macrumors 6502a

    itickings

    Joined:
    Apr 14, 2007
    #7
    Another vote for rechecking the perimeter math.

    You might want to try a naive exhaustive approach to get the correct set of answers for comparison.
     
  8. jsmwoolf thread starter macrumors regular

    Joined:
    Aug 17, 2011
    #8
    This morning, I found out why it didn't correctly solve the problem before I typed on this post. I found something on wikipedia that allowed you to find all the pythagorean triples with another constant.
    [​IMG]
    However, I don't know how effective this is exactly because I never see it anywhere else. This might be a B.S. statement. The rule can be found here.
    http://en.wikipedia.org/wiki/Pythagorean_triple
     
  9. itickings macrumors 6502a

    itickings

    Joined:
    Apr 14, 2007
  10. jsmwoolf, Sep 18, 2011
    Last edited: Sep 24, 2011

    jsmwoolf thread starter macrumors regular

    Joined:
    Aug 17, 2011
    #10
    However, is there a more effective way on how to find all of then without finding repeats?

    EDIT: Never mind. I solved it and got the correct answer. It's now removed to prevent cheating.
     

Share This Page