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

How can this program be rewritten to also find the sum of the remaining numbers

ID: 3683233 • Letter: H

Question

How can this program be rewritten to also find the sum of the remaining numbers in the list during each possible sum calculation?

public class PossibleSums {

public static void getAllSums(int array[], int startingValue, int pos) {

for (int i = pos; i < array.length; i++) {

int currentValue = startingValue + array[i];

System.out.println(currentValue);

getAllSums(array, currentValue, i + 1); } }

public static void main(String[] args)

{ int array[] = {1, 2, 4, 5};

System.out.println("0");

getAllSums(array, 0, 0); // Test array } }

Explanation / Answer

//You mean like this?

/* package whatever; // don't place package name! */

import java.util.*;
import java.lang.*;
import java.io.*;

class PossibleSums {

public static int total(int array[])
{
   int sum=0;
   for (int i = 0; i < array.length; i++)
   {
       sum += array[i];
   }
   return sum;
}
public static void getAllSums(int array[], int startingValue, int pos) {
for (int i = pos; i < array.length; i++) {
int currentValue = startingValue + array[i];
System.out.println(currentValue+" "+(total(array)-currentValue));
getAllSums(array, currentValue, i + 1); } }


public static void main(String[] args)
{ int array[] = {1, 2, 4, 5};
System.out.println("0");
getAllSums(array, 0, 0); // Test array
} }

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