Win a copy of Functional Reactive Programming this week in the Other Languages forum!

Multiplying elements of two arrays together

Edward Amankwa
Greenhorn
Posts: 15
How can I multipy elements of two different arrays which are of the same length. However, I know the values for one array but the other I know the value of the last element. To find the values of the other array I have to multiply the last element values of both array and stored them in the last but one value of the array which I don't know the values. For example, for array D and Array C my output should look like this.

C D
3 4 Last element 3*4
4 12 4*12
2 48
2 96

Ken Blair
Ranch Hand
Posts: 1078
First of all your requirements are somewhat confusing, I'm not entirely sure what it is you're after. Regardless, it is apparent that there are several steps to solving your problem. What are you stuck on? What attempt(s) have you made thus far and where is the code you've started?

Edward Amankwa
Greenhorn
Posts: 15
I am playing around of how to do some multiplication involving arrays. The point is I know all the elements value of size[] and only the last element value of increased[]. Starting from the last values, I should muiltiply both of them and put the result in the last but one element of increased []and do the same till I put the last result in first element

package arrayoffset;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

/**
*
*
*/
public class arrayOffset extends JFrame implements ActionListener{
int dim = 5;
private String theOutput = "";
private int [] collectinput1 = new int[dim];
private int [] collectinput2 = new int[dim];
private int [] collectinput3 = new int[dim];
private JLabel [] enter;
private JLabel subscript;
private JTextField [] input1;
private JTextField [] input2;
private JTextField [] input3;
private JTextArea output;

/** Creates a new instance of arrayOffset */
public arrayOffset() {
// Collect the number of dimension from user
String num = JOptionPane.showInputDialog("Enter the number of dimension");
dim = Integer.parseInt(num);
Container con = getContentPane();
con.setLayout(new FlowLayout());
//layout components to collect data
enter = new JLabel [dim];
input1 = new JTextField[dim];
input2 = new JTextField[dim];

for(int i = 0; i<dim; i++)
{
enter [i]= new JLabel("Enter the upper and lower bounds for subscript:" + i);
con.add(enter[i]);
input1 [i] = new JTextField(2);
input1 [i].addActionListener(this);
con.add(input1[i]);
input2 [i] = new JTextField(2);
input2 [i].addActionListener(this);
con.add(input2[i]);
}
subscript = new JLabel("Enter a set of subscripts");
con.add(subscript);
input3 = new JTextField[dim];
for(int s=0; s<dim; s++ )
{
input3 [s] = new JTextField(2);
input3 [s].addActionListener(this);
con.add(input3[s]);
}
//add component to display data
output = new JTextArea(10, 25);
output.setEditable(false);
con.add(output);

setSize(450, 400);
setVisible(true);
}

public void actionPerformed(ActionEvent l)
{
int size [] = new int [dim];
int increased []= new int [dim];
//String [] myoutput = l.getActionCommand();

for(int j= 0; j<dim; j++)
{
collectinput1[j] = Integer.parseInt(input1 [j].getText());
collectinput2[j] = Integer.parseInt(input2 [j].getText());
collectinput3[j] = Integer.parseInt(input3 [j].getText());
size[j] = (collectinput2[j]+1)- collectinput1[j];

}/*I am stuck here I will like to multiply size[] and increased [] with the last element of increased being 4.*/
theOutput = "Index\tLower\t Upper\t Size\t Increased\n";

for(int d=0; d < dim; d++)
theOutput += d + "\t"+collectinput1[d] +"\t" + collectinput2[d]+"\t"+size[d]+"\n";

output.setText(theOutput);
}
}

Ken Blair
Ranch Hand
Posts: 1078
What's wrong with a[n] = size[i] * increased[increased.length -1] where a is the array you're putting this result into and i is the index of size you're working on currently. I suppose I just don't understand your requirements.

Edward Amankwa
Greenhorn
Posts: 15
If I use your code the result is all zeros, and I think if I can initialize increased.length-1 the result will change

Ken Blair
Ranch Hand
Posts: 1078
There's too much unformatted and not directly related code for me to sort through. I was presuming by "last element" you literally meant the last element in the array. Apparently that's not the case and the last element in the array is 0 because that's what the default value of an int is and multiplying anything by zero will result in zero. So you'll have to determine what the last element is. This is something you need to figure out based upon your requirements.

Edward Amankwa
Greenhorn
Posts: 15
Let assume the last element has a value of 4, how will I initialize the that element only.

Ken Blair
Ranch Hand
Posts: 1078
I don't understand the question. increased[increased.length - 1] = 4 would change the value of the very last element in the array increased. The real question is how do you know which element you should be multiplying?

Edward Amankwa
Greenhorn
Posts: 15
What I meant is if last element is 4 in increased[]. This four should be multiplied to the last element in size [] and the result is the value of the last but one in incraesed and it go that way till the last result is put into the first element of increased [].

Ken Blair
Ranch Hand
Posts: 1078

Something like that?

Edward Amankwa
Greenhorn
Posts: 15
Thank you very much for your help.It is working and I will apprecaite it if you can explain a little further.

Ken Blair
Ranch Hand
Posts: 1078
Originally posted by Edward Amankwa:
Thank you very much for your help.It is working and I will apprecaite it if you can explain a little further.

First of all, this might be easier to understand:

That's exactly the same it just doesn't use a ternary operator which you may not be familiar with. In the problem you described you wished to iterate over an array, call it 'a' which was the same size as another array, call it 'b'. For each element you wanted to multiply the value in a by the value in b and assign it to the next element in b. When on the very last element of the two arrays you wanted to assign the result to the first element of b. That means there are several steps here:

1. Iterate over a.
2. For each element, multiply the current element in a by the current element in b.
3. If the current element is not the last element in the arrays then assign the result to the next element in the b. Otherwise assign the result to the first element in b.

Edward Amankwa
Greenhorn
Posts: 15
Thank you again Ken for your explaination. Since I am playing around I am wondering if incresed is initialized this way increased []= {0,0,0,4}.I tried that and got all zero except the last element which is 4. Is there any way to starting iterating from the last element and multiply the both last elements and store the results in the last but one element.

Ken Blair
Ranch Hand
Posts: 1078
Reverse the algorithm. Instead of starting at 0 start at the end and... I already posted enough code to easily do it. Once you understand how and why it works then you can reverse it. If you don't understand how and why it works then my posting more code isn't going to help.