Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

1) can u see my code, why it has nothing print out public class Array { public static void productXY(int[] a) { int sum =

1) can u see my code, why it has nothing print out

public class Array {

public static void productXY(int[] a) {

int sum = 0;

int x = 0;

int y = 0;

for (int s = 0; s < a.length; s++) {

sum = sum + a[s];

// array's total sum.

}

for (int i = 0; i < a.length; i++)

for (int j = 0; j < a.length; j++) {

if (i < j) {

if (a[i] * a[j] == sum) {

// test if there are 2 product x*y = sum.

x = a[i];

y = a[j];

if(a[i] * a[j] == sum) {

System.out.println("Two elements with product equal to array sum: " + x + " and " + y);

} else {

System.out.println("Two elements with product equal to array sum: " + "no");

}

}

}

}

if (a.length <= 1)//if there don't have 2 product

System.out.println("Two elements with product equal to array sum: " + "no");

}

public static void main(String args[]) {

int b[]= {-2, 59, 23, -76, -45, -81, -74, 70, -16, -95, 67, 77, 8, -88, -35, 47, -17, 0, 19};

//-299, no

productXY(b);

}

}

-------------------------

2) if i do like this, if will have too many print out

public class Array {

public static void productXY(int[] a) {

int sum = 0;

int x = 0;

int y = 0;

for (int s = 0; s < a.length; s++) {

sum = sum + a[s];

// array's total sum.

}

for (int i = 0; i < a.length; i++) {

for (int j = 0; j < a.length; j++) {

if (i < j) {

if (a[i] * a[j] == sum) {

// test if there are 2 product x*y = sum.

x = a[i];

y = a[j];

}

if (a[i] * a[j] == sum) {

System.out.println("Two elements with product equal to array sum: " + x + " and " + y);

} else {

System.out.println("Two elements with product equal to array sum: " + "no");

}

}

}

}

if (a.length <= 1)// if there don't have 2 product

System.out.println("Two elements with product equal to array sum: " + "no");

}

public static void main(String args[]) {

int b[] = { -2, 59, 23, -76, -45, -81, -74, 70, -16, -95, 67, 77, 8, -88, -35, 47, -17, 0, 19 };

// -299, no

productXY(b);

}

}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Concepts

Authors: David M. Kroenke, David J. Auer

7th edition

133544621, 133544626, 0-13-354462-1, 978-0133544626

More Books

Students also viewed these Databases questions