1. Welcome to the new MacRumors forums. See our announcement and read our FAQ

Could someone look over my code please?

Discussion in 'Mac Programming' started by Sirena400, Mar 14, 2005.

  1. macrumors newbie

    #1
    I thought at first that my code was okay but then when I actually tried running it I got an illegal vector index: 6 max index = 5 as a message. :confused: I've tried fixing it but I can't seem to find the problem. Can any of you guys figure out where I went wrong? Here's the code, and please excuse the poor programming, I'm not very good ^^;

    #include <iostream.h>
    #include <math.h>
    #include "apvector.h"
    apvector<double>scores(6);
    apvector<double>darts(6);
    int main()
    {
    double distance=0;
    double location=0;
    double total=0;
    double final=0;
    double q=0;
    double p=0;

    for (int x=0;x<scores.length();x++)
    {
    cin>>scores[x];
    }
    for (int y=0;y<darts.length();y++)
    {
    cin>>darts[x];

    }
    for (x=0;x<scores.length();x+=2)
    {
    if (scores[x]>0 && scores[x+1]>0)
    q=4;
    else if (scores[x]<0 && scores[x+1]>0)
    q=3;
    else if (scores[x]<0 && scores[x+1]<0)
    q=2;
    else (scores[x]>0 && scores[x+1]<0);
    q=1;
    }
    for (y=0;y<darts.length();y+=2)
    {
    if (darts[y]>0 && darts[y+1]>0)
    p=4;
    else if (darts[y]<0 && darts[y+1]>0)
    p=3;
    else if (darts[y]<0 && scores[y+1]<0)
    p=2;
    else (darts[y]>0 && darts[y+1]<0);
    p=1;
    }
    distance=sqrt((scores[x]*scores[x])+(scores[x+1]*scores[x+1]));
    location=sqrt((darts[y]*darts[y])+(darts[y+1]*darts[y+1]));

    if (distance<=7)
    {
    total=total+10;
    }
    else if (distance<=15 && distance>7)
    {
    total=total+7;
    }
    else if (distance<=55 && distance>15)
    {
    total=total+q;
    }
    else if (distance<=59 && distance>55)
    {
    total=total+(q*3);
    }
    else if (distance<=85 && distance>59)
    {
    total=total+q;
    }
    else if (distance<=89 && distance>85)
    {
    total=total+(q*2);
    }
    else (distance>89);
    {
    total=total+0;
    }


    if (location<=7)
    {
    final=final+10;
    }
    else if (location<=15 && location>7)
    {
    final=final+7;
    }
    else if (location<=55 && distance>15)
    {
    final=final+p;
    }
    else if (location<=59 && distance>55)
    {
    final=final+(p*3);
    }
    else if (location<=85 && location>59)
    {
    final=final+q;
    }
    else if (location<=89 && location>85)
    {
    final=final+(q*2);
    }
    else (location>89);
    {
    final=final+0;
    }


    if (total>final)
    {
    cout<<"First player wins, "<<total<<"-"<<final<<".";
    }

    if (final>total)
    {
    cout<<"Second player wins, "<<final<<"-"<<total<<".";
    }

    if (final=total)
    {
    cout<<"Tie game. "<<final<<"-"<<total<<".";
    }

    return 0;
    }
     
  2. Moderator emeritus

    SilentPanda

    #2
    Edit: This isn't the issue but I'll leave it here anyhow... the for loop is x+=2 whereas normally I just assume loops are x++....

    -------------------------

    It's where you have [x+1] and [y+1].

    Your loops go from 0 to < array.length. When you add 1 to it you are essentially going to array.length instead of 1 less than.

    Also you might consider using the "code" bb tag when pasting code as it makes it easier to read.
     
  3. macrumors 68020

    #3
    Also you should have comments, not just for other people's sake, but for your own.
     
  4. Moderator emeritus

    SilentPanda

    #4
    Ack! The [X+1] and [Y+1] isn't the problem.

    The problem is after your two loops. Your X and Y values will be 6. Then you are trying to access the array with those values...

    Your loop goes x=0, x=2, x=4, then it makes x=6 and breaks out of the loop.

    Same with y.

    Then right under that you access the arrays using x=6 and y=6.

    There is your problem.

    I don't normally read "for" loops, I just usually assume they're x++ instead of x+=2... :) so your for loop is good.
     
  5. Moderator emeritus

    SilentPanda

    #5
    I agree even though... I don't... :( I know I should and I could tell you why I don't but it's not valid anyhow... usually I do for large projects though...
     
  6. macrumors newbie

    #6
    Sorry about not adding more comments, I didn't know that I should've added some or else I would've done that. I'll make sure to do that next time. SilentPanda, thanks for the help, but I'm don't quite get what you're telling me. What part of my code accesses the arrays using x=6 and y=6 and why can't I do that? Is it because I'm trying to access an array that was already broken out of?
     
  7. Moderator emeritus

    SilentPanda

    #7
    I'll make a shorter example (I code in Java primarily but it's pretty much the same for what you're doing).

    Code:
    int[] iArray = new int[6];  // makes an array with index of 0 through 5
    int X = 0;
    int iSomeVariable = 0;
    
    // just some for loop stuff
    for (X = 0; X < iArray.length; X+=2) {
      iArray[X]++;
      iArray[X + 1]+=2;
    }
    
    // at this point X = 6.  This is where the issue lies.
    iSomeVariable = iArray[X] + iArray[X + 1];
    // this is actually iSomeVariable = iArray[6] + iArray[7];
    
    
    If you look in your code you are doing just this after your for loop for the variable y. You are calculating distance and location and going out of bounds on your index. Here is how the above for loop would process.

    X = 0
    Is X < iArray.length (which is 6)?
    Yes, do inside of the loop.
    X+=2 (now is 2)
    Is X < iArray.length (which is 6)?
    Yes, do inside of the loop.
    X+=2 (now is 4)
    Is X < iArray.length (which is 6)?
    Yes, do inside of the loop.
    X+=2 (now is 6)
    Is X < iArray.length (which is 6)?
    No! Leave.

    I think you are not realizing the +=2 happens and then the loop exits.
     
  8. macrumors newbie

    #8
    Ah I see, thank you SilentPanda. I get what my problem is. Now I just have to figure a way to fix it. Thanks a lot for the help! That example really helped me a lot, thanks again.
     
  9. macrumors 6502

    #9
    -- Never mind, I'm an idiot. Already been answered
     
  10. macrumors newbie

    #10
    thanks for the consideration anyways mwpeters8182 :) and the really good news is i finished the program and it works great! thanks everyone for your help!
     

Share This Page