Question

Write a function that takes an array of integers as an argument and returns a value...

  1. Write a function that takes an array of integers as an argument and returns a value based on the sums of the even and odd numbers in the array. Let X = the sum of the odd numbers in the array and let Y = the sum of the even numbers. The function should return X – Y

The signature of the function is:
int f(int[ ] a)

Examples

if input array is return
{1} 1
{1, 2} -1
{1, 2, 3} 2
{1, 2, 3, 4} -2
{3, 3, 4, 4} -2
{3, 2, 3, 4} 0
{4, 1, 2, 3} -2
{1, 1} 2
{} 0

by java program

0 0
Add a comment Improve this question Transcribed image text
Answer #1
public class EvenOddSumDiff {

    public static int f(int[] a) {
        int X = 0, Y = 0;
        for (int i = 0; i < a.length; i++) {
            if (a[i] % 2 == 0) {
                Y += a[i];
            } else {
                X += a[i];
            }
        }
        return X - Y;
    }

    public static void main(String[] args) {
        System.out.println(f(new int[]{1}));
        System.out.println(f(new int[]{1, 2}));
        System.out.println(f(new int[]{1, 2, 3}));
        System.out.println(f(new int[]{1, 2, 3, 4}));
        System.out.println(f(new int[]{3, 3, 4, 4}));
        System.out.println(f(new int[]{3, 2, 3, 4}));
        System.out.println(f(new int[]{4, 1, 2, 3}));
        System.out.println(f(new int[]{1, 1}));
        System.out.println(f(new int[]{}));
    }
}

Add a comment
Know the answer?
Add Answer to:
Write a function that takes an array of integers as an argument and returns a value...
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
  • (JAVA) Given an array of unique positive integers, write a function findSums that takes the array...

    (JAVA) Given an array of unique positive integers, write a function findSums that takes the array input and: 1. Creates a hashtable called “hashT” and inserts all the elements of the input array in the hashtable. 2. Finds pairs of elements in the hashtable whose sum is another element (sum) in the hashtable and print the pairs in the console. 3. Returns another hashtable names “sums” of those sum elements. For example: Input: [1,5,4,6,7,9] Output: [6,5,7,9] Explanation: 6 = 1...

  • IN C++ Write a function numberOfOddNumbers that takes an array of integers and its size (length)...

    IN C++ Write a function numberOfOddNumbers that takes an array of integers and its size (length) as parameters and then returns the number of the odd numbers found in the array. For example, if an array called list stores the following values: int list []= { 1,1, 8,6,9,4,3,9595,0 }; Then the call of numberOfOddNumbers (list,9) should return 5. If instead, the list had stored these values: int list2[] = { 2, 4, 6, 8, 10, 208 }; Then the call...

  • 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...

  • JAVA Code: Complete the method, sumOdds(), below. The method takes in an array of integers, numbers,...

    JAVA Code: Complete the method, sumOdds(), below. The method takes in an array of integers, numbers, and returns the sum of all the odd numbers in the array. The method should return 0 if the array contains no odd numbers. For example, if the given array is [12, 10, 5, 8, 13, 9] then the method should return 27 (5+13+9=27). Starter Code: public class Odds { public static int sumOdds(int[] numbers) { //TODO: complete this method    } }

  • In Java* ​​​​​​​ Write a program that reads an arbitrary number of 20 integers that are...

    In Java* ​​​​​​​ Write a program that reads an arbitrary number of 20 integers that are in the range 0 to 100 inclusive. The program will ask a user to re-enter an integer if the user inputs a number outside of that range. The inputted integers must then be stored in a single dimensional array of size 20. Please create 3 methods: 1. Write a method public static int countEven(int[] inputArray) The method counts how many even numbers are in...

  • 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...

  • 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...

  • In Java Write a program that reads an arbitrary number of 25 integers that are positive...

    In Java Write a program that reads an arbitrary number of 25 integers that are positive and even. The program will ask the user to re-enter an integer if the user inputs a number that is odd or negative or zero. The inputted integers must then be stored in a two dimensional array of size 5 x 5. Please create 3 methods: 1. Write a method public static int sum2DArray( int [1] inputArray ) The method sums up all elements...

  • Using Python3 Write a function that takes an array of integers and returns it in increasing...

    Using Python3 Write a function that takes an array of integers and returns it in increasing order. There can be negative numbers, but they are treated as positive numbers. Example list= list=[-6,7,6,7,-9,1,0,-3,-2,1,4] Answer list= [0, 1, 1, -2, -3, 4, 6, -6, 7, 7, 3]

  • Write a function that takes an int as an argument and returns true if the int...

    Write a function that takes an int as an argument and returns true if the int is an even number and false if not. In any case change the value of the original argument to twice its value. Use the integers 0 through 4 as test cases. Do not use any arrays.

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