[Webinar] Streamline your web hosting managementRegister Today

x
?
Solved

Sum of row using two pointers...

Posted on 2004-11-01
3
Medium Priority
?
216 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
Comment
Question by:KalluMama
3 Comments
 
LVL 3

Expert Comment

by:Indrawati
ID: 12468912
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
ID: 12471147
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:
Jaime Olivares earned 2000 total points
ID: 12472318
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

Featured Post

Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
Suggested Courses

612 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