0

I figured out how to make this one work already but can't explain in details what is so different in these two codes here.

Incorrect code:

const int nRows = 2;
const int nCols = 2;
int * colSum (int [nRows][nCols]);
int * rowSum (int [nRows][nRows]);

int main() {

    int my2Darray[nRows][nCols] = {{10, 20}, {30, 40}};
    int totalsByColumn[nCols] = {};
    *totalsByColumn = *(colSum(my2Darray));
    
    for (int i = 0; i < nCols; i++) {
        cout << totalsByColumn[i] << endl;
    } 
}

int * colSum (int arrayArg[nRows][nCols]) {

    static int arr[nRows] = {};

    for (int i = 0; i < nCols; i++) {
        for (int rowcount = 0; rowcount < nRows; rowcount++) {
            arr[i] += arrayArg[rowcount][i];
        }
    }
    return arr;
}

I was getting 40 0 as the output.

Then I fixed it by doing this:

int main() {

    int my2Darray[nRows][nCols] = {{10, 20}, {30, 40}};
    int *totalsByColumn = colSum(my2Darray);
    
    for (int i = 0; i < nCols; i++) {
        cout << totalsByColumn[i] << endl;
    } 
}

Output is 40 60, just what I wanted.

Was I just decaying to the first element of totalsByColumn by using the dereference operator on my first block of code? I feel like there might be a quicker way of adding the columns and rows together and assigning them to arrays in the main function, but as long as it does what I want I'm okay with that for the moment.

8
  • 1
    The dereference operator dereferences one int Commented Dec 14, 2020 at 15:54
  • 1
    *totalsByColumn = *(colSum(my2Darray)) is equivalent to totalsByColumn[0] = colSum(my2Darray)[0]. You are only assigning one element of totalsByColumn, the other preserves whatever value it had previously. Commented Dec 14, 2020 at 15:55
  • 1
    I have no idea why you are making your life difficult with raw arrays, use std::vector. Commented Dec 14, 2020 at 15:55
  • 1
    @Quimby Or std::array if the dimension of each array is a compile-time constant as it is here. Passing arrays to and from functions is fiddly and annoying, the standard library containers are a lot nicer. Commented Dec 14, 2020 at 15:56
  • 1
    @Quimby Newbies love pointers. It's a fact of life (or bad teaching). Commented Dec 14, 2020 at 15:57

1 Answer 1

1

Was I just decaying to the first element of totalsByColumn by using the dereference operator on my first block of code?

Yes.

I feel like there might be a quicker way of adding the columns and rows together

Certainly. Also ways that are thread safe unlike your solution. A simple way is to use an output iterator to write directly to the array where you want the results:

int* colSum (int arrayArg[][nCols], int out[]) {
    for (int i = 0; i < nCols; i++) {
        out[i] = 0;
        for (int rowcount = 0; rowcount < nRows; rowcount++) {
            out[i] += arrayArg[rowcount][i];
    // ...

int totalsByColumn[nCols];
colSum(my2Darray, totalsByColumn);
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you so much, that's much better!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.