# Totaling columns in a multi dim array using pointers

OK,

I am trying to study pointer and arrays and have been stuck on how to do this...can anyone help

I have an array:

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

I wanna total up each column, but only using pointers. Can't seem to get it right, can anyone please help!! Thanks!

KM
###### Who is Participating?

Commented:
--------8<--------
#include <iostream>

const int numOfCols = 3;

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

void f(int (*)[numOfCols],int (*)[numOfCols],int[numOfCols]);

int main()
{
int result[numOfCols];
std::cout << "Summing rows 0 to 3...\n";
f(&table[0],&table[3],result);
for (int i = 0;i < numOfCols;i++)
std::cout << "Total of column " << i << ": " << result[i] << '\n';

}

void f(int (*itr)[numOfCols],int (*last)[numOfCols],int result[numOfCols])
{
// Initialise result
for (int i = 0;i < numOfCols;i++)
result[i] = 0;

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

Commented:
You could e.g.

int sum[numOfCols];
int* pn = (int*)table;

memset(sum, 0, numOfCols*sizeof(int));

for (int i = 0; i < numOfRows * numOfCols; ++i, ++pn) {

sum[i % numOfCols] + = *pn;
}
0

Commented:
In the following, p is a pointer to an array of numOfCols of ints...

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

const numOfCols = 3;
const numOfRows = 4;

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

int main()
{
int (*p)[numOfCols] = table;
for (int row = 0;row < numOfRows;row++) {
std::cout << "First column is: " << (*p)[0] << '\n';
p++;
}
}
--------8<--------
0

Author Commented:
...can this be done as a function call??
0

Commented:
That should have been...

const int numOfCols = 3;
const int numOfRows = 4;

(VC 7.1 doesn't grumble when you omit the int)
0

Commented:
If you want it as a function call...

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

const int numOfCols = 3;
const int numOfRows = 4;

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

void f(int (*)[numOfCols]); // Prototype

int main()
{
f(table);
}

// Implementation
void f(int (*p)[numOfCols])
{
for (int row = 0;row < numOfRows;row++) {
std::cout << "First column is: " << (*p)[0] << '\n';
p++;
}
}
--------8<--------
0

Commented:
Clearly, it would be a smart move to pass the function the numOfRows, so it doesn't need to be global.
0

Commented:
i.e.
--------8<--------
#include <iostream>

const int numOfCols = 3;
const int numOfRows = 4;

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

void f(int (*)[numOfCols],int); // Prototype

int main()
{
f(table,sizeof(table)/sizeof(int[numOfCols]));
}

// Implementation
void f(int (*p)[numOfCols],int numOfRows)
{
for (int row = 0;row < numOfRows;row++) {
std::cout << "First column is: " << (*p)[0] << '\n';
p++;
}
}
--------8<--------
0

Commented:
As a function call? Sure, you could

void SumCols(int* pTable, int numOfCols, int numOfRows, int* sum) {

memset(sum, 0, numOfCols*sizeof(int));

for (int i = 0; i < numOfRows * numOfCols; ++i, ++pn) {

sum[i % numOfCols] + = *pn;
}
}

//...

int sum[numOfCols];
int* pn = (int*)table;

SumCols( pn, numOfCols,numOfRows, sum);
0

Author Commented:
oops i forgot to mention...what i was trying to do was set a pointer to the first element and one to the last...and then find the sum without using number of rows or columns...is it  possible?
0

Commented:
Yes..

// Implementation
void f(int (*itr)[numOfCols],int (*end)[numOfCols])
{
while (itr <= end) {
std::cout << "First column is: " << (*itr)[0] << '\n';
itr++;
}
}

Call the function with (say)

f(&table[0],&table[2]);
0

Author Commented:
rstaveley,

the code you gave only list the first element in the first 3 rows of the array
what i wanted to get was:

Sum of row 1: XXX
Sum of row 2: XXX
Sum of row 3: XXX
Sum of row 4: XXX
0

Commented:
>>what i was trying to do was set a pointer to the first element and one to the last.

That would work also:

void SumCols(int* pStart, int* pStart,int numOfCols, int* sum) {

int i = 0;
int* pn = pStart;

memset(sum, 0, numOfCols*sizeof(int));

for (; pn != pEnd; ++i, ++pn) {

sum[i % numOfCols] + = *pn;
}
}
0

Commented:
Ooops, that should be

void SumCols(int* pStart, int* pEnd,int numOfCols, int* sum) {

int i = 0;
int* pn = pStart;

memset(sum, 0, numOfCols*sizeof(int));

for (; pn != pEnd; ++i, ++pn) {

sum[i % numOfCols] + = *pn;
}
}

//...

int sum[numOfCols];
int* p1 = &table[0][0];
int* p1 = &table[numOfRows][numOfCols];

SumCols( p1, p2, numOfCols, sum);
0

Author Commented:
where is pEnd defined here?
0

Author Commented:
what is memset??
0

Author Commented:
...but jkr, how do i display the totals as i had listed it above...sorry i'm stil very new to C++...thanks
0

Commented:
'pEnd' should be 'p2' - copy and paste error :o)
'memset()' will initialize the sum array to 0 and can be found in 'memory.h'

To print the values, you could use

int sum[numOfCols];
int* p1 = &table[0][0];
int* p2 = &table[numOfRows][numOfCols];

SumCols( p1, p2, numOfCols, sum);

for (int i = 0; i < numOfCols; ++i) {

std::cout << "Sum of col " << i << " is " << sum[i] << std::endl;

}
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.