Link to home
Start Free TrialLog in
Avatar of KalluMama
KalluMama

asked on

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
Avatar of jkr
jkr
Flag of Germany image

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;
}
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<--------
Avatar of KalluMama
KalluMama

ASKER

...can this be done as a function call??
That should have been...

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

(VC 7.1 doesn't grumble when you omit the int)
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<--------
Clearly, it would be a smart move to pass the function the numOfRows, so it doesn't need to be global.
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<--------
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);
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?
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]);
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
>>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;
}
}
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);
where is pEnd defined here?
what is memset??
...but jkr, how do i display the totals as i had listed it above...sorry i'm stil very new to C++...thanks
SOLUTION
Avatar of jkr
jkr
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial