Perry Smith
Java Questions & Answers
Home /
Java Q & A Archive Page 3 /
Java Q & A Archive
More Arrays
Sunday, 17-Jan-99 16:08:16
207.179.135.63 writes:
Hello again. I forgot to add that data items are read into the two arrays, x and y.
Then, i have to store the product of corresponding elements of x and y in a third
array,z. How do i go about multiplying arrays? Can anyone give me the codes? After
that, i have to compute and display the square root of the sum of the items in z.
Please help me here.
Thanks a lot!
AG
Response
Re: More Arrays
Sunday, 17-Jan-99 19:34:19
24.1.16.214 writes:
you don't say how many dimensions, the size of the
array, or what type of data the arrays hold...
here is a solution for one-dimensional arrays of
a fixed number of type int data:
// create a class that has these variables
static int ARRAYELEMENTS = 4;
int [] x, y, z;
x = new int [ARRAYELEMENTS];
y = new int [ARRAYELEMENTS];
z = new int [ARRAYELEMENTS];
x = {1, 2, 3, 4};
y = {4, 3, 2, 1};
z = multiplyArrays (x, y);
// this would be a method in the class you create
public int [] multiplyArrays(int [] a, int [] b)
{
for (int i = 0; i < ARRAYELEMENTS; i++)
{
a[i] *= b[i];
}
return a;
}
good luck...
Home /
Java Q & A Archive Page 3 /
Java Q & A Archive