Comparing Ints

edited November 2013 in How To...

So I have this code, which takes 10 ints from an excel ark. I need to compare them to each other to see, which one is the biggest, I just can not figure out how to do it?

Tagged:

Answers

  • assuming they are in an array (and if not they should be) you basically need to go through them comparing the next element with the current maximum and replacing that if it's higher

    max = element[0]
    index = 0
    for (i = 1 to number of elements in array) {
      if element[i] > max {
        max = element[i], index = i  // remember new max and position of new max
      }
    }
    print (Max: max at position: index)
    
  • Im a little bit lost. Right now my code looks like this

    import ddf.minim.*;
    import de.bezier.data.*;
    
    Minim minim;
    
    final int teams = 10;
    
    AudioPlayer[] team = new AudioPlayer[teams];
    
    WaveformRenderer waveform;
    XlsReader reader;
    
    int[] team_player = new int[teams];
    
    int[] team_point = new int[teams];
    
    int[] team_isPlaying = new int[teams];;
    
    void setup()
    {
      size(512, 200, P3D);
    
      minim = new Minim(this);
      team[0] = minim.loadFile("team1.mp3", 2048);
      team[1] = minim.loadFile("team2.mp3", 2048);
      team[2] = minim.loadFile("team3.mp3", 2048);
      team[3] = minim.loadFile("team4.mp3", 2048);
      team[4] = minim.loadFile("team5.mp3", 2048);
      team[5] = minim.loadFile("team6.mp3", 2048);
      team[6] = minim.loadFile("team7.mp3", 2048);
      team[7] = minim.loadFile("team8.mp3", 2048);
      team[8] = minim.loadFile("team9.mp3", 2048);
      team[9] = minim.loadFile("team10.mp3", 2048);
    
      waveform = new WaveformRenderer();
      // see the example Recordable >> addListener for more about this
      for (int i = 0; i < teams; i++)
      {
        team[i].addListener(waveform);
      }
    }
    
    void draw()
    {
      background(0);
      // see waveform.pde for an explanation of how this works
      waveform.draw();
    }
    
    void keyPressed() {
      if ( key == 'p' ) {
        reader = new XlsReader( this, "workbook.xls" );
        for (int i = 0; i < teams; i++)
          {
            team_point[i] = reader.getInt(2, i);
          }
    }
    
    void stop()
    {
      // always close Minim audio classes when you are done with them
      for (int i = 0; i < teams; i++)
          {
            team[i].close();
          }
      // always stop Minim before exiting.
      minim.stop();
    
      super.stop();
    }
    

    I need to compare the score of the teams, and then call something like: winning_team.loop() //play the corresponding song

  • Answer ✓

    I am not sure if that's what you want, but here is an example:

    int[] team_player = { 5, 1, 7, 42, 9, 11, 13, 4, 12, 10 };
    int pos = -1;
    int max = -1;
    for (int i = 0; i < team_player.length; i++)
    {
      if (team_player[i] > max)
      {
        pos = i;
        max = team_player[i];    
      }
    }
    println("Biggest: " + max + " at position " + pos);
    exit();
    
Sign In or Register to comment.