Solved

Totaling columns in a multi dim array using pointers

Posted on 2004-11-01
222 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
Question by:KalluMama
    18 Comments
     
    LVL 86

    Expert Comment

    by:jkr
    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
    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
    ...can this be done as a function call??
    0
     
    LVL 17

    Expert Comment

    by:rstaveley
    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
    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
    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
    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
    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
    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
    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
    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
    >>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
    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
    where is pEnd defined here?
    0
     

    Author Comment

    by:KalluMama
    what is memset??
    0
     

    Author Comment

    by:KalluMama
    ...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
    '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:
    --------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

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Looking for New Ways to Advertise?

    Engage with tech pros in our community with native advertising, as a Vendor Expert, and more.

    Article by: SunnyDark
    This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
    C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
    The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
    The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

    934 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

    Need Help in Real-Time?

    Connect with top rated Experts

    23 Experts available now in Live!

    Get 1:1 Help Now