# Calcuate single line of 2d array

• 11-21-2007
swgh
Calcuate single line of 2d array
Hey guys.

None of my books tell me how to do this so im wondering if its even possible, but say i have an array like this:

Code:

```int myArray[4][4] = { { 1,2,3,4 }, { 5, 6, 7, 8 }, { 7, 10, 11, 12 }, { 13, 14, 15, 16 } };```
Its it possible to calculate and find the total of only the top row?

I know I can do it via an index like this:

Code:

`int totalRow = myArray[0][0] + myArray[0][1] ...etc`
But is there a simpler way to do that, as if I have a large array with say over 10
columns in size I really do not want to create a huge expression per row.

Any advice appreiciated guys :)
• 11-21-2007
laserlight
Um, use a for loop?
• 11-21-2007
swgh
Quote:

Originally Posted by laserlight
Um, use a for loop?

Thanks Laserlight I nearly have this sussed. Say I want to calculate the top row of the above array, I did this:

Code:

```for ( int k = myArray[0][0]; k <= myArray[0][3]; k++ ) {   rowTotal += k; } cout << "Row total is: " << rowTotal << endl;```
But its giving me the output of 9 when it should be 10. Im starting at the position [0][0]
as I should do, and im certain my ending loop index is also correct. Row[0] column[3]
• 11-21-2007
laserlight
More likely:
Code:

```for ( int k = 0; k < sizeof(myArray[0]); k++ ) {     rowTotal += myArray[0][k]; }```
• 11-21-2007
swgh
I thought sizeof returned the bit-size of a variable ( eg: integer is 2 bit ). Im getting the answer of 133.

Its surely not as hard as im making it out ot be.
• 11-21-2007
laserlight
Quote:

I thought sizeof returned the bit-size of a variable ( eg: integer is 2 bit ).
No, it returns the number of bytes, not bits.

Quote:

Im getting the answer of 133.
That's because I forgot to divide by sizeof(int), thus the array is accessed out of bounds.

Try:
Code:

```#include <iostream> int main() {     int myArray[4][4] = {         { 1,2,3,4 },         { 5, 6, 7, 8 },         { 7, 10, 11, 12 },         { 13, 14, 15, 16 }         };     int rowTotal = 0;     unsigned int size = sizeof(myArray[0]) / sizeof(int);     for (unsigned int k = 0; k < size; ++k)     {         rowTotal += myArray[0][k];     }     std::cout << rowTotal << std::endl; }```
• 11-21-2007
matsp
Code:

```for ( int k = 0; k < sizeof(myArray[0]) / sizeof(myArray[0][0]); k++ ) {     rowTotal += myArray[0][k]; }```
sizeof gives the BYTE-size [1] of the type you give sizeof.

[1] Technically, the smalles addressable unit, which is what sizeof returns, could be something other than an 8-bit byte, but that's only on rare and unusual architectures, not your common PC or workstation system.

--
Mats
• 11-21-2007
laserlight
Quote:

Technically, the smalles addressable unit, which is what sizeof returns, could be something other than an 8-bit byte, but that's only on rare and unusual architectures, not your common PC or workstation system.
True on a technicality, yet also by technicality such a unit is still called a byte by the C++ Standard.