[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 224
  • Last Modified:

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
0
KalluMama
Asked:
KalluMama
  • 7
  • 6
  • 5
2 Solutions
 
jkrCommented:
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
 
rstaveleyCommented:
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
 
KalluMamaAuthor Commented:
...can this be done as a function call??
0
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
rstaveleyCommented:
That should have been...

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

(VC 7.1 doesn't grumble when you omit the int)
0
 
rstaveleyCommented:
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
 
rstaveleyCommented:
Clearly, it would be a smart move to pass the function the numOfRows, so it doesn't need to be global.
0
 
rstaveleyCommented:
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
 
jkrCommented:
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
 
KalluMamaAuthor 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
 
rstaveleyCommented:
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
 
KalluMamaAuthor 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
 
jkrCommented:
>>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
 
jkrCommented:
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
 
KalluMamaAuthor Commented:
where is pEnd defined here?
0
 
KalluMamaAuthor Commented:
what is memset??
0
 
KalluMamaAuthor 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
 
jkrCommented:
'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
 
rstaveleyCommented:
--------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

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

  • 7
  • 6
  • 5
Tackle projects and never again get stuck behind a technical roadblock.
Join Now