skip to Main Content

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


  1. The error statement is pretty clear, Top-level statements (prettySudoku(puzzle);) must precede namespace (int [][] line) and type declarations public 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.

    Login or Signup to reply.
  2. There are several issues in your code:

    1. It is not clear in which scope list1..list9, puzzle are declared, but is should be inside some class.
    2. You can make puzzle a 2D array [,] instead of an array of arrays.
    3. Calling a method like the printing method cannot be done in global scope. It should be placed in another method (it can also be the Main method).
    4. In order to call the method which is non-static, you should have an object (i.e. an instance of the class) to call it on.
    5. All the relevant code can be placed in one class like Sudoku in my example below.
    6. The printing method can format the puzzle as a table adding a newline only after each row.

    This is demonstrated below:

    using System;
    
    namespace Test
    {
        class Sudoku
        {
            int[,] puzzle ={
                    { 7, 3, 6, 4, 5, 2, 9, 8, 1 },
                    { 1, 9, 8, 6, 3, 7, 4, 5, 2 },
                    { 4, 2, 5, 9, 8, 1, 3, 7, 6 },
                    { 3, 6, 4, 5, 2, 8, 1, 9, 7 },
                    { 9, 5, 2, 7, 1, 4, 6, 3, 8 },
                    { 8, 1, 7, 3, 9, 6, 2, 4, 5 },
                    { 2, 8, 9, 1, 7, 3, 5, 6, 4 },
                    { 6, 7, 3, 2, 4, 5, 8, 1, 9 },
                    { 5, 4, 1, 8, 6, 9, 7, 2, 3 },
            };
    
            public void print()
            {
                for (int i = 0; i < puzzle.GetLength(0); i++)
                {
                    for (int j = 0; j < puzzle.GetLength(1); j++)
                    {
                        Console.Write("{0} ", puzzle[i, j]);
                    }
                    Console.WriteLine("");  // go to next line
                }
            }
        }
    
        class Program
        {
            static void Main(string[] args)
            {
                // Create a Sudoku instance:
                Sudoku sudoku = new Sudoku();
    
                // Call the `print` method on the instance:
                sudoku.print();
            }
        }
    }
    

    Output:

    7 3 6 4 5 2 9 8 1
    1 9 8 6 3 7 4 5 2
    4 2 5 9 8 1 3 7 6
    3 6 4 5 2 8 1 9 7
    9 5 2 7 1 4 6 3 8
    8 1 7 3 9 6 2 4 5
    2 8 9 1 7 3 5 6 4
    6 7 3 2 4 5 8 1 9
    5 4 1 8 6 9 7 2 3
    

    Live demo

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search