Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6892409
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T06:31:54+00:00 2026-05-27T06:31:54+00:00

When I run the below program I am getting the following errors: (any ideas?)

  • 0

When I run the below program I am getting the following errors: (any ideas?)

Exception in thread "main" java.lang.StackOverflowError
at SumArray.binarySum(SumArray.java:31)
at SumArray.binarySum(SumArray.java:34)

Here is the code: I need to generate an array of random integers and use binary and linear recursion to add the values thereof…

import java.io.*;
import java.util.ArrayList;
import java.util.Random;

class SumArray {
    static int floor = 100000;
    static int ceil = 0; 
    int result;
    int half; 
    int half2;

    //Linear Recursion
    int sum(int a[], int n) {
        if (n == 1)
            return a[0];
        else {
            result = sum(a, n - 1);
            result = result + a[n - 1];
            return result;
        }
    }

    //Binary Recursion
    int binarySum(int a[], int i, int n) {
        if (n == 1)
            return a[0];

        return binarySum(a, i, ceil/2) + binarySum(a, i + ceil/2, floor/2);
    }

    public static void main(String args[]) throws IOException {

        //Define ArrayList to hold Integer objects
        ArrayList<Integer> numbers = new ArrayList<Integer>();

        // User determines number of elements
        DataInputStream dis = new DataInputStream(System.in);
        System.out.println("Hello! Please enter the size of your array: ");
        int n = Integer.parseInt(dis.readLine());

        // Generate specified number of random integers
        Random randNumGenerator = new Random();
        int[] x = new int[n];

        // While still less than indicated number.. add more
        for (int i = 0; i < x.length; i++) {
            x[i] = (randNumGenerator.nextInt(100000));
            numbers.add(x[i]);

            //calculate min and max values
            if(ceil < x[i]) {
                ceil = x[i];
            }
            if(floor > x[i]) {
                floor = x[i];
            }
        }

        SumArray a1 = new SumArray();

        // Print array values
        System.out.println("Array values inlude: " + numbers);

        // Print the sum
        System.out.println("The sum of the array using Linear Recursion is: " + a1.sum(x, n));

        // Print the binary sum
        System.out.println("The sum of the array using Binary Recursion is: " + a1.binarySum(x, n, n));

        System.out.println("Ceiling: " + ceil);
        System.out.println("Floor: " + floor);
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T06:31:55+00:00Added an answer on May 27, 2026 at 6:31 am

    You’re recursively calling your BinarySum method with it’s third parameter as ceil/2, and floor/2 which will never change. ceil/2 is 0 and floor/2 is 50000, neither of these is 1, so you’re getting infinite recursion. Seems like you probably want those values to be different for each recursive call…

    My Java is a little rusty (maybe somebody more used to coding in Java can clean this up), but as an example, try something like this:

    class Accumulator {
      static int binarySum(int a[]) {
        return binarySum(a, 0, a.length-1);
      }
    
      static int binarySum(int a[], int s, int e) {
        if (e-s == 0) return a[s];
        int mid = s + ((e-s)/2);
        return binarySum(a, s, mid) + binarySum(a, mid+1, e);
      }
    
      public static void main(String args[]) {
        int[] vals = {2,3,4,5,6,7};
        System.out.println(binarySum(vals));
      }
    }
    

    For each call we split what we’re searching into two halves and recurse, until we get down to a single item.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Any ideas given the code below why the highlight is being triggered to run
I am getting the below exception when I run my ASP.NET/C#/SQL project on an
I'm getting this exception when I try to run this program. It's one of
I'm getting a ton of linker errors (included below) when I run my C++
I am getting a weird exception when I exit the program. This has started
I'm obviously not quite getting the 'end-of-file' concept with C++ as the below program
I have a C program below written on UNIX. I am getting segmentation fault.
I wanted to print 100 as output in the below program. I am getting
I'm running a simple Java program with below directory structure: MyProject (A project in
When I run the program below, it prints one: 1, instead of one :

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.