[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

Totaling columns in a multi dim array using pointers

Posted on 2004-11-01
18
Medium Priority
?
223 Views
Last Modified: 2010-04-01
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
Comment
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
  • Learn & ask questions
  • 7
  • 6
  • 5
18 Comments
 
LVL 86

Expert Comment

by:jkr
ID: 12464971
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
 
LVL 17

Expert Comment

by:rstaveley
ID: 12465015
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 Comment

by:KalluMama
ID: 12465049
...can this be done as a function call??
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 17

Expert Comment

by:rstaveley
ID: 12465050
That should have been...

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

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

Expert Comment

by:rstaveley
ID: 12465074
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
 
LVL 17

Expert Comment

by:rstaveley
ID: 12465088
Clearly, it would be a smart move to pass the function the numOfRows, so it doesn't need to be global.
0
 
LVL 17

Expert Comment

by:rstaveley
ID: 12465120
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
 
LVL 86

Expert Comment

by:jkr
ID: 12465133
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 Comment

by:KalluMama
ID: 12465210
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
 
LVL 17

Expert Comment

by:rstaveley
ID: 12465264
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 Comment

by:KalluMama
ID: 12465450
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
 
LVL 86

Expert Comment

by:jkr
ID: 12465517
>>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
 
LVL 86

Expert Comment

by:jkr
ID: 12465527
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 Comment

by:KalluMama
ID: 12465557
where is pEnd defined here?
0
 

Author Comment

by:KalluMama
ID: 12465584
what is memset??
0
 

Author Comment

by:KalluMama
ID: 12465672
...but jkr, how do i display the totals as i had listed it above...sorry i'm stil very new to C++...thanks
0
 
LVL 86

Assisted Solution

by:jkr
jkr earned 200 total points
ID: 12465751
'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
 
LVL 17

Accepted Solution

by:
rstaveley earned 1800 total points
ID: 12465809
--------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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
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

656 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