Register FAQ / Rules Forum Spy Search Today's Posts Mark Forums Read
Go Back   MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Reply
 
Thread Tools Search this Thread Display Modes
Old Mar 14, 2005, 11:43 AM   #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, 11:52 AM   #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!

Last edited by SilentPanda; Mar 14, 2005 at 12:14 PM.
SilentPanda is offline   0 Reply With Quote
Old Mar 14, 2005, 12: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, 12: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!
SilentPanda is offline   0 Reply With Quote
Old Mar 14, 2005, 12: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!
SilentPanda is offline   0 Reply With Quote
Old Mar 16, 2005, 11:43 AM   #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, 11:59 AM   #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!
SilentPanda is offline   0 Reply With Quote
Old Mar 17, 2005, 11:26 AM   #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, 12: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, 12: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

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
thread Thread Starter Forum Replies Last Post
can someone look at my battery usage pls kikeboy iPhone Tips, Help and Troubleshooting 3 Apr 11, 2011 09:43 AM
Could someone help assess my situation? Soon to be college student MB98XJ MacBook Pro 20 Jul 3, 2009 11:26 PM
Could someone clarify this for me please sk8mash Jailbreaks and iOS Hacks 14 Mar 15, 2009 10:01 PM
Look at my code, please moea Web Design and Development 3 Mar 13, 2008 02:44 PM
Can someone look over my C++ code please? Sirena400 Mac Programming 1 Feb 22, 2005 09:13 PM


All times are GMT -5. The time now is 11:30 PM.

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

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