Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
Solved

# Sum of row using two pointers...

Posted on 2004-11-01
Medium Priority
214 Views
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
[X]
###### Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

• Help others & share knowledge
• Earn cash & points

LVL 3

Expert Comment

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

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

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

Question has a verified solution.

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

Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
Looking for a way to avoid searching through large data sets for data that doesn't exist? A Bloom Filter might be what you need. This data structure is a probabilistic filter that allows you to avoid unnecessary searches when you know the data defin…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
###### Suggested Courses
Course of the Month11 days, 10 hours left to enroll