programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
• Campbell Ritchie
• Tim Cooke
• Devaka Cooray
• Ron McLeod
• Jeanne Boyarsky
Sheriffs:
• Liutauras Vilda
• paul wheaton
• Junilu Lacar
Saloon Keepers:
• Tim Moores
• Stephan van Hulst
• Piet Souris
• Carey Brown
• Tim Holloway
Bartenders:
• Martijn Verburg
• Frits Walraven
• Himai Minh

# Arrays within arrays

Greenhorn
Posts: 7
• Number of slices to send:
Optional 'thank-you' note:
I am currently learning java and I'm having a bit of trouble working with arrays. In this example I'm taking two arrays (of length k) and producing another from the sums of their products. The code compiles and seems to be okay apart from the fact that sum stays at 0 instead of adding terms, as it should. Any help?!

Marshal
Posts: 76870
366
• Number of slices to send:
Optional 'thank-you' note:
Welcome to the Ranch

Why are you reassigning the two parameters? Are you sure you want to assign to the n‑th index of the array? Shouldn't any assignments be inside the loop? Why k+1 rather than k?

James Russel
Greenhorn
Posts: 7
• Number of slices to send:
Optional 'thank-you' note:

Campbell Ritchie wrote:Welcome to the Ranch

Why are you reassigning the two parameters? Are you sure you want to assign to the n‑th index of the array? Shouldn't any assignments be inside the loop? Why k+1 rather than k?

In reverse order: because the result of the two arrays (I'll use as an example a={1,2,3,2,1} and x={1,1,2,1,3}) would be one longer than the original arrays (here, xk would be {1,1,3,7,10,14}).
As for assignments, the second loop gets the terms. It gets xk[n], and the first loop cycles through the 'n's.
I think that answers the second question as well.
How am I reassigning the parameters?

Sheriff
Posts: 7123
184
• Number of slices to send:
Optional 'thank-you' note:
static double[] solveGeneric (double[] a, double[] x, int k)

Above you have a[] and x[] as parameters.

x = new double[k];
a = new double[k];

Here you are initializing the parameters a and x. Now instead of having the values passed into the method, they are both arrays, k in length, of 0.0s. So when you come to sum them, it is just 0.0 * 0.0.

James Russel
Greenhorn
Posts: 7
• Number of slices to send:
Optional 'thank-you' note:
That clears it up. Thanks a lot, I still need to get the hang of instantiating and intializing!

Campbell Ritchie
Marshal
Posts: 76870
366
• Number of slices to send:
Optional 'thank-you' note:

James Russel wrote:. . . because the result of the two arrays . . . would be one longer than the original arrays . . .

I never realised that. Thank you. You may not need the parameter k because you have two arrays each with a .length field. I think you can probably use that .length field instead.

James Russel
Greenhorn
Posts: 7
• Number of slices to send:
Optional 'thank-you' note:
So I've adapted the previous code to do something slightly different and all is (mostly) well. Only thing is, I have an out of bounds error and I can't for the life of me see why, probably because I've used a few different things to determine bounds.

Knute Snortum
Sheriff
Posts: 7123
184
• Number of slices to send:
Optional 'thank-you' note:
for (int n = a.length; n < k; n++)

Do you really want to start n at the length of a and increase n? It might help if you posted to complete method and maybe a short main() testing the method.

James Russel
Greenhorn
Posts: 7
• Number of slices to send:
Optional 'thank-you' note:
n can't be started as less than a.length because the method takes two input arrays, x and a which are of equal length where x is the initial values.

For an example, if the inputs have a.length = 3 and k = 5, with x0 x1 and x2 given, it should give x3 = a0x2 + a1x1 + a2x0 and x4 = a0x3 + a1x2 + a2x1 in an array.

Knute Snortum
Sheriff
Posts: 7123
184
• Number of slices to send:
Optional 'thank-you' note:
Okay, let's see the whole method plus a main() with an example of the out of bounds error.

James Russel
Greenhorn
Posts: 7
• Number of slices to send:
Optional 'thank-you' note:
I haven't yet written a main, but;

Bartender
Posts: 732
10
• Number of slices to send:
Optional 'thank-you' note:
What is k ? is it the length of the a and x arrays?

James Russel
Greenhorn
Posts: 7
• Number of slices to send:
Optional 'thank-you' note:
k is the number of iterations you want. If you give it 2 initial conditions and want a sequence of k=5 values, it will compute from xk[2] to xk[4] since it knows xk[0]=x0 and xk[1]=x1

EDIT: i've just realised the method isn't quite right as it doesn't give the initial conditions in the output, I'm not sure if this may be causing a bounds error, though?
EDIT 2: so this is what it is now, although the error still exists.

Knute Snortum
Sheriff
Posts: 7123
184
• Number of slices to send:
Optional 'thank-you' note:
Here is the main() I used to call your method. I have no idea if it's valid or not. (Nevermind that your code has been reformatted. That is just what my IDE does. Your format is fine.)

Notice that if n = 3, then x[2] is accessed, which is out of bounds.

Bartender
Posts: 1810
28
• Number of slices to send:
Optional 'thank-you' note:

James Russel wrote:

This just makes me cringe. Please use meaningful variable names. Your code should read like a story. That means words, not individual letters.

There is no advantage to using short names like this.

Bartender
Posts: 10780
71
• Number of slices to send:
Optional 'thank-you' note:

James Russel wrote:k is the number of iterations you want. If you give it 2 initial conditions and want a sequence of k=5 values, it will compute from xk[2] to xk[4] since it knows xk[0]=x0 and xk[1]=x1...

Hunhh??

My advice: back up and explain WHAT you want to do, not how you want to do it,

So here's an idea: If we were in a Maths class instead of CS 101, and those two arrays were sets of numbers instead, what would your 'arraysv2()' function be called?

And if you can't find a single word, try to describe what it does in one sentence.

Winston

 He got surgery to replace his foot with a pig. He said it was because of this tiny ad: the value of filler advertising in 2021 https://coderanch.com/t/730886/filler-advertising