aeipownu
aeipownu

Reputation: 107

trouble iterating through multidimensional array

The question is to write a function that returns a bounding rectangle for a set of points in a two dimensional plane. SIZE is two. I know the points will come in this format {double, double} and I know how to create the bounding rectangle. I can't seem to grab the points though. I tried iterating like this.

Rectangle2D getRectangle(const double points[][SIZE], int s) {
for (int i = 0; i < s; i++) {
    for (int j = 0; j < SIZE; j++) {
        cout << points[s][SIZE] << endl;
    }
}
// will put these points in after i figure out the iteration.
Rectangle2D rekt(x, y, width, height);
return rekt;
}

Upvotes: 1

Views: 101

Answers (2)

Abhishek
Abhishek

Reputation: 7035

Here you go.

for (int i = 0; i < s; i++) {
    for (int j = 0; j < SIZE; j++) {
        cout << points[i][j] << endl; //observe i,j
    }
}

In above case you are iterating row-wise. If you want to iterate column-wise then following will work.

for (int j = 0; j < SIZE; j++) {
   for (int i = 0; i < s; i++) {
            cout << points[i][j] << endl; //observe i,j
        }
    }

Upvotes: 1

Leo Lindhorst
Leo Lindhorst

Reputation: 232

You are accessing the same element element every time, because s and SIZE remain constant. You have to access it like this points[i][j] . And I'm not sure, but i think you can't pass SIZE within the array argument, you should pass it as an additional parameter. Good luck ;)

Upvotes: 1

Related Questions