Modify a text file: $/array7424b.pde
$/array7424b.pde
//// Array exercise: add up #s in an int [] array String title= "Array exercise: manipulate #s in an int [] array"; String author= "Prof.BAM:7424b"; String help= "Press 'r' key for new random values"; int[] a = { 99, 47, 62, 403, 18, -7, 22, 49 }; //-- int asize=10; int asize= a.length; void setup() { size(400,300 ); //-- a= new int[asize]; reset( a, asize ); } void draw() { background( 255,200,50 ); fill(0); text( title, width/3, 20 ); text( help, width/2, 40 ); text( author, 20, height-20 ); // Display a[] show( a, asize ); // Compute total. int total= sum( a, asize ); int biggest= big( a, asize ); // Output. text( "TOTAL: " + total, width/3, height-80 ); text( "BIGGEST: " + biggest, width/3, height-65 ); } void reset( int[] p, int m ) { for( int j=0; j
big) big= a[j]; } return big; } //// Return INDEX OF THE biggest of all elements //// int wherebig( int[] a, int m ) { int w= 0; for( int j=1; j
big) big= a[j]; if (a[j] > a[w]) w= j; } return w; } //// Add 1 to every element in array. void bump( int[] p, int m ) { for( int j=0; j
0) a[j]= a[j] + 1; } } //// EVENT HANDLERS //// void keyPressed() { if (key == 'r') reset( a, asize ); if (key == 'b') bump( a, asize ); if (key == 'e') even( a, asize ); if (key == 'z') { //// Move biggest to end (swap) int w= wherebig( a, asize ); int tmp; tmp= a[w]; a[w]= a[asize-1]; a[asize-1]= tmp; } }