/**          Simple programe to read a 5 element vector using the
 *           Display classes and print out the scalar product.
 *
 */
import uk.ac.ed.ph.sciprog.*;

public class ReadVector {
    public static void main(String args[]) {

	//                   Create the input panel.

	Display panel = new Display("Reading a Vector");

	double vectorDefaults[] = new double[5];       // Default value (zero)
	Input firstInput = new Input("First",vectorDefaults);
	Input secondInput = new Input("Second",vectorDefaults);
	panel.addInput(firstInput);
	panel.addInput(secondInput);


	//                   Infinite loop reading/writing.

	while(true) {
	    panel.waitForButtonPress();

	    //                Get the vector array back from the input
	    //                object

	    double first[] = firstInput.getDoubleArray();
	    double second[] = secondInput.getDoubleArray();

	    //                 Print out the values and sum at the same time
	    panel.clearText();
	    double scalarProduct = 0.0;                   // set to zero
	    panel.println("Vector values are:");
	    for(int i = 0; i < first.length; i++) {
		panel.printf("Element %d first is  %.6g second is %.6g\n",
			     i, first[i],second[i]);
		scalarProduct += first[i]*second[i];
	    }

	    //                  Print out the sum of the elements

	    panel.printf("Scalar product is  %.6g\n",scalarProduct);
	}
    }
}
