Question

In: Computer Science

In the java programming language. How would you find if numbers in an array element add...

In the java programming language.

How would you find if numbers in an array element add up to a certain sum so for example if my array element consists of: INPUT: 200, 10, 50, 20 and my output asks if these elements add to: 210? YES (200+10) 260? YES (200+10+50) 30? NO What method would you suggest to look through the elements in an array (which im going to add through a text file) and determine if these sums exist in O(n^2)?

Solutions

Expert Solution

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;

public class SumChecker {
  
private static final String FILENAME = "array_nums.txt";
  
public static void main(String[] args) {
Scanner fileReader;
int arr[] = null;
int count = 0;
try
{
fileReader = new Scanner(new File(FILENAME));
  
// 1st line contains all the elements of the array separated by comma
String[] data = fileReader.nextLine().trim().split(",");
arr = new int[data.length];
for(String s : data)
arr[count++] = Integer.parseInt(s);
  
// second line contains all the sums to be checked separated by comma
String[] sumData = fileReader.nextLine().trim().split(",");
for(String sumD : sumData)
{
checkSum(arr, Integer.parseInt(sumD));
}
fileReader.close();
}catch(FileNotFoundException fnfe){
System.out.println(FILENAME + " could not be found! Exiting..");
System.exit(0);
}
}
  
private static void checkSum(int arr[], int sum)
{
int sumTemp = sum;
int arrTemp[] = new int[arr.length];
int count = 0;
for(int i = 0; i < arr.length; i++)
{
if((sumTemp - arr[i]) >= 0)
{
sumTemp = sumTemp - arr[i];
arrTemp[count++] = arr[i];
}
}
  
int add = 0;
for(int i = 0; i < count; i++)
add += arrTemp[i];
if(add == sum)
{
System.out.print(sum + " : YES (");
for(int i = 0; i < count; i++)
{
if(i == count - 1)
System.out.println(arrTemp[i] + ")");
else
System.out.print(arrTemp[i] + " + ");
}
}
else
System.out.println("Sorry, no such elements add up to a sum of " + sum);
}
}

******************************************************** SCREENSHOT ********************************************************


Related Solutions

In the java programming language. How would you find if THREE (3) numbers in an array...
In the java programming language. How would you find if THREE (3) numbers in an array add up to a certain sum so for example if my array element consists of: INPUT: 200, 10, 50, 20 and my output asks if these elements add to: 270? YES (200+50+70) 260? YES (200+10+50) 30? NO What method would you suggest to look through the elements in an array (which im going to add through a text file) and determine if these sums...
How would you find if numbers in an array element add up to a certain sum...
How would you find if numbers in an array element add up to a certain sum so for example if my array element consists of: INPUT: 200, 10, 50, 20 and my output asks if these elements add to: 210? YES (200+10) 260? YES (200+10+50) 30? NO What method would you suggest to look through the elements in an array (which im going to add through a text file) and determine if these sums exist in O(n^2)?
Programming Language: JAVA In this assignment you will be sorting an array of numbers using the...
Programming Language: JAVA In this assignment you will be sorting an array of numbers using the bubble sort algorithm. You must be able to sort both integers and doubles, and to do this you must overload a method. Bubble sort work by repeatedly going over the array, and when 2 numbers are found to be out of order, you swap those two numbers. This can be done by looping until there are no more swaps being made, or using a...
Given an array of numbers, find the index of the smallest array element (the pivot), for...
Given an array of numbers, find the index of the smallest array element (the pivot), for which the sums of all elements to the left and to the right are equal. The array may not be reordered. Example arr=[1,2,3,4,6] the sum of the first three elements, 1+2+3=6. The value of the last element is 6. Using zero based indexing, arr[3]=4 is the pivot between the two subarrays. The index of the pivot is 3. Function Description Complete the function balancedSum...
In Java Find the second largest and second smallest element in a given array. You can...
In Java Find the second largest and second smallest element in a given array. You can hardcode/declare the array in your program.
IN JAVA Create an array and add random values to it, and then find the sum...
IN JAVA Create an array and add random values to it, and then find the sum of the values using recursion.
JAVA programming language Please add or modify base on the given code Adding functionality Add functionality...
JAVA programming language Please add or modify base on the given code Adding functionality Add functionality for multiplication (*) Adding JUnit tests Add one appropriately-named method to test some valid values for tryParseInt You will use an assertEquals You'll check that tryParseInt returns the expected value The values to test: "-2" "-1" "0" "1" "2" Hint: You will need to cast the return value from tryParseInt to an int e.g., (int) ValidationHelper.tryParseInt("1") Add one appropriately-named method to test some invalid...
Write a Java Program that can:​ Remove a particular element from an array.​ Add a new...
Write a Java Program that can:​ Remove a particular element from an array.​ Add a new element to an array.​ Change an element with the new one.​ Search for a particular element in the array.​ ​The code must have four separate methods for each task stated above.​ Do not use any pre-defined Java functions.​ You are free to use int or String data-type for the array.​
Java Programming I need an application that collects the user input numbers into an array and...
Java Programming I need an application that collects the user input numbers into an array and after that calls a method that sums all the elements of this array. and display the array elements and the total to the user. The user decides when to stop inputting the numbers. Thanks for your help!
The programming language is Java. Write the code you would use to fill the variable cubed...
The programming language is Java. Write the code you would use to fill the variable cubed with the value stored in x to the third power.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT