The Origin Forum
File Exchange
Try Origin for Free
The Origin Forum
Home | Profile | Register | Active Topics | Members | Search | FAQ | Send File to Tech support
Username:
Password:
Save Password
Forgot your Password? | Admin Options

 All Forums
 Origin Forum for Programming
 Forum for Origin C
 Odd Behaviour in For Loops
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic Lock Topic Edit Topic Delete Topic New Topic Reply to Topic

Nai

Canada
Posts

Posted - 05/30/2005 :  12:23:22 PM  Show Profile  Edit Topic  Reply with Quote  View user's IP address  Delete Topic
Origin Version (Select Help-->About Origin): Originro 7.5 SR5
Operating System: Windows 2000

For some reason I hit a general operation failure whenever I hit element v[498] WITHIN the FOR LOOP in the following subroutine:
int subMaxIndex(vector<double>& v){
printf("\n v[497] = %f", v[497]);
printf("\n v[498] = %f", v[498]); //it prints here
printf("\n v[499] = %f", v[499]); //it prints here
printf("\n v.GetSize() = %d", v.GetSize());
for(int i=0; v[i]<v[i+1] && i<499;i++){ //it does not exceed v[498 here]
printf("\n v[%d] = %f", i,v[i]);
}
if(i==v.GetSize())
return -1;
return i;
}

Notice that I can access vector elements 498 and 499 outside of the FOR loop, but not inside it in the expression 2 condition.
Odd? or is it just me!

Mike Buess

USA
3037 Posts

Posted - 05/30/2005 :  1:13:11 PM  Show Profile  Edit Reply  Reply with Quote  View user's IP address  Delete Reply
Try reversing the order of your conditions...

for(int i=0; i<499 && v[i]<v[i+1]; i++)

...I suspect that v[499] is the last element and the error occurs when the condition v[499]<v[500] is tested. Testing i first avoids that.

Mike Buess
Origin WebRing Member

Edited by - Mike Buess on 05/30/2005 1:20:09 PM
Go to Top of Page
  Previous Topic Topic Next Topic Lock Topic Edit Topic Delete Topic New Topic Reply to Topic
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
The Origin Forum © 2020 Originlab Corporation Go To Top Of Page
Snitz Forums 2000