What is COUT doing with these integers?

c++, integer, variables

Solution

It's not adding, and it's not multiplying - it's indexing. You have a two dimensional array, and element `2` of row `0` contains the number `3`.

Problem

I'm using cplusplus.com and C++ Programming In Easy Steps by Mike McGrath to accomplish a lifelong goal of doing what you guys do all the time. I'm understanding and learning, but I've reached a question I cannot seem to answer, and it is most likely because of the way I'm asking. In the book, we have an example ``` #include <iostream> using namespace std ; int main() { float nums[3] ; // Declared then initialized. nums[0] = 1.5 ; nums[1] = 2.75 ; nums[2] = 3.25 ; // Declared and initialized. char name[5] = { 'm', 'i', 'k', 'e', '\0' } ; int coords[2] [3] = { { 1, 2, 3 } , { 4, 5, 6 } } ; cout << "nums[0]: " << nums[0] << endl ; cout << "nums[1]: " << nums[1] << endl ; cout << "nums[2]: " << nums[2] << endl ; cout << "name[0]: " << name[0] << endl ; cout << "Text string: " << name << endl ; cout << "coords[0][2]: " << coords[0][2] << endl ; cout << "coords[1][2]: " << coords[1][2] << endl ; return 0 ; } ``` Now, I understand all the code used here, but what I don't understand is how the last two `cout`s work. So what we've done here, if I am understanding right, is define `coords` (co-ordinates) as int `coords[2] [3] = { { 1, 2, 3 } , { 4, 5, 6 } }` ;. Right. And now we're outputting data from it, right? Okay, so we say [0][2] and that, if added, would equal five. But 3 is the output. So my first assumption was that `cout` must be instead multiplying the two ints. But then on the second one, we see that 1 and 2 are respectively 2 and 3, and when they are multiplied they equal six. So far so good. But then, I find, if I change 6 to 9, the output is ... 9. So, what's going on here? What is COUT doing here?

Original source