Go Back   MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Reply
 
Thread Tools Search this Thread Display Modes
Old Mar 14, 2005, 12:43 PM   #1
Sirena400
macrumors newbie
 
Join Date: Feb 2005
Could someone look over my code please?

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. 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;
}
Sirena400 is offline   0 Reply With Quote
Old Mar 14, 2005, 12:52 PM   #2
SilentPanda
Moderator emeritus
 
SilentPanda's Avatar
 
Join Date: Oct 2002
Location: The Bamboo Forest
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.
__________________
My 24 hour web cam! ʕノᴥʔノ ︵ ┻━┻
And remember.

Last edited by SilentPanda; Mar 14, 2005 at 01:14 PM.
SilentPanda is offline   0 Reply With Quote
Old Mar 14, 2005, 01:07 PM   #3
cubist
macrumors 68020
 
Join Date: Jul 2002
Location: Muncie, Indiana
Send a message via AIM to cubist
Also you should have comments, not just for other people's sake, but for your own.
cubist is offline   0 Reply With Quote
Old Mar 14, 2005, 01:09 PM   #4
SilentPanda
Moderator emeritus
 
SilentPanda's Avatar
 
Join Date: Oct 2002
Location: The Bamboo Forest
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.
__________________
My 24 hour web cam! ʕノᴥʔノ ︵ ┻━┻
And remember.
SilentPanda is offline   0 Reply With Quote
Old Mar 14, 2005, 01:13 PM   #5
SilentPanda
Moderator emeritus
 
SilentPanda's Avatar
 
Join Date: Oct 2002
Location: The Bamboo Forest
Quote:
Originally Posted by cubist
Also you should have comments, not just for other people's sake, but for your own.
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...
__________________
My 24 hour web cam! ʕノᴥʔノ ︵ ┻━┻
And remember.
SilentPanda is offline   0 Reply With Quote
Old Mar 16, 2005, 12:43 PM   #6
Sirena400
Thread Starter
macrumors newbie
 
Join Date: Feb 2005
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?
Sirena400 is offline   0 Reply With Quote
Old Mar 16, 2005, 12:59 PM   #7
SilentPanda
Moderator emeritus
 
SilentPanda's Avatar
 
Join Date: Oct 2002
Location: The Bamboo Forest
Quote:
Originally Posted by Sirena400
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?
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.
__________________
My 24 hour web cam! ʕノᴥʔノ ︵ ┻━┻
And remember.
SilentPanda is offline   0 Reply With Quote
Old Mar 17, 2005, 12:26 PM   #8
Sirena400
Thread Starter
macrumors newbie
 
Join Date: Feb 2005
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.
Sirena400 is offline   0 Reply With Quote
Old Mar 19, 2005, 01:08 PM   #9
mwpeters8182
macrumors 6502
 
Join Date: Apr 2003
Location: Boston, MA
-- Never mind, I'm an idiot. Already been answered
__________________
2.0 GHz Aluminum MacBook
iPhone 3G
mwpeters8182 is offline   0 Reply With Quote
Old Mar 21, 2005, 01:44 AM   #10
Sirena400
Thread Starter
macrumors newbie
 
Join Date: Feb 2005
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!
Sirena400 is offline   0 Reply With Quote

Reply
MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Similar Threads
thread Thread Starter Forum Replies Last Post
Library for simple Face and QR Code/Bar Code/Etc detection DarthFoley iPhone/iPad Programming 0 Jan 26, 2014 01:11 PM
Resolved: ML code for OSX server code Processor OS X 10.8 Mountain Lion 5 Jul 28, 2012 12:51 AM
AppStore:"The code you entered is not recognized as valid code" (or Already Redeemed) sevimli OS X 10.8 Mountain Lion 223 Jul 27, 2012 05:50 AM

Forum Jump

All times are GMT -5. The time now is 12:35 AM.

Mac Rumors | Mac | iPhone | iPhone Game Reviews | iPhone Apps

Mobile Version | Fixed | Fluid | Fluid HD
Copyright 2002-2013, MacRumors.com, LLC