• Post Reply Bookmark Topic Watch Topic
  • New Topic

Arrays within arrays  RSS feed

 
James Russel
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?!

 
Campbell Ritchie
Marshal
Posts: 56521
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Knute Snortum
Sheriff
Posts: 4270
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That clears it up. Thanks a lot, I still need to get the hang of instantiating and intializing!
 
Campbell Ritchie
Marshal
Posts: 56521
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
Roel has explained about reassigning. I may have been mistaken about your line 19.
 
James Russel
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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: 4270
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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: 4270
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Okay, let's see the whole method plus a main() with an example of the out of bounds error.
 
James Russel
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I haven't yet written a main, but;
 
Fred Kleinschmidt
Bartender
Posts: 571
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What is k ? is it the length of the a and x arrays?

 
James Russel
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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: 4270
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
J. Kevin Robbins
Bartender
Posts: 1801
28
Chrome Eclipse IDE Firefox Browser jQuery Linux MySQL Database Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!