Solved

Sum of row using two pointers...

Posted on 2004-11-01
167 Views
Last Modified: 2010-05-18
I am trying to get the sum of the rows in a multi dim array using two pointers, one to the first element and one to the last element. I tried a few things but can't get it to work. I am trying this via a function call. Here is what i have...


int table[numOfRows][numOfCols] =      {
                                                            {1, 19, 18},
                                                            {17, 16, 15},
                                                            {14, 113, 12},
                                                            {11, 10,  9}
                                                            };

int result2[numOfRows];
    cout << "Summing row 0 to 3...\n";
    sumRowTwoPtr(table, table + sizeof table / sizeof *table, result);
    for (int ii = 0; ii < numOfRows;ii++)
      {
         cout << "Total of Row " << ii +1 << ": " << result[ii] << '\n';
      }

void sumRowTwoPtr(int (*first)[numOfCols],int (*last)[numOfCols],int result[numOfRows])
{
     // Initialise result
     for (int i = 0;i < numOfCols;i++)
       {
          result[i] = 0;
       }

      // Iterate through 2D table
     while (first <= last)
       {
          for (int i = 0; i < numOfRows; i++)
              {
                    for (int zz = 0; zz < numOfCols; zz++)
                    {
                          result[zz] += (*first)[zz];
                    }
              }
          first++;
     }
}


Any ideas..thanks
0
Question by:KalluMama
    3 Comments
     
    LVL 3

    Expert Comment

    by:Indrawati
    void sumRowTwoPtr(int (*first)[numOfCols],int (*last)[numOfCols],int result[numOfRows])
    {
         // Initialise result
         for (int i = 0;i < numOfRows;i++)
          {
              result[i] = 0;
          }

          // Iterate through 2D table
              for (i = 0; i < numOfRows; i++)
               {
                     for (int zz = 0; zz < numOfCols; zz++)
                     {
                          result[i] += (*first)[zz];
                     }
                 ++first;
           }
    }

    If you modify your sumRowTwoPtr function as above, you'll get the correct result. You can also get rid of the int (*last)[numOfCols] parameter for the function.
    0
     
    LVL 17

    Expert Comment

    by:rstaveley
    For what it's worth, here's a way to do this with the standard library's accumulate function:

    --------8<--------
    #include <iostream>
    #include <numeric>

    const int numOfCols = 3;
    int table[][numOfCols] = {
            {1, 19, 18},
          {17, 16, 15},
          {14, 113, 12},
          {11, 10,  9}
          };
    const int numOfRows = sizeof(table)/sizeof(int[numOfCols]);

    int main()
    {
          typedef int Row[numOfCols];
          for (Row *row = table;row < table+numOfRows;++row)
                std::cout << "Sum of row " << std::accumulate(&(*row)[0],&(*row)[numOfCols],0) << '\n';
    }
    --------8<--------

    I'm sure there's a less ugly construct than &(*row)[0]...
    0
     
    LVL 55

    Accepted Solution

    by:
    Have tested this shorter solution:

    int table[numOfRows][numOfCols] =     { {1, 19, 18}, {17, 16, 15}, {14, 113, 12}, {11, 10,  9} };
    int result[numOfRows];

    int (*first)[numOfCols] = &table[0];                      /* more readable declaration */
    int (*last)[numOfCols]  = &table[numOfRows-1];

        cout << "Summing row 0 to " << numOfRows << "...\n";
        sumRowTwoPtr(first, last, result);
        for (int ii = 0; ii < numOfRows;ii++)
         {
             cout << "Total of Row " << ii +1 << ": " << result[ii] << '\n';
         }

    void sumRowTwoPtr(int (*first)[numOfCols],int (*last)[numOfCols],int result[numOfRows])
    {
          // Iterate through 2D table
         for (int i = 0; first <= last; i++, first++) {   /* a for loop is more elegant */
                    result[i] = 0;        /* don't need a separated loop to initialize */
                    for (int zz = 0; zz < numOfCols; zz++)
                          result[i] += (*first)[zz];
         }
    }


    0

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Cisco Complete Network Certification Training

    If you’re an IT engineer or technician, it's time you take your career to the next level. This elite training bundle is brimming with all of the information you need to learn to sit for Cisco CNNA, CCNP, and CCENT certification exams.

    When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
    Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
    The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
    The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

    884 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    19 Experts available now in Live!

    Get 1:1 Help Now