Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Please actually answer it... In this assignment, you are to write a java program

ID: 3585934 • Letter: P

Question

Please actually answer it...

In this assignment, you are to write a java program that reads an array of integers of a certain size. So,
first it will as the user to input the size of the array (let’s call this size), then it proceeds to read the
elements of the array (array). Do not assume a maximum number of elements in the array. You should
input the numbers in the array in sorted order all the time. If the user inputs the number in a non-sorted
order, then the program should print an error message an exit. Therefore, you are required to check to
see if the array is sorted (you need to implement a recursive function that checks to see if the array is
sorted).
Once the array is read in and verified as sorted, the user should be asked to input a single integer (let’s
call this one S). Then you are required to write a recursive function that checks to see if the array
contains two numbers that add up to the number S or not. If so, then your program should print a
message indicating that yes the array does have two numbers that add up to S, otherwise, the program
should print a message indicating no it doesn’t.
Your program should continue to perform the above tasks until a user inputs in an array of size 0, then
the program should stop.
The requirements:
• You only need one .java program
• You must have two recursive functions in addition to main:
o One to take the array as an argument and check if it’s sorted.
o Another to take an array and an integer as arguments and returns if the array has two
numbers that add up to the integer.
• Your program must run indefinitely until a 0 is entered for the size to allow you to test with
various test cases.
• Make sure that your program is well documented (commented very well), and is as user-friendly
as possible.
• You must submit your source code only (the .java file)
The following scenario should help you design your program:
Please Enter a Size or Zero to stop: 5
Please Enter an array of 5 elements: 3 8 9 10 14
Please Enter a single Integer: 18
Answer: Yes the array contains two numbers that add up to 18.
----
Please Enter a Size or Zero to stop: 6
Please Enter an array of 5 elements: 3 8 9 10 14 7
***** Error the array must be sorted.
----
Please Enter a Size or Zero to stop: 6
Please Enter an array of 5 elements: 3 8 9 10 14 18
Please Enter a single Integer: 25
Answer: No the array does not contain two numbers that add up to 25.
---
Please Enter a Size or Zero to stop: 0
Good bye
Now the program exits.

Explanation / Answer

// I have added this code to an online gist. https://gist.github.com/anonymous/3e6ccc6dc1988029c499e2ceff812afb

import java.util.Scanner;

public class Sum {

/**

* Checks if 2 numbers in the array form the given sum.

* arr - array of numbers

* start - start index of array.

* end - last index of array.

* sum - the desired sum to be found

*/

private static boolean hasSum(int [] arr, int start, int end, int sum) {

if(start < end) {

// Numbers found

if (arr[start] + arr[end] == sum) {

return true;

}

if (arr[start] + arr[end] > sum) {

return hasSum(arr, start, end-1, sum);

}

if (arr[start] + arr[end] < sum) {

return hasSum(arr, start+1, end, sum);

}

}

// Termination case

return false;

}

/**

* Checks if the array is sorted.

* arr - array of numbers

* start - start index of array.

* end - last index of array.

*/

private static boolean isSorted(int[] arr, int start, int end){

if (start<end) {

return (arr[start] <= arr[start+1]) && isSorted(arr, start + 1, end);

}

return true;

}

public static void main(String[] args) {

int size, index, sum;

Scanner sc=new Scanner(System.in);

while(true) {

System.out.println("Please Enter a Size or Zero to stop: ");

size = sc.nextInt();

// Terminate Program if size is 0

if (size == 0) {

System.out.println("Good bye");

break;

}

// Create a array of given size

int[] arr = new int[size];

for(index=0; index<size; index++) {

arr[index] = sc.nextInt();

}

if(isSorted(arr, 0, arr.length-1)) {

System.out.println("Please Enter a single Integer");

sum = sc.nextInt();

if (hasSum(arr, 0, arr.length-1, sum)) {

System.out.println("Yes the array contains two numbers that add up to " + sum);

} else {

System.out.println("No the array does not contain two numbers that add up to " + sum);

}

} else {

System.out.println("Error the array must be sorted.");

}

}

}

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote