Sum of row using two pointers...

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
KalluMamaAsked:
Who is Participating?
 
Jaime OlivaresSoftware ArchitectCommented:
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
 
IndrawatiCommented:
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
 
rstaveleyCommented:
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.