Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 8 of 8

Thread: Array container is not enough to hold all needed values

  1. #1
    Junior Member
    Join Date
    Mar 2018
    Posts
    8
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Array container is not enough to hold all needed values

    this program finds fibonacci sequence starting at 1 then separate the 2 digit numbers so that it's not too big then factorial the numbers and add them. my problem is that its missing the final value in the sequence because the array is not big enough.

    int sum = 1;
        int input;
     
        System.out.print("Enter a number: ");
        input = console.nextInt();
     
        int[] fib = new int[input];
        int[] out = new int[input];
        int[] fib2 = new int[input];
     
        fib[0] = 0;
        fib[1] = 1;
     
        fib2[0] = 0;
        fib2[1] = 1;
        if (input > 0) {
     
            if (input <= 7) { // for numbers 1,1,2,3,5,8 
                for (int i = 2; i < input; i++) {
                    fib[i] = fib[i - 1] + fib[i - 2];
                    out[i] = factorial(fib[i]);
     
                }
            } else { // this is for the 2 digit numbers 13,21,34,55,89
                for (int i = 2; i < input; i++) {
                    fib[i] = fib[i - 1] + fib[i - 2];
                    fib2[i] = fib2[i - 1] + fib2[i - 2];
                }
                for (int i = 2; i < input; i++) {
                    fib[i] = separate(fib[i]);
                    fib2[i] = separate2(fib2[i]);
                    out[i] = factorial(fib[i]) + factorial(fib2[i]);
                }
     
            }
     
            for (int i : out) {
                sum = sum + i;
            }
            System.out.println(sum);

    output is 40,490 but it should be 40,730 all is working except that if I input 10 it's missing the final value 55. computation is like this 1!+1!+2!+3!+5!+8!+1!+3!+2!+1!+3!+4!+5!+5! = 40,730

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Array container is not enough to hold all needed values

    How does the size of the array relate to the number that the user inputs? Right now the size is the same as the user's input.
    Can the size of the array be computed from the user's input?
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Mar 2018
    Posts
    8
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Array container is not enough to hold all needed values

    if for example the user input 10 first the program would calculate the first 10 fibonacci sequence 1,1,2,3,5,8,13,21,34,55 then it will separate the 2 digit numbers into 1 digit numbers so its not too big when we factorial it so the actual value is 1,1,2,3,5,8,1,3,2,1,3,4,5,5 the we factorial each 1!,1!,2!,3!,5!,8!,1!,3!,2!,1!,3!,4!,5!,5! then we add its results 1!+1!+2!+3!+5!+8!+1!+3!+2!+1!+3!+4!+5!+5! = 40,730, but my program does not include the last 5!+5! into the array so the result is only 40,490 instead of 40,730

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Array container is not enough to hold all needed values

    I don't see where you answered this question:
    Can the size of the array be computed from the user's input?
    For example: The array's size should be the user's input plus one.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Mar 2018
    Posts
    8
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Array container is not enough to hold all needed values

    you mean like this ?
    input = console.nextInt() + 1;
    that would make the
    if (input <= 7)
    wrong and the else statement right

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Array container is not enough to hold all needed values

    You don't have to change the value of input.
    I was suggesting that the size of the array be changed: ... = new int[input+extra]
    where extra is the number of extra slots required for the value of input.
    You would need a formula to compute the value of extra.
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Junior Member
    Join Date
    Mar 2018
    Posts
    8
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Array container is not enough to hold all needed values

    sorry but what do you mean compute value of extra? i can't visualize what your saying , can you show me?

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Array container is not enough to hold all needed values

    extra = someMethod(input); // compute value of extra using value of input

    I have no idea what logic would go in someMethod. Did the assignment describe what the program needs to do?
    Where did the value 7 used in the if statement come from?

    Why isn't the array large enough? How much larger does it need to be? That would determine the value of extra.

    What happens if extra is set to input*5? Do all the values now fit in the array?
    The contents of someMethod would be:
    return input*5;
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. how to find 2nd largest array if array values like{10,20,92,81,92,34}
    By anjijava16 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: November 2nd, 2013, 04:59 PM
  2. Array Help Needed- Testing if ArrayIsFull & Displaying Most Recently added Array data
    By FundamentalLearner in forum What's Wrong With My Code?
    Replies: 4
    Last Post: October 21st, 2013, 06:44 AM
  3. Help needed with array
    By Pacina in forum What's Wrong With My Code?
    Replies: 16
    Last Post: March 21st, 2013, 11:14 AM
  4. Array help needed
    By The Lost Plot in forum Collections and Generics
    Replies: 5
    Last Post: March 12th, 2010, 12:22 PM
  5. Array program help needed
    By SCM in forum Loops & Control Statements
    Replies: 2
    Last Post: December 2nd, 2009, 11:28 PM