objective c - NSMutableArray array isn't created -
on line marked comment "right here" (the last if statement) compiler tells me "index 0 beyond bounds empty array" interpret - array wasn't created.
the idea - in last loop i'm going sum areas of existing triangles calc areas.
nsmutablearray *xcoordinate = [nsmutablearray array]; nsmutablearray *ycoordinate = [nsmutablearray array]; // code in here... int t; int g = [xcoordinate count]; if (g<3) { printf("please enter @ least 3 value pairs form polygon\n"); return 0; } nsmutablearray *arrayofcorners = [nsmutablearray array]; nsmutablearray *arrayoftriangls = [nsmutablearray array]; (t=0; t < g; t++) { float x = [[xcoordinate objectatindex:t] floatvalue]; float y = [[ycoordinate objectatindex:t] floatvalue]; rectanglecorner *corner = [[rectanglecorner alloc] initwithx:x andy:y]; // 3. add corner array. [arrayofcorners addobject:corner]; if (t>=2) { // 4. forming triangle. triangle *triangle = [[triangle alloc] init]; // 5. calc sides length. calculate lengths , assignes values side1, side2, side3 properties of triangle. [triangle sidelengthwithvert:arrayofcorners[t] vert2:arrayofcorners[t+1] vert3:arrayofcorners[t+2]]; // 6. calc triangle area. [triangle calcarea]; // 7. adding triangle's area our array [arrayoftriangls addobject:triangle]; } // 8. adding areas of triangles (if have array of them) int = 0; nsinteger nsi = (nsinteger) i; // right here. triangle *testingtriangle = [arrayoftriangls objectatindex:nsi]; if (testingtriangle) { int y = [arrayoftriangls count]; int r; (r=0; r<=y; r++) { float p; int q = r; nsinteger ndi = (nsinteger) q; triangle *triangle = [arrayoftriangls objectatindex:ndi]; p +=triangle.area; printf("polygon's area %f", p); } } }
lets walk through code:
if (g<3) { printf("please enter @ least 3 value pairs form polygon\n"); return 0; } nsmutablearray *arrayofcorners = [nsmutablearray array]; nsmutablearray *arrayoftriangls = [nsmutablearray array]; (t=0; t < g; t++) { you create array of triangles before loop, go loop @ least g = 3.
now lets start t = 0, , go through loop:
float x = [[xcoordinate objectatindex:t] floatvalue]; float y = [[ycoordinate objectatindex:t] floatvalue]; rectanglecorner *corner = [[rectanglecorner alloc] initwithx:x andy:y]; // 3. add corner array. [arrayofcorners addobject:corner]; if (t>=2) { // 4. forming triangle. triangle *triangle = [[triangle alloc] init]; // 5. calc sides length. calculate lengths , assignes values side1, side2, side3 properties of triangle. [triangle sidelengthwithvert:arrayofcorners[t] vert2:arrayofcorners[t+1] vert3:arrayofcorners[t+2]]; // 6. calc triangle area. [triangle calcarea]; // 7. adding triangle's area our array [arrayoftriangls addobject:triangle]; } the if not true @ point, didn't add triangle yet array. triangle array contains 0 objects. lets move on:
int = 0; nsinteger nsi = (nsinteger) i; // right here. triangle *testingtriangle = [arrayoftriangls objectatindex:nsi]; now try object @ index 0, array contains 0 objects. suppose t >= 2 code works, t = 0, 1 code crashes.
Comments
Post a Comment