CS 70

Before You Start

Say we'd like to write a function that prints out the ages of the Cows in an array.

void printAges(const Cow* firstCow, const Cow* pastTheEnd) {
    // Your job!
}

Can you write a for loop that would accomplish this?

(If you are unsure, consider reviewing the “Past-the-End Pointers” page of Lesson 1)

Write the body of the function here!

Here's our implementation:

void printAges(const Cow* firstCow, const Cow* pastTheEnd) {
    for (const Cow* p = firstCow; p < pastTheEnd; ++p) {
        cout << p->getAge() << endl;
    }
}

Let's walk through it with the following main function:

int main() {
    Cow cows[2];

    Cow bessie{4, 2};
    cows[0] = bessie;
    Cow buttercup{3, 14};
    cows[1] = buttercup;

    printAges(cows, cows+2);
    return 0;
}

(When logged in, completion status appears here.)