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)