8 Replies Latest reply: Nov 6, 2007 12:18 AM by 807603

# Array help

how would you make a program with two arrays that calculates the values of the two areas. The sum and product.
Here is an example of two arrays that I need to find the sum and product.
int a[] = {  1,  2,  3,  4,  5,  6,  7,  8,  9, 10 };
int b[] = { 11, 12, 13, 14, 15, 16, 17, 18, 19, 20 };
Should I use some kind of loop or something?
• ###### 1. Re: Array help
Probably.

It would help to know how you want or have been told to calculate the sum and product using those arrays. For example, you could take the product of values in corresponding positions in the arrays. Or, you could take the product of all the values in a single array. Or, you could take the product of the lengths of the arrays.

What did the assignment say to do, exactly?
• ###### 2. Re: Array help
it seems like you have a vauge understanding of what an array is, but where your knowledge is lacking is how to use them. What you gotta understand is that when the program hears a[0] it finds the first value in the arrray. In this case it is 1. so a[1] =2, a[2] =3...

so if you want to add up the sum of them in this closed situation you should just be able to go
sumA = a[0]+a[1] a[2]a[3]+a[4]+a[5]+a[6]+a[7]+a[8]+a[9]

however thats a terrible solution and a simple loop would work perfectly but i'll leave that part up to you
• ###### 3. Re: Array help
here is what the assignment is:
Write a program that reads data from two arrays a[ ] and b[ ], both of type integer and containing 10 elements, and calculates the values of two arrays, sum[ ] and product[ ], which contain the corresponding sum and product of the a and b arrays.
sum[i] = a[i] + b;      product[i] = a[i] * b[i]
• ###### 4. Re: Array help
Use a for loop. Use the index variable of the loop to index both input arrays as well as the sum and product arrays.
• ###### 5. Re: Array help
some like this?:

for(int i= 0 ;i<len1;i++){
a3[i] = a1[i] + a2;
a3[i] = a1[i] * a2[i];
}
• ###### 6. Re: Array help
Yeah, but you probably don't want to assign the results of both calculations to the same array.

Also, when you post code, wrap it in [code][/code] tags so it's more legible.
• ###### 7. Re: Array help
This is not working, can you tell me whats wrong with it?
``````public class help {
public static void main (String args[] ){

int[] a1 = {4,27,7,4,7};
int[] a2 = {1,2,3,4,5};

int len1= a1.length;
int len2= a2.length;

int sum, product;
for(int i= 0 ;i<len1;i++){
sum = a1[i] + a2;
product = a1[i] * a2[i];
}

System.out.println(sum);
}

}
``````
• ###### 8. Re: Array help
It looks like you put a space or something in the code tags, thus preventing it from formatting your code properly, thus making it hard to read.

But it looks like:

1) You never declare sum or product.

2) you assign the sum of each pair of numbers to sum (similarly with product), overwriting any previous value. You probably want to add the sum of each pair, as in:
``````sum += a1[i] + a2;
which means, add a1 and a2, and add the result to whatever value sum has, and assign the result to sum.

Or maybe you meant to do this?
sum[i] = a1[i] + a2;
in which case, you'd end up with an array of sums.
Note that in this case you'd need to declare sum differently than before. ``````