0

I'm trying to create code that calculates the percentage of values between a min and max value (inclusive). The output for this code should be 50.0, but I'm getting 100.0. Any ideas on why this is happening? Thanks! Edit: The code compiles, and I'm not having issues with division. I'm confused as to why the numbers are different. Thanks!

public class Test {
  public static double tempCheck(double[][] temps, double minTemp, double maxTemp) {
    int i;
    int betweenTemps = 0;
    double percentage = 0.0;
    int j;
    int numVals = 0;
    for (i = 0; i < temps.length; i++) {
      for (j = 0; j < temps[i].length; j++) {
        if (temps[i][j] >= minTemp && temps[i][j] <= maxTemp) {
          betweenTemps++;
        }
      }
      numVals++;
    }
    percentage = (betweenTemps / numVals) * 100;
    return percentage;
  }
  public static void main(String[] args) {
    double[][] temps = {
      { 72.0, 78.0, 74.5 },
      { 79.0, 80.0, 71.0 }
    };
    double minTemp = 70.0;
    double maxTemp = 75.0;
    System.out.println(tempCheck(temps, minTemp, maxTemp));
  }
}
  • 2
    It looks like `numVals++;` must be moved up one line. Also, integer division will give an integer as result, and `x/y` with `0 <= x < y` will be `0`. A remark: In java, you can initailize loop variables in the loop header: `for (int i = 0, ...)` – Turing85 Aug 07 '20 at 16:30
  • 1
    If you'd try to debug your code, you'd see why – B001ᛦ Aug 07 '20 at 16:31
  • Please read: [How to debug small programs](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/) – Turing85 Aug 07 '20 at 16:31
  • Well, it would also help to format your code properly. – MC Emperor Aug 07 '20 at 16:34
  • People, please link the duplicate in the comments if there is one. In my opinion these are not duplicates of this exact and clear question. –  Aug 07 '20 at 16:58

1 Answers1

1

https://ideone.com/B8rDJA

public class Test {
  public static double tempCheck(double[][] temps, double minTemp, double maxTemp) {
    int i;
    int betweenTemps = 0;
    double percentage = 0.0;
    int j;
    int numVals = 0;
    for (i = 0; i < temps.length; i++) {
      for (j = 0; j < temps[i].length; j++) {
        if (temps[i][j] >= minTemp && temps[i][j] <= maxTemp) {
          betweenTemps++;
        }
        numVals++;
      }
    }
    percentage = ((double) betweenTemps / (double) numVals) * 100.0;
    return percentage;
  }
  public static void main(String[] args) {
    double[][] temps = {
      { 72.0, 78.0, 74.5 },
      { 79.0, 80.0, 71.0 }
    };
    double minTemp = 70.0;
    double maxTemp = 75.0;
    System.out.println(tempCheck(temps, minTemp, maxTemp));
  }
}

You need to increment numVals within the nested loop, and cast your result to a double for an accurate result.

Ceilingfish
  • 5,397
  • 4
  • 44
  • 71
  • I think the indentation is a bit off. –  Aug 07 '20 at 16:35
  • Oh wow, I just noticed this is Java, not C#, that takes me back a few years. – Ceilingfish Aug 07 '20 at 16:37
  • Hey! This is super helpful, but the result is still 100.0 when I run the program! I'm not sure what math error is happening but I'm super confused as to why the output is different than expected. – Jenna Crist Aug 07 '20 at 16:42
  • 1
    Hey @JennaCrist I made a typo, and have revised it. If you take a look at the code, I've moved the `numVals++` outside of the `if` block, so it's _always_ incremented for each value, and `betweenTemps` is only incremented for the right values. If you're using an IDE for development, you can do some interactive debugging and step through, see the comments on your question for more info. – Ceilingfish Aug 07 '20 at 16:45
  • 1
    I have added an ideone link so you can see the result of the code (https://ideone.com/B8rDJA). –  Aug 07 '20 at 16:51