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

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

1. ## 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;
fib = 1;

fib2 = 0;
fib2 = 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  Reply With Quote

2. ## 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?  Reply With Quote

3. ## 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  Reply With Quote

4. ## 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.  Reply With Quote

5. ## 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  Reply With Quote

6. ## 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.  Reply With Quote

7. ## 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?  Reply With Quote

8. ## 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;  Reply With Quote