Home > Software design >  Returning New Array and getting wrong output
Returning New Array and getting wrong output

Time:01-11

#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("]", 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?

CodePudding user response:

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("]", 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.

  •  Tags:  
  • Related