Thread: Calcuate single line of 2d array

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

    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
    Double Helix STL

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Um, use a for loop?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    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,688
    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]
    Double Helix STL

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    More likely:
    Code:
    for ( int k = 0; k < sizeof(myArray[0]); k++ )
    {
        rowTotal += myArray[0][k];
    }
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    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,688
    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.
    Double Helix STL

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    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;
    }
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    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
    28,413
    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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    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, 10:33 AM
  2. Replies: 2
    Last Post: 07-11-2008, 07: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, 09:57 PM
  5. how to pass 2D array into function..?
    By IngramGc in forum C++ Programming
    Replies: 2
    Last Post: 10-21-2001, 08:41 AM