Create array with a size of an integer number, n. n will be generated as a rando
ID: 3827292 • Letter: C
Question
Create array with a size of an integer number, n. n will be generated as a random integer number in the interval between 3 and 10 (both included). Also, generate random numbers to fill the array. These numbers must be in the interval between 1 and 20 (both included). Then, find if this array "Winner" or a "Loser". Considering every element in the array, check if there is any element which has the equal summation of the elements in the left-hand side and in the right-hand side. If there is at least 1 element this array which satisfies this condition, print "Winner". Otherwise, print "Loser". You need to check every element in the array if there element to satisfy this condition or not. If there are no elements on the left or right-hand sides, then take the summation as zero. Considering every element in the array, check if sum of the elements in the left-hand side is equal to the sum of the elements the right-hand side, this array has a victory. If this condition satisfied, say "Winner" to user. Otherwise, say "Loser". You need to check all elements in the array if there element to satisfy this condition or not. If there are no elements to the left or right, then take the summation as zero. Display the size of the array and the elements of the array when it is filed by random numbers. Display "winner" if there exist at least one element in the array, such that the sum of the elements its left-hand side and the sum of the elements on its right-hand side equal. Otherwise display "Loser". Sample output 1 4 elements in the array Elements 1, 2, 3, 3 Winner Explanation 1 Element in 2nd index satisfies the given condition (1 + 2 = 3). Sample output 2 8 elements in the array Elements: 2, 5, 3, 3, 5, 8, 12, 6 Winner Explanation 2 Element in 5th index satisfies the given condition (2 + 5 + 3 + 3 + 5 - 12 + 6). Sample output 3 5 elements in the array Elements: 6, 9, 2.1, 20 Explanation 3 No elements in the array that satisfies the given conditionExplanation / Answer
PROGRAM CODE:
package util;
import java.util.Random;
public class WinnerLoser {
public static void publishResult(int data[])
{
int sumLeft = 0;
for(int i=0; i<data.length/2; i++)
{
sumLeft += data[i];
int sumRight = 0;
for(int j=data.length-1; j>data.length/2; j--)
{
sumRight += data[j];
if(sumRight == sumLeft)
{
System.out.println("Winner");
return;
}
}
}
System.out.println("Loser");
}
public static void main(String[] args) {
Random rand = new Random();
int n = rand.nextInt(7) + 3;
System.out.println(n + " elements in the array ");
int numbers[] = new int[n];
System.out.print("Elements:");
for(int i=0; i<n; i++)
{
numbers[i] = rand.nextInt(19) + 1;
System.out.print(numbers[i]);
if(i != n-1)
System.out.print(",");
}
System.out.println(" ");
publishResult(numbers);
}
}
OUTPUT:
6 elements in the array
Elements:11,1,12,18,9,3
Winner
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.