I’m trying to print the array called puzzle. However, I get this error:
Error CS8803: Top-level statements must precede namespace and type declarations.
This happens when trying to use prettySudoku() on the last line. I am running this using dotnet and the linux/ubuntu terminal. This is my first time using methods.
Here’s the code:
int[] list1 = new int[]{7, 3, 6, 4, 5, 2, 9, 8, 1};
int[] list2 = new int[]{1, 9, 8, 6, 3, 7, 4, 5, 2};
int[] list3 = new int[]{4, 2, 5, 9, 8, 1, 3, 7, 6};
int[] list4 = new int[]{3, 6, 4, 5, 2, 8, 1, 9, 7};
int[] list5 = new int[]{9, 5, 2, 7, 1, 4, 6, 3, 8};
int[] list6 = new int[]{8, 1, 7, 3, 9, 6, 2, 4, 5};
int[] list7 = new int[]{2, 8, 9, 1, 7, 3, 5, 6, 4};
int[] list8 = new int[]{6, 7, 3, 2, 4, 5, 8, 1, 9};
int[] list9 = new int[]{5, 4, 1, 8, 6, 9, 7, 2, 3};
int[][] puzzle = new int[][] {list1, list2, list3, list4, list5, list6, list7, list8, list9};
public class prettyPrinters{
public void prettySudoku(int[][] arr){
for(int i = 0; i<arr.GetLength(0); i++){
for(int j =0; j<arr.GetLength(1); j++){
Console.WriteLine(arr[i][j]);
}
}
}
}
prettySudoku(puzzle);
2
Answers
The error statement is pretty clear, Top-level statements (
prettySudoku(puzzle);
) must precede namespace (int [][]
line) and type declarationspublic class prettyPrinters
).This is the general program structure of all variants of C (C++, C# etc. ) languages.
So, you should move up declaration line
prettySudoku(puzzle);
. To avoid repetition, read here for more details.There are several issues in your code:
list1
..list9
,puzzle
are declared, but is should be inside some class.puzzle
a 2D array[,]
instead of an array of arrays.Main
method).Sudoku
in my example below.This is demonstrated below:
Output:
Live demo