• Post Reply Bookmark Topic Watch Topic
  • New Topic
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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

product all array elements without the first element

 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How do I find the product all array elements without the first element
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Using a "for" loop. Where are you stuck? Show us how far you've gotten and we can offer some advice.
 
Edward Amankwa
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here is my code and if you can show me how to find the product without the last 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 JLabel [] enter;
private JTextField [] input1;
private JTextField [] input2;
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]);
}

//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 totalSize = 1;
//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());
size[j] = (collectinput2[j]+1)- collectinput1[j];
}
theOutput = "Index\tLower\t Upper\t Size\n";
for (int f=0; f < dim; f++)
{
totalSize *= size[f];// I am stuck here
theOutput += f + "\t"+collectinput1[f] +"\t" + collectinput2[f]+"\t"+size[f]+"\n" ;
}
totalSize = totalSize * 2;
theOutput+="TotalSize\n" + totalSize;
output.setText(theOutput);
}
}
 
Ernest Friedman-Hill
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In your first post you said without the first element; in your second post, you said without the last element. If you want to avoid the first element, then just change the lower limit of the for loop -- i.e., the initial value of the counter. If you're skipping the last element, then change the upper limit -- i.e., the loop exit condition. You can compute the array index of the last element by subtracting one from the array length.
 
Edward Amankwa
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you very much Ernest for your quick reply.
 
Who knew that furniture could be so violent? Put this tiny ad out there to see what happens:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic