169k views
5 votes
Problem: you need to write a code that read 2 array of size 10x10 of zero and ones that represent black

and white image 0 represent black and 1 represent white and send the array for a function name
recognition and this function should recognize this image if its contains digits 1,4,3, or unknown (return-1)

1 Answer

1 vote

Answer:

import java.util.Scanner;

import java.lang.*;

public class Main

{

//variables declared static; to be used inside main()

static int err;

static int size=10;

//arrays declared static; to be used inside main()

static int[][] white = new int[size][size];

static int[][] black = new int[size][size];

//method to recognize any errors in the image

static int recognition(int[][] img)

{

//variable to indicate any error

int notImage=0;

if(img[1][1]==0)

{

for(int l=0;l<size; l++)

{

for(int k=0;k<size; k++)

{

if(img[l][k]!=0)

notImage++;

}

}

}

else

{

for(int l=0;l<size; l++)

{

for(int k=0;k<size; k++)

{

if(img[l][k]!=1)

notImage++;

}

}

}

if(notImage!=0)

return -1;

else

return 0;

}

public static void main(String[] args) {

Scanner sc = new Scanner(System.in);

System.out.println("Enter the elements for 2d black array of size 10(enter 0): ");

for(int l=0;l<size; l++)

{

for(int k=0; k<size; k++)

{

white[l][k]=sc.nextInt();

}

}

System.out.println("Enter the elements for 2d white array of size 10(enter 1): ");

for(int l=0;l<size; l++)

{

for(int k=0; k<size; k++)

{

black[l][k]=sc.nextInt();

}

}

err = recognition(white);

if(err==-1)

System.out.println("White image has errors.");

else

System.out.println("White image does not has errors.");

err = recognition(black);

if(err==-1)

System.out.println("Black image has errors.");

else

System.out.println("Black image does not has errors.");

}

}

OUTPUT

Program tested for smaller sizes.

Step-by-step explanation:

1. Two two dimensional integer arrays are declared.

2. The variable size is declared and initialized to 10.

3. The main() prompts the user to enter the elements for both arrays; 1 for white and 0 for black.

4. Both the arrays are initialized inside two for loops independently.

5. These arrays are passed as parameter to the method, recognition(), one at a time.

6. Inside recognition(), an integer variable, notImage is declared and initialized to 0. If any other integer, other than 0 or 1 is encountered in the respective arrays, this variable is initialized to -1.

7. Inside main(), an integer variable, err, is declared. This variable stores the value returned by recognition().

8. Depending on the value of variable, err, message is displayed to the user appropriately.

User Hossein Margani
by
3.2k points