I am trying to create a program which prints a matrix of integers, but the output returns weird numbers before the actual matrix. There are no compiling errors.
This is what my code looks like: //ignore void function for now, focus on main function::
#include <stdio.h> #include <stdlib.h> //array[30][30] = 2D array of max 30 rows and 30 columns //n = number of rows and columns void printmatrix(int array[30][30], int n){ for(int i = 0; i < n; i++){ for(int j = 0; j < n; j++){ printf("%d", array[i][j]); } printf("\n"); } return; } int main(){ int n; scanf("%d", &n); int ints2D[n][n]; for(int i = 0; i < n; i++){ for(int j = 0; j < n; j++){ scanf("%d", &ints2D[i][j]); } } printmatrix(ints2D, n); for(int i = 0; i < n; i++){ for(int j = 0; j < n; j++){ printf("%d ", ints2D[i][j]); } printf("\n"); } return 0; } And this is my output (I only want the last three lines)
123 -514159984327663 -51415932632766-514159305 1 2 3 4 5 6 7 8 9
printmatrix(), is expecting an array of arrays, where each array has 30 elements. But the array you pass has only three arrays, and each of those has only three elements. This means that once the loop gets past the first row, you're just printing random data. Also, you didn't include the extra' 'character in the function, so all the numbers run together.