0

Am writing a program that decides if a number X is in the matrix of input n x n matrix of number that is already stored in memory in 2D array and each individual rows are increasing from left to right; column top to bottom. I enter the size of the matrix n, the content, and the number X to search; I also display the result of my search in the screen. While testing my program my the input is sorted in 2D array.

Here is my code, and its showing the below compiling error

import java.util.*;

public class matrix
{
    public static void main (String[] args){

        int search(int mat[4][4]; int n; int x)
        {
            int i = 0, j = n-1;  //set indexes for top right element
            while ( i < n && j >= 0 )
            {
                if ( mat[i][j] == x )
                {
                    printf("\n Found at %d, %d", i, j);
                    return 1;
                }
                if ( mat[i][j] > x )
                    j--;
                else //  if mat[i][j] < x
                    i++;
            }

            printf("\n Element not found");
            return 0;  // if ( i==n || j== -1 )
        }

        // driver program to test above function
        int main()
        {
            int mat[4][4] = { {10, 20, 30, 40},
                              {15, 25, 35, 45},
                              {27, 29, 37, 48},
                              {32, 33, 39, 50},
                             };
            search(mat, 4, 29);
            return 0;
        }
    }
}

Stack trace:

Exception in thread "main" java.lang.Error: Unresolved compilation problems: 
    Syntax error on token "(", ; expected
    Syntax error on token "4", delete this token
    Syntax error on token "4", delete this token
    Syntax error on token ")", ; expected
    The method printf(String, int, int) is undefined for the type matrix
    Void methods cannot return a value
    The method printf(String) is undefined for the type matrix
    Void methods cannot return a value
    Syntax error on token "int", new expected
    main cannot be resolved to a type
    Syntax error, insert ";" to complete FieldDeclaration
    Syntax error, insert "}" to complete ClassBody
    Syntax error on token "}", delete this token

    at matrix.main(matrix.java:8)
1
  • 2
    Your function parameters are delimited by semicolons instead of commas. Commented Mar 10, 2014 at 16:07

2 Answers 2

6

You have the search method inside the main(), it should be outside. Also, you are declaring another main inside the main() method. Remember that the main method is an entry point for the program, and there should only exist just one:

public static void main(String[] args) { ... }

In Java, you are not supposed to declare the length of the arrays in the parameters declaration. Also, use commas to separate the parameters:

public static int search(int[][] mat, int n, int x)

You should use System.out.printf() instead of just printf().

Finally, an array can be declared/initialized in multiple ways:

int mat[][] = {{1,2}, {3,4}};
int mat[][] = new int[3][3];
int mat[][] = new int[3][];

The final code should look like:

public static int search(int[][] mat, int n, int x)
{
    int i = 0, j = n - 1; //set indexes for top right element
    while (i < n && j >= 0) {
        if (mat[i][j] == x) {
            System.out.printf("\n Found at %d, %d", i, j);
            return 1;
        }
        if (mat[i][j] > x) {
            j--;
        } else {
            i++;
        }
    }

    System.out.printf("\n Element not found");
    return 0; // if ( i==n || j== -1 )
}

public static void main(String[] args)
{
    int mat[][] = { { 10, 20, 30, 40 }, { 15, 25, 35, 45 }, { 27, 29, 37, 48 }, { 32, 33, 39, 50 }, };
    search(mat, 4, 29);
}
Sign up to request clarification or add additional context in comments.

Comments

1

As far as I know, local functions are not supported in Java so that's why the compiler is thrown off. Of course, using ';' to separate function arguments does not help either.

public static void main (String[] args){


int search(int mat[4][4]; int n; int x) // Local method declaration.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.