//Daniel Douglas 
//CST112 Project 5

float[] da= new float[20] ;
int many=  da.length;
int x=10, y=40;
float wbig;
int nextX, nextY;        // Coords of next display.
final int FIRSTX=10, FIRSTY=40, SPACING=12;
float big=100,  multiplier;
int r,g,b;

void setup() {
  size(800,480);
  r=127;
  b=255;
  g=255;
  background(r,b,g);
  ellipseMode(CORNER);
  text(
  "Project 5 // Daniel Douglas",
  10, height-20 );

  fill(0);
  text("Press the 's' key to sort the array.", 10,10);

  for (int i=0;i<many;i++) {
    da[i]=random(0,100);
  }
  wbig=-1;
  //showArray(da,many);

  ////
  float big;

  // Find the biggest number.
  big=  da[0];
  for (int j=0; j<many; j++) 
  {
    if (da[j] > big) big=  da[j];
  }

  // Output the result.
  println("Biggest:  "+big);
  wbig=big;
  fill(0,0,0);
}

void draw() {
  fill(255,0,0);
  rect (x+600,50,50,25);
  fill (0,255,0);
  ellipse(x+600,150,50,25);
  fill(0);
  text(  "SORT", x+610, 70 );
  text(  "FILL", x+610, 170 );
  int k=  whereBig( da );
  big=  da[ k ];              // Gets  biggest value in a.


  multiplier=  (width-300) / big;

  //scene();
  show();
}
void show()      //// Show elements of array.
{
  nextX=  FIRSTX;
  nextY=  FIRSTY;
  for (int j=0; j<many; j++ )
  {
    display( da[j] );
  }
}
void display( float v )    //// display one value on the screen. 
// ++++ Note to self:  maybe later I'll figure out how to make a bar.
{
  fill(0);
  text( v, nextX, nextY );
  bar( v );
  nextY += 15;
}
void bar( float v )        //// Draw a bar.
{
  float x, y, w, h;
  x= nextX + 60;
  y=  nextY - SPACING;
  w=  v * multiplier;
  h=SPACING-2;
  // Draw the bar.
  fill(255,0,0);
  rectMode( CORNER );
  rect( x,y, w, h );
}


float biggest( float[] z )        // Retgurn biggest value in this array.
{
  float result; 
  result=  z[0]; 
  for ( int j=0; j < z.length; j++)
  {
    if (z[j] > big) result=  z[0]  ;    /// replace with bigger value.
  }
  return result;
}
int  whereBig( float[] z )        // Return INDEX OF the biggest value in this array.
{
  int w=0;  
  for ( int j=0; j < z.length; j++)
  {
    if (z[j] > z[w]) w=j;        // replace w with INDEX of bigger value!
  }
  return w;
}




void keyPressed() {
  if (key=='s') {
    r=255;
    g=127;
    b=255;
    text("SORTED ARRAY:", 10,10);
    y=  50;

    //// Sort the array.
    sortArray(da, many);
  }
  else if(key=='f') {
    for (int i=0;i<many;i++) {
      da[i]=random(0,100);
    }  
    r=127;
    b=255;
    g=255;
    background(r,g,b);
  }
  else if (key=='b') {
    wbig=da[whereBig(da,many)];
    text ("Biggest value in array: "+wbig,600,20); // click the B button too show biggest array.
  }
  else if (key=='m') {
    int k= whereBig( da, many );
    swap( da, k, many-1 );
    background(r,g,b);
    showArray (da,many);
  }
  else if (key=='n') { 
    int k= whereBig( da, many-1 );
    swap( da, k, many-2 );
    background(r,g,b);
    showArray (da,many);    
  }
  else if(key=='d') {
    for(int i=0;i<many;i++) {
      for(int i2=i+1;i2<many;i2++) {
        if(da[i]==da[i2]) {
          da[i]=da[i2]=0;
        }
      }
      showArray(da,many);
    }
  }
}
  void sortArray( float da[], int many) {
    //// Sort the array
    background(r,g,b);
    int n=many;
    while( n>1 ) {
      //// Find biggest and move it to the end.
      int k= whereBig( da, n );
      swap( da, k, n-1 );      // Move biggest to end.
      n--;                    // Now, shrink the array.
    }
    text("SORTED ARRAY:", 10,10);
  }
  int whereBig( float da[], int n ) {
    //// Return index of biggest.
    int w=  0;                    //// Start with first element.
    for (int j=1; j<n; j++) 
    {
      if (da[j] > da[w]) w=  j;
    }
    return w;
  }  
  void swap( float da[], int i, int j ) {
    //// Swap 2 elements of an array.
    float tmp;
    tmp=  da[i];
    da[i]=  da[j];
    da[j]=  tmp;
  }

  void showArray( float da[], int many) {
    //// Show the array

      // Display the array.
    for (int j=1; j<many; j++) {
      println(da[j]);
      y += 12;
    }
  }
  void mousePressed() {
    if (mouseX >x+600&&mouseX<x+650&&mouseY>50&&mouseY<75) {
      background(255,127,255);
      text("SORTED ARRAY:", 10,10);
      y=  50;
      r=255;
      g=127;
      b=255;
      //// Sort the array.
      sortArray(da, many);
    }
    else if (mouseX>x+600&&mouseX<x+750&&mouseY>150&&mouseY<175) {
      for (int i=0;i<many;i++) {
        da[i]=random(0,100);
      }

      r=127;
      b=255;
      g=255;
      background(r,g,b);
    }
  }
