-2
#define N 3
int subMatrix(int a[][N]) {
    int i, j;
    int sum = 0;
    int arr[N];

    for (i = 0; i < N; i++) {
        for (j = 0; j < N; j++) {
            sum += a[i][j];
            sum -= a[j][i];
        }
        arr[i] = sum;
        sum = 0;
    }
    return *arr;
}

void main() {
    int a[N][N] = {
        {9,2,4},
        {3,7,11},
        {3,1,2}
    };
    for (int i = 0; i < N; i++) {
        printf("%5d", subMatrix(a[i]));
    }
}

The function works fine, the problem is when I'm returning the new array and loop over it in the main function I get the first element of the array and the other elements are addresses. i did it before with another array with size of doubles and it worked. There is something i miss?

double avgMatrix(int a[][C]) {
    int i, j, sum=0;
    double M[R];
    for (i = 0; i < R; i++) {
        for (j = 0; j < C; j++) {
            sum += a[i][j];
        }
        M[i] = (double)sum / C;
        sum = 0;
    }
    return *M;
}

void main() {
    int a[R][C] = {
        {9,2,4},
        {3,8,11},
        {3,1,2}
    };
    for (int i = 0; i < R; i++)
        printf("%5.2lf", avgMatrix(a[i]));
}

this code works. what can be the difference?

Snir Taub
  • 3
  • 3
  • Does this answer your question? [Returning an array using C](https://stackoverflow.com/questions/11656532/returning-an-array-using-c) – kaylum Jan 11 '22 at 01:00
  • "*I'm returning the new array*". Can you please clarify what you are trying to do? The function returns an `int` and not an array. Is that really what you are intending? If you really do want to return a whole new array please read the duplicate post for how to do that correctly. – kaylum Jan 11 '22 at 01:02
  • @kaylum the Function sub the rows and cols from this array : int a[N][N] = { {9,2,4}, {3,7,11}, {3,1,2} }; and insert the sum in arr. well i did the same thing before and it worked. – Snir Taub Jan 11 '22 at 01:13
  • @SnirTaub This code is very hard to understand because there are no comments. The function, `subMatrix` looks like it expects a two-dimensional array. It references `a[i][j]` and `a[j][i]`, which is consistent with it expecting a two-dimensional array. But then you pass it `a[i]`, which is a one-dimensional piece of a two-dimensional array. If the function is supposed to be passed a two-dimensional array, why are you passing it one line of `a`? – David Schwartz Jan 11 '22 at 01:16
  • the output should be one line array, 0,11,-11. im getting 0, and 2 long addresses instead – Snir Taub Jan 11 '22 at 01:18
  • @SnirTaub One the last iteration of the loop in `main`, you call `subMatrix(a[2])`. But then inside `subMatrix`, you do `a[i][j]`, which on the last iteration is `a[2][2]`. But `a[2]` has no element `[2][2]`. Fundamentally, this code doesn't make any sense. What do you think `(a[2])[2][2]` means? – David Schwartz Jan 11 '22 at 01:24

1 Answers1

0

I do not really understand what your function subMatrix does. Your code needs a few modifications to be able to compile.
First, include the necessary header #include <stdio.h>, because your code needs printf.
Second, make sure the passed parameter and the attribute be the same type.
Third, if you would like to return an array from a function, you should use dynamic allocation function to help you do that. malloc

/* At least, make sure to include necessary head files
* #include <stdio.h>
*/
#define N 3
int subMatrix(int a[][N]) {
    int i, j;
    int sum = 0;
    int arr[N];

    for (i = 0; i < N; i++) {
        for (j = 0; j < N; j++) {
            /* Because the passed parameter is one dimensional 
             * so the following code does not make sense.
             */
            sum += a[i][j];
            sum -= a[j][i];
        }
        arr[i] = sum;
        sum = 0;
    }

    /* arr is a local variable. It is actually a pointer
     * It should never be returned.
     * In fact, *arr is only the first element of the array of arr.
     * At least, you should return the address of the first element.
     * Considering your purpose, to use dynamic allocation is proper.
     */
    return *arr;
}
/* 'void main()' is not right.
 * 'int main(void)' is the right way.
 */
void main() {
    int a[N][N] = {
        {9,2,4},
        {3,7,11},
        {3,1,2}
    };
    for (int i = 0; i < N; i++) {
        /* a[i] is a one-dimensional array, 
         * but subMatrix needs a two dimensional one.
         */
        printf("%5d", subMatrix(a[i]));
    }
}

A possible working code:

#include <stdio.h>
#include <stdlib.h>
#define N 3
int *subMatrix(int a[N][N]) {
    int sum = 0;
    int *arr = (int *)malloc(N*sizeof(int));

    for (int i = 0; i < N; i++) {
        for (int j = 0; j < N; j++) {
            sum += a[i][j];
            sum -= a[j][i];
        }
        arr[i] = sum;
        sum = 0;
    }

    return arr;
}
int main(void) {
    int a[N][N] = {
        {9,2,4},
        {3,7,11},
        {3,1,2}
    };
    int *arr = subMatrix(a);

    for (int i = 0; i < N; i++)
        printf("arr[%d] : %d\n", i, arr[i]);

    free(arr);
}

Is this what you want? Try it.

Wu Xiliang
  • 316
  • 3
  • 10