String title="//// TABLE ////"; String author="//// K.CUSH ////"; String names[]= { "Kanye West", "Jay-Z", "Talib Kweli", "Mos Def", "Busta Rhymes", "Q-Tip", " J.Cole", "Kid Cudi", "Ab-Soul", "Electric Guest", "Chiddy Bang", "Donald"}; int ages[]= { 34, 42, 40, 40, 43, 41, 25, 25, 23, 22, 21, 30 }; int heights[]= { 68, 64, 73, 58, 71, 57, 57, 61, 57, 39, 46, 65 }; int weights[]= {188, 206, 165, 177, 192, 181, 99, 125, 206, 199, 85, 185 }; String columns[]= { "NAMES", "AGE", "WEIGHT", "HEIGHT", "BMI"}; int many= ages.length; float[] bmis= new float[many]; void setup() { size( 950, 600); int many= ages.length; } void draw() { //// Next frame /// background( 7,52, 147 ); scene(); table(); fill(0); textSize(17); text( "The last key you pressed was " + key, 20, 20 ); textSize(17); text( "Press key A to sort by age", width/2, height-30); text( "Press key W to sort by Weight", width/2, height-50); text( "Press key H to sort by Height", width/2, height-70); text( "Press key N to sort by Name", width/2, height-90); text( "Press key Bto sort by Body Mass Index", width/2, height-110); text( "Press the Q key to quit.", width/2, height-10); if (key=='N') { //// Find George int k= search( "Kanye West", names, many ); if (k<0) text( "Kanye not found)", 50, 400 ); else text( "k="+ k +" names[k]="+ names[k], 50, 400 ); } /* if (key == 'P') table(); else { action(); text( "Press P key to see the table", 10, height/2 ); } */ } int search( String s, String[] names, int m ) { //// return index of matching element int w; for (w=m-1; w>=0; w--) { //// return if match if (names[w].compareTo(s) == 0) return w; } return -1; } void keyPressed() { //// Respond to key events. if (key == 'A') { sortby( ages, many ); } //// Sort ALL arrays by age. if (key == 'H') { sortby( heights, many ); } // if (key == 'W') { sortby( weights, many ); } // if (key == 'B') { sortby( bmis, many ); } // if (key == 'N') { sortby( names, many ); } if ( key == 'q' ) { exit(); } } // //// SORT void sortby( int[] a, int many ) { ////// Rearranges the arrays for( int m=many; m>1; m-- ) { //// Widdles down the array to one part. int w= wherebig( a, m ); // The biggest. switchsall( w, m-1 ); // Move to end } } void sortby( float[] a, int many ) { for( int m=many; m>1; m-- ) { switchsall( wherebig(a,m), m-1 ); /// Move biggest to bottom } } void sortby( String[] a, int many ) { for( int m=many; m>1; m-- ) { switchsall( wherebig(a,m), m-1 ); // Move biggest to end } } void switchsall( int c, int k ) { //// Switches all colums of two given data switchs( names, c, k ); switchs( ages, c, k ); switchs( heights, c, k ); switchs( weights, c, k ); switchs( bmis, c, k ); } void switchs( int[] a, int c, int k) { int t=a[c]; a[c]=a[k]; a[k]=t; } void switchs( float[] a, int c, int k) { float t=a[c]; a[c]=a[k]; a[k]=t; } void switchs( String[] a, int c, int k) { String t=a[c]; a[c]=a[k]; a[k]=t; } //// Return index of biggest. int wherebig( int[] a, int m ) { //// Return index of biggest. int w=0; for (int c=1; c a[w] ) w=c; //// Index of biggest, so far } return w; } int wherebig( float[] a, int m ) { int w=0; for (int c=1; ca[w] ? c : w; } return w; } int wherebig( String[] a, int m ) { //// Return index of biggest. int w=0; for (int c=1; c 0 ) w=c; //// Index of biggest, so far } return w; } void table() { //// Display the array values in a table. int x1=10, x2=135, x3=190, x4=260, x5=330; // Columns of the table. int y=50, dy=12; // Row, row-spacing. //// Fill the bmi array. for (int c=0; c