Calcuate single line of 2d array

This is a discussion on Calcuate single line of 2d array within the C++ Programming forums, part of the General Programming Boards category; Hey guys. None of my books tell me how to do this so im wondering if its even possible, but ...

  1. #1
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,479

    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
    I'm just trying to be a better person - My Name Is Earl

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,265
    Um, use a for loop?
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,479
    Quote Originally Posted by laserlight View Post
    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]
    I'm just trying to be a better person - My Name Is Earl

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,265
    More likely:
    Code:
    for ( int k = 0; k < sizeof(myArray[0]); k++ )
    {
        rowTotal += myArray[0][k];
    }
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,479
    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.
    I'm just trying to be a better person - My Name Is Earl

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,265
    I thought sizeof returned the bit-size of a variable ( eg: integer is 2 bit ).
    No, it returns the number of bytes, not bits.


    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;
    }
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    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
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,265
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. from 2D array to 1D array
    By cfdprogrammer in forum C Programming
    Replies: 17
    Last Post: 03-24-2009, 11:33 AM
  2. Replies: 2
    Last Post: 07-11-2008, 08:39 AM
  3. Dynamic 2d array
    By Mithoric in forum C++ Programming
    Replies: 8
    Last Post: 12-29-2003, 09:19 AM
  4. Replies: 6
    Last Post: 10-21-2003, 10:57 PM
  5. how to pass 2D array into function..?
    By IngramGc in forum C++ Programming
    Replies: 2
    Last Post: 10-21-2001, 09:41 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21