0

I'm trying to create a null matrix with the method zeros (); and then show it on the screen. Nothing is shown.

double * zeros(int m,int n){
    double matrix[3][3] ={0};
    return matrix;
}

void printMatrice(int row,int column, double matrix [3][3]) {
     for (row=0; row<3; row++)
     {
        for(column=0; column<4; column++)
            {printf("%f     ", matrix[row][column]);}
            printf("\n");
     }
}


MAIN:

int main () {

    printMatrice(3,3,zeros(3,3));

   return 0;
}

Show this on screen:

Process returned -1073741819 (0xC0000005)

These are the warnings

Of method printMatrice():

--warning: passing argument 3 of 'printMatrice' from incompatible pointer type [-Wincompatible-pointer-types]|

Of method zeros():

-- warning: returning 'double (*)[3]' from a function with incompatible return type 'double *' [-Wincompatible-pointer-types]

--warning: function returns address of local variable [-Wreturn-local-addr]|

Diego R
  • 13
  • 3
  • 1
    definitely a few problems - can't return a local array in `zeros`, weird to reuse `row` and `column` as counters instead of limits. But I find it hard to believe nothing is printing. – AShelly Apr 11 '19 at 19:12
  • Fix your compiler warnings and try again. – AShelly Apr 11 '19 at 19:13
  • 2
    It would be helpful for you to 1. fix compiler warnings 2. think about what you are doing and why. Why is each number being passed to each function and why do you need it. Go though every line and think about it, this should help fix errors and odd things you are doing – liamcomp Apr 11 '19 at 19:17
  • I just put the warnings that I have, I do not know how to solve them. – Diego R Apr 11 '19 at 19:26
  • Have you had success with 1D arrays? If not I would start there, 2D arrays are more complected. Maybe look up some tutorials on arrays in C. If you are used to using MATLAB, C is much much different. – liamcomp Apr 11 '19 at 19:32
  • 2
    Your code shows that you're just learning to program. Asking this question on SO is counterproductive for your learning: most people here know the answer, but it cannot be taught by QA site. Someone will use their knowledge to fix your code, but your knowledge will not be increased. In the end, "think about what you are doing and why" is the best advice, but you don't need SO for that. –  Apr 11 '19 at 20:34

1 Answers1

0

Working with multidimensional arrays in c++ is a pain in the ass. One of the easiest ways is to use a simple array and convert coordinates into linear index manually.

This way, you do not have to hard-wire matrix dimensions into functions, you do not have to alloc/free memory, you can reuse and reinterpret data, etc. Of course, it would be better to encapsulate it in a class, but this is just an example.

void reset(double matrix[], int rows, int cols, double value) {
    for (int i = 0; i < rows * cols; i++)
        matrix[i] = value;
}

void print(double matrix[], int rows, int cols) {
    for (int r = 0; r < rows; r++) {
        for (int c = 0; c < cols; c++)
            printf("%f     ", matrix[r * cols + c]);
        printf("\n");
    }
}

int main() {

    double m[3 * 3];
    reset(m, 3, 3, 0.0);
    print(m, 3, 3);

    printf("\n");

    double n[2*5];
    reset(n, 2, 5, 1.0);
    print(n, 2, 5);

    return 0;
}
Jiri Volejnik
  • 1,034
  • 6
  • 9