//Array Exercise: Add up #'s in an int [] array String title = "Array Exercise: Add up #'s in an int [] array"; String author = "Tristan Szakacs"; int [] a; int aSize = 10; void setup() { size( 400, 300); a = new int[aSize]; keyPressed(); } void draw() { background(200); //Add background to redraw every frame to erase prev nums. //Display a[] show( a, aSize); //Compute total int total = sum(a, aSize); //Output text("TOTAL: " + total, width/3, height-20); text("To reset values press \"R\" or \"r\" ", 100, 100); // The \ allows the " to escape its meaning and be // Written as text. } void fill( int[] p, int m ) //Dummy Parameters. { for( int j = 0; j < m; ++j) { p[j] = int( random( 1, 101)); } } void show( int[] p, int m) { float y = 20, x = 20; for( int j = 0; j < m; ++j) { text( p[j], x, y); y += 15; } } int sum( int[] p, int m) //Walks through the array and adds the value for [j] to result then returns total result. { int result = 0; for( int j = 0; j < m; ++j) { result += p[j]; } return result; } void keyPressed() { if( key == 'r' || key == 'R') { fill( a, aSize); } }