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

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

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

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

  • 1. Write a function named findTarget that takes three parameters: numbers, an array of integers -...

    1. Write a function named findTarget that takes three parameters: numbers, an array of integers - size, an integer representing the size of array target, a number The function returns true if the target is inside array and false otherwise 2. Write a function minValue that takes two parameters: myArray, an array of doubles size, an integer representing the size of array The function returns the smallest value in the array 3. Write a function fillAndFind that takes two parameters:...

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

  • 1.) Write a function called canMakeSum() that takes a sorted array of n integers and determines...

    1.) Write a function called canMakeSum() that takes a sorted array of n integers and determines whether two distinct elements of the array add up to some specified value, "key". If so, return 1. Otherwise, return 0. The function signature is: int canMakeSum(int *array, int n, int key);

  • /*    Write a function that takes as an argument the value x,    and returns...

    /*    Write a function that takes as an argument the value x,    and returns the result of the expression: 20x^4 - 12x^3 - 3x^2 + 15x + 3    Note: No importing; use the power function given to you, and use it    as a reference for writing functions (note also that main itself        if a function!). */ public class Exercise9_1 { public static void main(String[] args) { int num = 5, pow = 2; System.out.println(num...

  • Write a method that takes in a two-dimensional array of integers, do some calculation for each...

    Write a method that takes in a two-dimensional array of integers, do some calculation for each row in the two-dimensional array, and returns a one-dimensional array of sums for each row. Your code must work any array dimensions, including jagged arrays. The calculations are: • Sum of each row • Maximum value of each row • Minimum value of each row • Average of each row Here is an example an array passed and returns an array of sums for...

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
ADVERTISEMENT