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!
mtx.mtx. Please take some time to go back to your text-book and read more about scopes.mtxinside thematrix()function is not the same as the variable inmain().mtxis not modified in any way. As the other comments have said,mtxinmainandmtxinmatrixare two different variables.