String title="//// Project 8";
String author="//// Phillip Eliacin";

String names[]=  { 
    "Phillip", "Jeffery", "Tyronne", "Jason", "Anthony", "Pierre", "Todd",
    "Zeus", "Anderson", "Kevin", "Marcus", "Aaron", "",
    "Chase", "Gary", "Ben", "Juan", "Wilson", "Harold",
    "Carson", "Larry", "Mason", "Darius", "Darnell", "Dixon", "Gerald", "Ronald"  };
int ages[]=  { 33, 76, 52, 24, 53, 19, 44, 65, 22, 23, 36, 61,  }; 
int heights[]= { 72, 59, 84, 31, 69, 72, 76, 61, 61, 29, 50, 60 };
int weights[]= {150, 209, 170, 185, 185, 175, 80, 140, 219, 200, 185, 160 };
int many=  ages.length;
float[] bmis= new float[many];

int[] rcolors= new int[many], gcolors= new int[many], bcolors= new int[many];


void setup() {
  size( 750, 500);
  int many=  ages.length;
  initcolors();
}
void draw() {
  //// Next frame ////
  background( 255,255, 145 );
  fill(0);
  scene();
  table();
 
  }
/*
  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 == 'Z') { sortby( ages, many ); }    //// Sort ALL arrays by age.
  if (key == 'W') { sortby( heights, many ); }    //
  if (key == 'T') { sortby( weights, many ); }    //
  if (key == 'Y') { sortby( bmis, many ); }    //
  if (key == 'I') { sortby( names, many ); }    //
  if (key == 'M') { initcolors(); }    //
}
//// SORT
void sortby( int[] a, int many ) {
  //// Rearrange ALL arrays, using a[] as "key"
  for( int m=many; m>1; m-- ) {
    //// Shrinking array (until only one element).
    int w=  wherebig( a, m );    // Find biggest.
    swapall( w, m-1 );           // Move to end
  }
}
void sortby( float[] a, int many ) {  // See above
  for( int m=many; m>1; m-- ) {
    swapall( wherebig(a,m), m-1 );  // Move biggest to end
  }
}
void sortby( String[] a, int many ) {  // See above
  for( int m=many; m>1; m-- ) {
    swapall( wherebig(a,m), m-1 );  // Move biggest to end
  }
}
void swapall( int j, int k ) {
  //// Swap all fields of two records 
  swap( names, j, k );
  swap( ages, j, k );
  swap( heights, j, k );
  swap( weights, j, k );
  swap( bmis, j, k );
}
void swap( int[] a, int j, int k) { int t=a[j]; a[j]=a[k]; a[k]=t; }
void swap( float[] a, int j, int k) { float  t=a[j]; a[j]=a[k]; a[k]=t; }
void swap( String[] a, int j, int k) { String t=a[j]; a[j]=a[k]; a[k]=t; }

//// Return index of biggest.
int wherebig( int[] a, int m ) {
  //// Return index of biggest.
  int w=0;
  for (int j=1; j<m; j++ ) { 
    if ( a[j] > a[w] ) w=j;      //// Index of biggest, so far    
  }
  return w;
}
int wherebig( float[] a, int m ) { 
  int w=0;  
  for (int j=1; j<m; j++) { w=  a[j]>a[w] ? j : w;  }
  return w;
}
int wherebig( String[] a, int m ) {
  //// Return index of biggest.
  int w=0;
  for (int j=1; j<m; j++ ) { 
    if ( a[j].compareTo(a[w]) > 0 ) w=j;      //// Index of biggest, so far    
  }
  return w;
}


void table() {
  //// Display the array values in a table.
  int x1=60, x2=120, x3=180, x4=240, x5=360;    // Columns of the table.
  int x6=  450;
  int y=65, dy=20;    // Row, row-spacing.
  //// Fill the bmi array.
  for (int j=0; j<many; j++) {
    bmis[j]=  bmicalc( heights[j], weights[j] );
  }
  // Column titles.
  fill(0);
  text( "NAME", x1, y );
  text( "AGE", x2, y );
  text( "HEIGHT", x3, y );
  text( "WEIGHT", x4, y );
  text( "BMI-index", x5, y );
  y += dy+5;
  fill(0,0,255);
  // Values from arrays. //
  for (int j=0; j<many; j++ ) {
    fill( rcolors[j], gcolors[j], bcolors[j] );
    text( names[j], x1, y );
    text( ages[j], x2, y );
    text( heights[j], x3, y );
    text( weights[j], x4, y );
    text( bmis[j], x5, y );
    // bars
    rect( x6, y-10, weights[j], 4 );
    fill( (rcolors[j]+50)%255, (gcolors[j]+50)%255, (bcolors[j]+50)%255 );
    rect( x6, y-8, 2*heights[j], 4 );
    fill(255,255,0);
    rect( x6, y-6, -10*bmis[j], 4 );
    y += dy;
  }
}


void bmitable() {
  //// Compute a BMI values.
  for (int j=0; j<many; j++) {
    bmis[j]=  bmicalc( heights[j], weights[j] );
  }
}float bmicalc( int h, int mass ) {
  //// Return bmi index
  return 703.0 * h / (mass*mass);
}

// STUBS //
void action() { text( "ACTION", 150, 150 ); }
void scene() { 
  text( title, 20, height-60  ); 
  text( author, 20, height-30 ); 
  //
  text( "To sort the table, pres a key:  A, H, W, B, or N", width/4, 30 );
}

void initcolors() {
  //// Initialize color arrays.
 for (int i=0; i<many; i++ ) {
   rcolors[i]=  int( random(250) );
   gcolors[i]=  int( random(250) );
   bcolors[i]=  int( random(250) );
  }
}




