CS 70

Array practice (optional)

In groups of 2-3, take a few minutes to draw a memory diagram for the following program. When you reach the point marked "Return to discuss", stop what you're doing and wait for everyone else to catch up to that point. Then, compare answers as a group. If there are any disagreements, try to resolve them and reach the correct answer!

#include <iostream>

int main() {
    constexpr size_t size = 3;
    int x = 2;
    int a[size];
    for (size_t i = 0; i < size; ++i) {
        *(a + i) = i * x;
    }

    // Return to discuss!

    int y = 1;
    for (size_t i = 0; i < size; ++i) {
        a[i] = a[i + y];
    }

    std::cout << *a << std::endl;
    std::cout << *(a + 2) << std::endl;
}

(When logged in, completion status appears here.)