#include using namespace std; struct array_of_four { int v1, v2, v3, v4; }; // v1 is supposed to represent A[0], v2 is A[1], v3 is A[2], v4 is A[3] int & access(array_of_four & A, int pos) { if (pos < 0 || pos > 3) { cout << "Index to array_of_four is out of bounds\n"; exit(1); } if (pos == 0) return A.v1; if (pos == 1) return A.v2; if (pos == 2) return A.v3; return A.v4; } int main() { int a = 111, b = 222, c = 333, d = 444, e = 555, f = 666; array_of_four R; int z = 11, y = 22, x = 33, w = 44, v = 55, u = 66, t = 77; int start, stop; cout << "\nVariables before accessing the array:\n"; cout << " a = " << a << "\n"; cout << " b = " << b << "\n"; cout << " c = " << c << "\n"; cout << " d = " << d << "\n"; cout << " e = " << e << "\n"; cout << " f = " << f << "\n"; cout << " z = " << z << "\n"; cout << " y = " << y << "\n"; cout << " x = " << x << "\n"; cout << " w = " << w << "\n"; cout << " v = " << v << "\n"; cout << " u = " << u << "\n"; cout << " t = " << t << "\n"; /* this represents a really big computation that went wrong */ start = 0; stop = 3; for (int i = start; i <= stop; i += 1) access(R, i) = 1000 + i; /* pretend to use the array here */ cout << "The array conatins:\n"; cout << " R[0] = " << access(R, 0) << "\n"; cout << " R[1] = " << access(R, 1) << "\n"; cout << " R[2] = " << access(R, 2) << "\n"; cout << " R[3] = " << access(R, 3) << "\n"; cout << "\nVariables after accessing the array:\n"; cout << " a = " << a << "\n"; cout << " b = " << b << "\n"; cout << " c = " << c << "\n"; cout << " d = " << d << "\n"; cout << " e = " << e << "\n"; cout << " f = " << f << "\n"; cout << " z = " << z << "\n"; cout << " y = " << y << "\n"; cout << " x = " << x << "\n"; cout << " w = " << w << "\n"; cout << " v = " << v << "\n"; cout << " u = " << u << "\n"; cout << " t = " << t << "\n"; }