Reputation: 537
On line that is marked with a comment "RIGHT HERE" (the last if statement) compiler tells me "index 0 beyond bounds for empty array" which I interpret as - the array wasn't created.
The idea is - in that last loop I'm going to sum up areas of already existing triangles with calc areas.
NSMutableArray *xCoordinate = [NSMutableArray array];
NSMutableArray *yCoordinate = [NSMutableArray array];
// some code in here...
int t;
int g = [xCoordinate count];
if (g<3) {
printf("Please enter at least 3 value pairs to form a polygon\n");
return 0;
}
NSMutableArray *arrayOfCorners = [NSMutableArray array];
NSMutableArray *arrayOfTriangls = [NSMutableArray array];
for (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 this corner to an array.
[arrayOfCorners addObject:corner];
if (t>=2) {
// 4. forming a triangle.
Triangle *triangle = [[Triangle alloc] init];
// 5. calc its sides length. Calculate lengths and assignes those values to side1, side2, side3 properties of the triangle.
[triangle sideLengthWithVert:arrayOfCorners[t] vert2:arrayOfCorners[t+1] vert3:arrayOfCorners[t+2]];
// 6. calc triangle area.
[triangle calcArea];
// 7. adding this triangle's area to our array
[arrayOfTriangls addObject:triangle];
}
// 8. adding up areas of triangles (if we have an array of them)
int i = 0;
NSInteger nsi = (NSInteger) i;
// RIGHT HERE.
Triangle *testingTriangle = [arrayOfTriangls objectAtIndex:nsi];
if (testingTriangle)
{
int y = [arrayOfTriangls count];
int r;
for (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 is %f", p);
}
}
}
Upvotes: 0
Views: 50
Reputation: 11016
If your array was not created, arrayOfTriangls
would be nil
, and calling objectAtIndex:
would return nil
and not crash.
Instead, "index 0 beyond bounds for empty array" means that you tried to access the first element of an empty (but initialized) array.
Your problem is that your code 7.
is not executed for the first two loop iteration (if t>=2
), hence the empty array.
Upvotes: 0
Reputation: 4854
Lets walk through your code:
if (g<3) {
printf("Please enter at least 3 value pairs to form a polygon\n");
return 0;
}
NSMutableArray *arrayOfCorners = [NSMutableArray array];
NSMutableArray *arrayOfTriangls = [NSMutableArray array];
for (t=0; t < g; t++) {
You create array of triangles before the loop, then go with the for loop with at least g = 3.
Now lets start with t = 0, and go through the loop:
float x = [[xCoordinate objectAtIndex:t] floatValue];
float y = [[yCoordinate objectAtIndex:t] floatValue];
RectangleCorner *corner = [[RectangleCorner alloc] initWithX:x andY:y];
// 3. add this corner to an array.
[arrayOfCorners addObject:corner];
if (t>=2) {
// 4. forming a triangle.
Triangle *triangle = [[Triangle alloc] init];
// 5. calc its sides length. Calculate lengths and assignes those values to side1, side2, side3 properties of the triangle.
[triangle sideLengthWithVert:arrayOfCorners[t] vert2:arrayOfCorners[t+1] vert3:arrayOfCorners[t+2]];
// 6. calc triangle area.
[triangle calcArea];
// 7. adding this triangle's area to our array
[arrayOfTriangls addObject:triangle];
}
The if is not true at this point, so you didn't add a triangle yet to the array. The triangle array contains now 0 objects. Lets move on:
int i = 0;
NSInteger nsi = (NSInteger) i;
// RIGHT HERE.
Triangle *testingTriangle = [arrayOfTriangls objectAtIndex:nsi];
Now you try to get the object at index 0, but the array contains 0 objects. I suppose for t >= 2 your code works, but for t = 0, 1 your code crashes.
Upvotes: 3