/**  SquareAdd:      Simple program to demonstrate 1-d double arrays.
 *                   Array is filled with squares, then summed
 *
 */
 
import uk.ac.ed.ph.sciprog.*;                 // Import Display classes
 
 
public class SquareAdd{                       // Define main class
    public static void main(String args[]){     // main program start
	
        //                 Setup the Display
 
        Display panel = new Display("Squares Loops");
        Input maxInput = new Input("Maximum square", 10);
        panel.addInput(maxInput);           // Add to display

	//                 Loop ``for ever ''

	while(true) {
	    panel.waitForButtonPress();

	    int maxValue = maxInput.getInt();    // Read the integer

	    //      Create array to hold the squares
	    //      Note array starts a 0, to to hold
	    //      0 -> maxValue you need maxValue + 1 elements

	    double squaresArray[] = new double[maxValue + 1];

	    //      Loop to fill the array with squares

	    for(int i = 0; i < squaresArray.length; i++) {
		squaresArray[i] = i*i;
	    }


	    //       Sum up the elements and their squareds with another loop

	    double sum = 0.0;                 // Start sum of at zero
	    double sumSqr = 0.0;
	    for(int i = 0; i < squaresArray.length; i++ ) {
		sum += squaresArray[i];              // Add each elementturn 
		sumSqr += squaresArray[i]*squaresArray[i]; // And it square
	    }


	    //        Print out the sum and the average

	    panel.printf("Sum is : %.0g\n", sum);
	    panel.printf("Average is : %.4g\n", sum/maxValue);
	    panel.printf("Sum of squares is : %.0g\n",sumSqr);
	}
    }
}
