please help (seven judges)

A sketch that will obtain seven judges' scores where each score must be between 1 and 10 inclusively. Your program will calculate and output the average of the five middle scores (in other words, throw out the highest and the lowest score before finding the average). This is what I want to do and I can do everything but figure out how to exclude the max and min scores from my average calculation.

Answers

  • edited December 2017

    ... how to exclude the max and min scores from my average calculation.

    If you've got an array w/ those scores already sort() & reverse():

    1. https://processing.org/reference/sort_.html
    2. https://processing.org/reference/reverse_.html

    In order to exclude the highest score, start iterating over the array from index 1.
    And to exclude the lowest score, subtract 1 from the array's length for the loop's condition: O:-)

    /**
     * Middle Score Average (v1.0)
     * GoToLoop (2017/Dec/13)
     * https://Forum.Processing.org/two/discussion/25557/please-help#Item_1
     */
    
    static final int SCORES = 7, MIDDLE = SCORES - 2, MIN_SC = 1, MAX_SC = 10;
    byte[] scores = new byte[SCORES];
    
    void setup() {
      for (int i = 0; i < SCORES; scores[i++] = (byte) random(MIN_SC, 1 + MAX_SC));
      println(scores);
      println();
    
      scores = reverse(sort(scores));
      println(scores);
    
      int sum = 0;
      for (int i = 1; i < SCORES - 1; sum += scores[i++]);
      final float avr = sum/(float) MIDDLE;
    
      println("\nSum:", sum, " - Avr:", avr);
      exit();
    }
    
  • edited December 2017
        import javax.swing.*;
    
        Float FirstFloat;
        Float SecondFloat;
        Float ThirdFloat;
        Float FourthFloat;
        Float FifthFloat;
        Float SixthFloat;
        Float SeventhFloat;
    
        // Input first number
        String FirstStr = 
        JOptionPane.showInputDialog("Enter a number: ");
        FirstFloat = Float.parseFloat(FirstStr);
          if(FirstFloat < 1 || FirstFloat > 10)
            JOptionPane.showMessageDialog (null, "Error: Number input is invalid.");
    
        String SecondStr = 
        JOptionPane.showInputDialog("Enter a number: ");
        SecondFloat = Float.parseFloat(SecondStr);
          if(SecondFloat < 1 || SecondFloat > 10)
            JOptionPane.showMessageDialog (null, "Error: Number input is invalid.");
    
        String ThirdStr = 
        JOptionPane.showInputDialog("Enter a number: ");
        ThirdFloat = Float.parseFloat(ThirdStr);
          if(ThirdFloat < 1 || ThirdFloat > 10)
            JOptionPane.showMessageDialog (null, "Error: Number input is invalid.");
    
        String FourthStr = 
        JOptionPane.showInputDialog("Enter a number: ");
        FourthFloat = Float.parseFloat(FourthStr);
          if(FourthFloat < 1 || FourthFloat > 10)
            JOptionPane.showMessageDialog (null, "Error: Number input is invalid.");
    
        String FifthStr = 
        JOptionPane.showInputDialog("Enter a number: ");
        FifthFloat = Float.parseFloat(FifthStr);
          if(FifthFloat < 1 || FifthFloat > 10)
            JOptionPane.showMessageDialog (null, "Error: Number input is invalid.");
    
        String SixthStr = 
        JOptionPane.showInputDialog("Enter a number: ");
        SixthFloat = Float.parseFloat(SixthStr);
          if(SixthFloat < 1 || SixthFloat > 10)
            JOptionPane.showMessageDialog (null, "Error: Number input is invalid.");
    
        String SeventhStr = 
        JOptionPane.showInputDialog("Enter a number: ");
        SeventhFloat = Float.parseFloat(SeventhStr);
          if(SeventhFloat < 1 || SeventhFloat > 10)
            JOptionPane.showMessageDialog (null, "Error: Number input is invalid.");
    
    
    
        // Calculate the average of the five middle scores
        float avg = (FirstFloat + SecondFloat + ThirdFloat + FourthFloat + FifthFloat + SixthFloat +
        SeventhFloat) / 5.0;
    
        // Output average
        JOptionPane.showMessageDialog (null, "The average of the middle scores is " + avg );
    
        // Quit the sketch
        exit();
    
  • This is what I have so far, I'm not sure how you're supposed to make an array with this

  • edited December 2017
    // Import the library necessary to obtain user input and provide an error message
        import javax.swing.*;
    
        // Define the global variables
        String FirstString;
        Float FirstInteger;
        String OtherString;
        Integer OtherInteger;
        Float Average = 0.0;
    
    
              do {  // do
              FirstString = JOptionPane.showInputDialog ("Enter a number: "); // Prompt the user and obtain a value as a string
              FirstInteger = Float.parseFloat(FirstString); // Convert the string to an integer
                if ((FirstInteger < 7) || (FirstInteger > 7)) // IF the converted value is not valid
                  JOptionPane.showMessageDialog (null,"ERROR: Number is invalid."); // Provide an error message
              } while ((FirstInteger < 7) || (FirstInteger > 7)); // while the converted value is not valid
    
            for (int NewNum = 1; NewNum <= FirstInteger; NewNum++) {  
              do {  // do
              OtherString = JOptionPane.showInputDialog ("Enter a number " + NewNum +": "); // Prompt the user and obtain a value as a string
              OtherInteger = Integer.parseInt(OtherString); // Convert the string to an integer
                if ((OtherInteger < 1) || (OtherInteger > 10)) // IF the converted value is not valid
                  JOptionPane.showMessageDialog (null,"ERROR: Number is invalid."); // Provide an error message
              } while ((OtherInteger < 1) || (OtherInteger > 10)); // while the converted value is not valid 
              Average += OtherInteger;
    
          } // for
          Average /= FirstInteger;
          JOptionPane.showMessageDialog (null, "Average is " + Average);
    
        exit() ;
    
  • Or there is this version

  • Let's just throw out the second version right now. How can you find the max and min scores if you are only keeping a running total or average? If you aren't recording all the scores, finding the min and max scores will be a pain.

    Looking at the first version, does it seem repetitive to you at all? Does it seem to repeat the same thing a lot? Is it repeating itself? Does it say the same thing in the same way several times? Is it repetitive? Is it repetitive? Is it repetitive? Is it repetitive? Is it repetitive? Is it repetitive? Is it repetitive? Is it repetitive? Is it repetitive? Is it repetitive? Is it repetitive? Is it repetitive? Is it repetitive? Is it repetitive? Is it repetitive? Is it repetitive? Is it repetitive? Is it repetitive?

    This is a good indication that there is (Is it repetitive?) a better way to do things.

    First of all, you have 7 scores. These variables are where you are storing the scores:

    Float FirstFloat;
    Float SecondFloat;
    Float ThirdFloat;
    Float FourthFloat;
    Float FifthFloat;
    Float SixthFloat;
    Float SeventhFloat;
    

    Is this repetitive? Yes. Instead of having a variable that can store one score, and then having seven of those, we can just have one LIST of scores that is seven scores long. This list of variables is commonly called an ARRAY.

    float[] scores; // There is a list of scores.
    scores = new float[7]; // Make space on the list for 7 scores.
    

    Is this repetitive? Yes. We have scores in there twice. We can actually do this on one line:

    float[] scores = new float[7];
    

    Next, look at how you ask for the scores:

    String FirstStr = 
    JOptionPane.showInputDialog("Enter a number: ");
    FirstFloat = Float.parseFloat(FirstStr);
      if(FirstFloat < 1 || FirstFloat > 10)
        JOptionPane.showMessageDialog (null, "Error: Number input is invalid.");
    
    String SecondStr = 
    JOptionPane.showInputDialog("Enter a number: ");
    SecondFloat = Float.parseFloat(SecondStr);
      if(SecondFloat < 1 || SecondFloat > 10)
        JOptionPane.showMessageDialog (null, "Error: Number input is invalid.");
    
    String ThirdStr = 
    JOptionPane.showInputDialog("Enter a number: ");
    ThirdFloat = Float.parseFloat(ThirdStr);
    // ... AND SO ON...
    

    Is this repetitive? Very yes. You could use a LOOP here to do the same thing many times. Since you want exactly as many scores as there are space for in the list, we can use a for loop to ask for each score:

    for( int i = 0; i < scores.length; ){
      String resultStr = JOptionPane.showInputDialog("Enter a number: ");
      scores[i] = Float.parseFloat(resultStr);
      if(scores[i] < 1 || scores[i] > 10){
        JOptionPane.showMessageDialog (null, "Error: Number input is invalid.");
      } else {
        i++;
      }
    }
    

    Now that you have the scores in an array, you can sort them much easier, as GoToLoop has already shown you. Once they are sorted, it is a simple matter to sum up the middle five scores and then computer the average of them.

Sign In or Register to comment.