Question

1- takes a 1D integer array arr as a parameter and returns a boolean value. 2-...

1- takes a 1D integer array arr as a parameter and returns a boolean value.

2- The method returns true if the argument array contains two distinct values such that the sum of those two values is equal to the first element of the array arr. Otherwise, it returns false.

3- Assume that arr contains only positive integers (greater than 0). Sample runs provided below.

4- Sample runs provided below.

5- Complete and place your code in the Question 3 box in the Answer Packet. Any code on this page will not be considered as part of your solution.

Argument Array arr Return value

75 89 45 50 1 30 49 true

81 3 27 9 1 24 false

88 5 44 19 44 91 false

12 7 4 1 5 true

0 0
Add a comment Improve this question Transcribed image text
Answer #1

import java.util.Scanner;
public class Main
{
   public static void main(String[] args)
   {
       Scanner sc = new Scanner(System.in);
       int n,i;
       System.out.print("Enter number of elements: ");
       n = sc.nextInt();
       int arr[] = new int[n];
       for(i=0;i<n;i++)
       arr[i] = sc.nextInt();
       boolean res;
       res = calculate(arr,n);
       System.out.print(res);
   }
   public static boolean calculate(int arr[],int n)
   {
   int sum=0,count=0,i,j;
   for(i=1;i<n;i++)
   {
   for(j=i+1;j<n;j++)
   {
   if(arr[0]==arr[i]+arr[j]&&(arr[i]!=arr[j]))
   count++;
   }
   }
   if(count!=0)
   return true;
   else
   return false;
   }
}

Add a comment
Know the answer?
Add Answer to:
1- takes a 1D integer array arr as a parameter and returns a boolean value. 2-...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  •    /**    * Returns an array of booleans that are set true or    *...

       /**    * Returns an array of booleans that are set true or    * false based on the associated values in the array    * arr using the following rules:    * 1. If arr[i] is divisible by three then the boolean    * value in the the array returned at the same position    * should be true    * 2. Unless the values in arr[i] is also divisible by 5,    * then the value returned...

  • JAVA 1.Write a static method named getMaxEven(numbers) which takes an array of positive integers as a...

    JAVA 1.Write a static method named getMaxEven(numbers) which takes an array of positive integers as a parameter. This method calculates and returns the largest even number in the list. If there are no even numbers in the array, the method should return 0. You can assume that the array is not empty. For example: Test Result int[] values = {1, 4, 5, 9}; System.out.println(getMaxEven(values)); 4 System.out.println(getMaxEven(new int[]{1, 3, 5, 9})); 0 public static int --------------------------------------------------------------------------------- 2. Write a static method...

  • Write a method booleanisValid() that takes a list of 3 boolean values as argument and returns...

    Write a method booleanisValid() that takes a list of 3 boolean values as argument and returns true if any of the entries in the array is true, and false otherwise. This can be done in 4 lines (2 of the lines are { and }   )               Fill in the Solution:                              public static boolean isValid(______ _______, ______ _______, ______ _______)                              {                                 return(true);                              } Java language, thank you.

  • #include <assert.h> #include <stdio.h> #include <stdlib.h> // initialize_array is given an array "arr" of "n" elements....

    #include <assert.h> #include <stdio.h> #include <stdlib.h> // initialize_array is given an array "arr" of "n" elements. // It initializes the array by setting all elements to be "true" (any non-zero value). void initialize_array(int *arr, int n) { // TODO: Your code here. assert(0); } // mark_multiples is given an array "arr" of size n and a (prime) number "p" less than "n" // It assigns "false" (the zero value) to elements at array indexes 2*p, 3*p, 4*p,.., x*p (where x*p...

  • Python 3 Create a function that takes an array and an integer v, and returns the...

    Python 3 Create a function that takes an array and an integer v, and returns the number of v occurrences in the array. Add a variable Nsteps to count how many times the loops have executed and display that information in a message The main program must take an array / list 2D, call the function, and display the result. >>>l3 = [52, 14, 14, 8, 85, 69, 1, 77, 94, 96, 51, 65, 35, 32, 87, 92, 74, 47,...

  • In JAVA: Write a method that reverses the array passed the argument and returns this array....

    In JAVA: Write a method that reverses the array passed the argument and returns this array. Write a test program that prompts the user to enter ten numbers, invokes the method to reverse the numbers and display the numbers. Write a method that returns a new array by eliminating the duplicate values in the array using following method header: a. Public static int[] eliminateDuplicates(int list) b. Write a test program that reads in ten integers and invoke the method, the...

  • Write a method public static boolean FindSum (int[] a, int m) that takes an ascending sorted...

    Write a method public static boolean FindSum (int[] a, int m) that takes an ascending sorted array a with n distinct integers, and an integer m. If there are two elements in the array that add to be m, the method returns True; if not, it returns False. You may assume that the elements in the array are all distinct. There are several ways to solve this problem! (and some solutions are worth more points than others!) For 6 points,...

  • Our 1st new array operation/method is remove. Implement as follows: public static boolean remove( int[] arr,...

    Our 1st new array operation/method is remove. Implement as follows: public static boolean remove( int[] arr, int count, int key ) { 1: find the index of the first occurance of key. By first occurance we mean lowest index that contains this value. hint: copy the indexOf() method from Lab#3 into the bottom of this project file and call it from inside this remove method. The you will have the index of the value to remove from the array 2:...

  • C++ Single Dimensional Arrays

    Exercise #1: Design and implement a program (name it AssignGrades) that stores and processes numeric scores for a class. The program prompts the users to enter the class size (number of students) to create a single-dimensional array of that size to store the scores. The program prompts the user to enter a valid integer score (between 0 and 100) for each student. The program validates entered scores, rejects invalid scores, and stores only valid scores in the array.  The program...

  • JAVA~ 1. Write a static method named countOdd(my_array) that returns the number of odd integers in...

    JAVA~ 1. Write a static method named countOdd(my_array) that returns the number of odd integers in a given array. If there are no odd numbers in the array, the method should return 0. If the array is empty, the method should also return 0. For example: Test Result System.out.println(countOdd(new int[]{2, 3, 5, 6})); 2 System.out.println(countOdd(new int[]{2})); 0 System.out.println(countOdd(new int[]{})); 0 System.out.println(countOdd(new int[]{-7, 2, 3, 8, 6, 6, 75, 38, 3, 2})); 4 public static int ----------------------------------------------------------------------------------------------------------- 2. Write a static...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT