通过多维阵列迭代问题

trouble iterating through multidimensional array

本文关键字:迭代 问题 阵列      更新时间:2023-10-16

问题是编写一个函数,该函数在二维平面中返回一组点的边界矩形。大小是两个。我知道这些要点将以这种格式{double,double}出现,我知道如何创建边界矩形。不过,我似乎无法获得积分。我尝试了这样的迭代。

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;
}

您每次都访问相同的元素,因为S和大小保持恒定。您必须像此points[i][j]一样访问它。而且我不确定,但是我认为您不能在数组参数中传递大小,您应该将其作为附加参数传递。祝你好运;)

在这里你去。

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

在上面的情况下,您正在迭代行。如果您想迭代列,那么以下将有效。

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