0

I have a function that creates a new matrix based on dimensions given by the user, it looks like this:

void matrix(n, m){ int mtx[n][m]; for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { if ((j == 1 && i != 0 && i != n - 1) || (i == 1 && j != 0 && j < m - 3) || (i == n - 2 && j != 0 && j < m - 3) || (j == m - 4 && i != 0 && i != n - 1)) { mtx[i][j] = 1; printf("%d ", mtx[i][j]); } else { mtx[i][j] = 0; printf("%d ", mtx[i][j]); } } printf("\n"); } } 

It outputs something like this (for the dimensions 13x13 as an example):

0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 1 0 0 0 0 1 0 0 0 0 0 0 0 1 0 0 0 0 1 0 0 0 0 0 0 0 1 0 0 0 0 1 0 0 0 0 0 0 0 1 0 0 0 0 1 0 0 0 0 0 0 0 1 0 0 0 0 1 0 0 0 0 0 0 0 1 0 0 0 0 1 0 0 0 0 0 0 0 1 0 0 0 0 1 0 0 0 0 0 0 0 1 0 0 0 0 1 0 0 0 0 0 0 0 1 0 0 0 0 1 0 0 0 0 0 0 0 1 0 0 0 0 1 1 1 1 1 1 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 

But if I try to do the same in main(), something like this is the output:

0 0 0 0 0 0 7602432 0 0 0 0 0 -13856 0 0 0 0 0 -2145891403 1 -1 0 0 0 0 0 -2144601408 1 -13360 0 1 0 64 0 0 0 -2145133264 1 -13168 0 -12816 0 0 0 0 0 231600 8 0 0 -13168 0 -12816 0 -2145990936 1 15 0 3 0 4206656 1 50 0 -2145996184 1 -13904 0 18 0 -2145970054 1 2 0 -2145145120 1 -2144661632 1 -2145988848 1 0 0 -2144002969 0 0 0 0 0 -2145996184 1 -13808 0 0 0 -2145968555 1 -2145969808 1 -13808 0 0 0 0 0 0 0 -2147184784 1 -2145147008 1 -2145968503 1 102 0 48 0 4202496 1 0 0 -2147184784 1 0 0 5624 0 -10624 0 -2145503066 1 -2145147008 1 0 0 0 0 -13168 0 -12816 0 0 0 0 0 -13312 0 -2145650929 1 0 0 -2145145672 1 4206712 1 -13416 0 0 0 688 0 13 0 4199690 1 13 1 13 0 -13360 

This is the piece of code I'm using inside main to output this matrix:

 int mtx[t1][t2]; matrix(t1, t2); for (int i = 0; i < t1; i++) { for (int j = 0; j < t2; j++) { printf("%d ", mtx[i][j]); } printf("\n"); } 

My objective is to have the right values stored on the mtx[t1][t2] defined in main.

Thanks for your help!

6
  • 1
    You never set the values in mtx. Commented Mar 13, 2021 at 17:53
  • You have two different and distinct and totally unrelated variables named mtx. Please take some time to go back to your text-book and read more about scopes. Commented Mar 13, 2021 at 17:55
  • @dbush you mean the dimensions? ("t1" and "t2") Commented Mar 13, 2021 at 17:56
  • The local variable mtx inside the matrix() function is not the same as the variable in main(). Commented Mar 13, 2021 at 17:58
  • @Tastefool I mean mtx is not modified in any way. As the other comments have said, mtx in main and mtx in matrix are two different variables. Commented Mar 13, 2021 at 18:02

1 Answer 1

2

As already stated, the mtx array in the main function, despite having the same name, is completely unrelated with the mtx array inside the function, they belong in different scopes and one is not aware of the other, in order to modify the main function mtx array inside the function you must pass it as a parameter, something like this:

// add a parameter of the same type to the function parameter list void matrix(int n, int m, int mtx[][m]) { for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { if ((j == 1 && i != 0 && i != n - 1) || (i == 1 && j != 0 && j < m - 3) || (i == n - 2 && j != 0 && j < m - 3) || (j == m - 4 && i != 0 && i != n - 1)) { mtx[i][j] = 1; } else { mtx[i][j] = 0; } } printf("\n"); } } 
int main() { int t1 = 13, t2 = 13; int mtx[t1][t2]; matrix(t1, t2, mtx); // pass it as an argument for (int i = 0; i < t1; i++) { for (int j = 0; j < t2; j++) { printf("%d ", mtx[i][j]); } printf("\n"); } } 

You'll need to remove the declaration of mtx in the function to avoid a redeclaration error.

Sign up to request clarification or add additional context in comments.

4 Comments

I was working on that solution, the only error that I was making now was that when, in main, I was passing it as an argument I was using the variable name with its dimensions as well, so the program wasn't outputing nothing, thanks for the answer!
@Tastefool, yes, that's it, the dimensions are not part of the argument, I'm glad you solved it.
@anastaciu is it considered to be a bad style to do mtx[i][j] = (j == 1 && i != 0 && i != n - 1) || (i == 1 && j != 0 && j < m - 3) || (i == n - 2 && j != 0 && j < m - 3) || (j == m - 4 && i != 0 && i != n - 1); ?
@dreamcrash, that's... quite a line of code, let's just leave at that. ;)