int numberArray [9][11];
write a statement that assigns 145 to the first colum of the first row of this array.
write a statement that assigns 18 to the last col of the last row of this array

this is what i have

numberArrray [1][1] = 148;
numberArray [9][11] = 18;

Dani AI

Generated

Quick clarification and a couple of practical tips.

The declaration int numberArray[9][11]; creates nine rows and eleven columns. As pointed out, C and C++ use 0-based indexing: the first row/column use index zero and the last valid index is size minus one. The original post also contained a likely typo in the variable name (an extra r) which would cause a compiler error if left unchanged.

A safe way to compute first/last indices programmatically (so magic numbers are avoided) is to derive row and column counts and subtract one for the last index:

size_t rows = sizeof(numberArray) / sizeof(numberArray[0]);
size_t cols = sizeof(numberArray[0]) / sizeof(numberArray[0][0]);

size_t firstRow = 0, firstCol = 0;
size_t lastRow = rows - 1, lastCol = cols - 1;

numberArray[firstRow][firstCol] = 145;
numberArray[lastRow][lastCol] = 18;

Notes and cautions: C++ does not check bounds at runtime — writing outside the valid range is undefined behavior. For safer code, prefer std::array (fixed size) or std::vector and use .at() for range-checked access, or keep row/column counts in named constants to avoid off-by-one mistakes. The memory layout is row-major, so iterating rows outermost is usually the fastest for locality. Good catch by ; ’s confirmation is in line with that.

Recommended Answers

All 4 Replies

remember that an array's max index is always 1 less than its size, since 0 is included. So for part 1,

numberArray[0][0] = 148;

and #2

numberArray [8][10] = 18;

Above answer is exactly correct ............


please follow that one...........

thanks!!! totally forgot that! :D

Above answer is exactly correct ............


please follow that one...........

Lol...thanks for the insightful contribution.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.