I'm trying to make a program that calculates the determinant of a matrix. I ask the user the size of the matrix they want and the values of the elements of the matrix. Whenever I try to print the matrix the value of the variable which holds the size of the matrix weirdly changes and I end up printing a whole bunch of random values. This only seems to happen if the size inputted is more than 4. This is my code:
#include <stdio.h>
#define MAX 20
void fillMatrix(int b[][MAX], int size);
void printMatrix(int b[][MAX], int size);
int main(){
//size of the matrix
int size;
//user inputs size of matrix
printf("What size? ");
scanf(" %d", &size);
//declare matrix
int matrix[size][size];
//fill matrix and print it
fillMatrix(matrix, size);
printMatrix(matrix, size);
}
//fill matrix
void fillMatrix(int b[][MAX], int size){
for (int i = 0; i < size; i ){
for (int j = 0; j < size; j ){
printf("Element %d, %d: ", i, j);
scanf(" %d", &b[i][j]);
}
}
}
//prints matrix
void printMatrix(int b[][MAX], int size){
printf("\nYour matrix: Size is: %d \n\n", size);
for (int i = 0; i < size; i ){
for (int j = 0; j < size; j ){
printf("%-5d", b[i][j]);
}
printf("\n");
}
}
This is the output i got, which is extremely weird. Whenever I run the program the 'size' variable changes to a new random value (this time it decided it wanted to be 14):
What size? 5
Element 0, 0: 1
Element 0, 1: 2
Element 0, 2: 3
Element 0, 3: 4
Element 0, 4: 5
Element 1, 0: 6
Element 1, 1: 7
Element 1, 2: 8
Element 1, 3: 9
Element 1, 4: 10
Element 2, 0: 11
Element 2, 1: 12
Element 2, 2: 13
Element 2, 3: 14
Element 2, 4: 15
Element 3, 0: 16
Element 3, 1: 17
Element 3, 2: 18
Element 3, 3: 19
Element 3, 4: 20
Element 4, 0: 21
Element 4, 1: 22
Element 4, 2: 23
Element 4, 3: 24
Element 4, 4: 25
Your matrix: Size is: 14
1 2 3 4 5 0 0 0 83666196832767-1154713840325690 0
6 7 8 9 10 0 41957330 194 0 83642160032767-11569205121
11 12 13 14 15 0 -1167000585325691 0 83642200832767327681
16 17 18 19 20 0 0 0 426856809-15535752972138001769-15616396370 32767
21 22 23 24 25 0 0 0 0 0 41954400 83642200032767
1 0 836428099327670 0 83642810632767836428255327678364283383276783642842332767
83642849532767836428517327678364286073276783642862432767836428643327678364287843276783642892232767
83642913432767836430278327678364304723276783643048932767836430553327678364308953276783643094032767
83643171232767836431732327678364317403276783643176032767836431772327678364318093276783643183332767
16 0 5292677110 6 0 4096 0 17 0 100 0 3 0
5 0 9 0 7 0 -1156988928325698 0 0 0 9 0
12 0 1000 0 13 0 1000 0 14 0 1000 0 23 0
26 0 2 0 31 0 8364318573276715 0 836422649327670 0
-448521986-126364567290967056834217910 0 0 0 0 0 0 0 0 0
CodePudding user response:
You told fillMatrix
and printMatrix
that the array is an int[][MAX]
, but then you declared an int[size][size]
, which is smaller — each row only holds 5 ints instead of 20. Because the array you actually allocated is smaller than fillMatrix
thinks it is, fillMatrix
is writing beyond the end of the array and corrupting other memory — the result is undefined behavior, but what happened in your case is that one of the values you input (the 14) ended up overwriting the size
variable.
You need to declare your matrix in main as int matrix[MAX][MAX]
or int matrix[size][MAX]
so that the row stride is right — or you can use variable length arrays to declare void fillMatrix(int size, int matrix[size][size])
(note the change in the argument order so that size
appears first).
CodePudding user response:
void fillMatrix(int b[][MAX], int size);
and void printMatrix(int b[][MAX], int size);
declare those routines to have a parameter that is an array of an array of MAX
int. (That parameter is automatically adjusted to be a pointer instead of an array, but that is not an issue here.)
In main
, int matrix[size][size];
defines matrix
to be an array of size
arrays of size
int
. You later pass matrix
to those routines. That is an error, because matrix
is an array of size
int
, not an array of MAX
int
. So the addressing in the functions goes awry and destroys other data in your program.
Use the same type in the function parameters that you use in the array you define in main
or vice-versa. (To declare an array with a size based on size
instead of MAX
, make size
the first parameter instead of the last.)