I cant figure out how to check the array for AND and OR. please, help, i got stuck

my code:

import java.util.Scanner;

public class BooleanProduct

import java.util.Scanner;

public class BooleanProduct
{
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
int[][] A = new int[10][10];
int[][] B = new int[10][10];
int[][] C = new int[10][10];
int m,n,p,q;
System.out.println("Enter number of rows for matrix A : ");
m = input.nextInt();
System.out.println("Enter number of columns for matrix A : ");
n = input.nextInt();
System.out.println("Enter number of rows for matrix B : ");
p = input.nextInt();
System.out.println("Enter number of columns for matrix B : ");
q = input.nextInt();
System.out.println("Enter elements for matrix A (by rows): ");
for (int i=0 ; i < m ; i++)
for(int j=0 ; j < n ; j++)
{
A[i][j] = input.nextInt();
}
System.out.println("Enter elements for matrix B : ");
for (int i=0 ; i < p ; i++)
for(int j=0 ; j < q ; j++){
B[i][j] = input.nextInt();
}
System.out.println("Matrix A: ");
for(int i=0 ; i < m ; i++)
{
System.out.println();
for(int j=0 ; j < n ; j++)
{
System.out.print(A[i][j]+" ");
}
}
System.out.println();
System.out.println("Matrix B: ");
for (int i=0 ; i < p ; i++)
{ System.out.println();
for(int j=0 ; j < q ; j++){
System.out.print(B[i][j]+" ");
}
}
System.out.println();
}
}
System.out.println("Boolean Product of matrices A and B is: ");
{
 i=0;
 j=0;
  if (A [i][j]==1 && B [i][j]==1)
 {
     temp C [i][j] = 1;
     i++;
     j++;
 }
 else
 {
     C [i][j]= 0;
     i++;
     j++;
 }
}
{

.... i lost it here...how can i store the value, because i need it than to compare it with OR ||

Dani AI

Generated

For a Boolean product you must treat "multiplication" as logical AND and "addition" as logical OR. Concretely:

C[i][j] = OR over k of ( A[i][k] AND B[k][j] )

Common mistakes seen in the thread: using A[i][j] and B[i][j] (wrong k index), not resetting a temporary for each (i,j), or trying to combine everything in one expression without the loop. is right that OR (||) is the accumulation you want across k; is right that parentheses control precedence, but using an explicit loop is clearer and easier to short‑circuit.

Example (drop this into your class after you read A and B):

static int[][] booleanProduct(int[][] A, int[][] B) {
    int m = A.length;
    int n = A[0].length;
    int p = B.length;
    int q = B[0].length;
    if (n != p) throw new IllegalArgumentException("A.columns must equal B.rows");
    int[][] C = new int[m][q];
    for (int i = 0; i < m; i++) {
        for (int j = 0; j < q; j++) {
            for (int k = 0; k < n; k++) {
                if (A[i][k] == 1 && B[k][j] == 1) {
                    C[i][j] = 1;    // OR becomes true, stop scanning k
                    break;
                }
            }
        }
    }
    return C;
}

Integration notes and troubleshooting:

  • Call int[][] C = booleanProduct(A, B); and then print C.
  • Ensure your input values are 0 or 1. If you want to treat any nonzero as true, test A[i][k] != 0 and B[k][j] != 0.
  • Watch loop bounds: the inner loop must run k from 0 to A[0].length-1 (not m or q).
  • Using break short‑circuits once an AND pair is found, which is both correct and faster.

Recommended Answers

All 3 Replies

tried
if (A [i][j]==1 || B [i][j]==1)
?

well, but first i gotta do AND operator &&. After that two value compare with OR ||. I gor new values after &&. do i store them and how? and than use OR operator on them?

You can use parens to control the order of execution, eg

((a && b) || (c && d))
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.