My requirements are as follows:

  1. use main method
  2. use the following array-
    int[] grades = new int[] {
    82, 70, 99, 90, 92, 75, 87, 85, 91, 100, 91, 87
    };
  3. Write code to step through the array of grades and calculate the average grade. Modify your code so that while it is stepping through the grades, it also looks for the minimum grade. Modify your code so that while it is stepping through the grades, it also looks for the maximum grade.
    You should only have one loop and work on finding the average, min, and max on one sweep through the loop.

  4. Format your output so that it looks like this:

    min:  70
    max: 100
    

    average: 87.4

----------------------------------This is what I have------------------------------------

package com.abc.array;

public class ArrayAverage {
    public static void main(String[] args) {
        int sum = 0;
        int max = 0;
        int min = 0;

        // Creating array of grade values
        int[] grades = new int[] {
            82, 70, 99, 90, 92, 75, 87, 85, 91, 100, 91, 87 
        };

        for (int i = 0; i < grades.length; i++) {
            sum += grades[i];
            if (min > grades[i]) {
                min = grades[i];
            }
            if (max < grades[i]) {
                max = grades[i];
            }
        }

        double average = (double) sum / grades.length;

        System.out.printf("    min:  " + min);
        System.out.printf("%n    max:  " + max);
        System.out.printf("%naverage:  %.1f", average);

    }
}

Recommended Answers

All 12 Replies

OK, so what's the question? Calculation of the minimum perhaps? if (min > grades[i]) how often is that true when min is 0?

OK, so what's the question? Calculation of the minimum perhaps? if (min > grades[i]) how often is that true when min is 0?

Yes, the question is calculation of minimum. I'm just starting out in a Fundamentals class... I guess what I want to know is what do I have to set min equal to....70??? I wanna make sure I'm not hard-coding the value of 70 into the min variable though

OK. Here at Daniweb we always try to help you find the answer yourself rather than just giving it to you (that way you learn more and better).
Your problem is that you start out with min smaller than the smallest number in the array, so (min > grades) is always false. What do you have to set it to so that test is true, at least once?

OK. Here at Daniweb we always try to help you find the answer yourself rather than just giving it to you (that way you learn more and better).
Your problem is that you start out with min smaller than the smallest number in the array, so (min > grades) is always false. What do you have to set it to so that test is true, at least once?

Set min value to the lowest number in the set (70)?

Well, that works, but it means you have to know what the lowest number is - which is what you are trying to find!
Look at the code for max - that starts out lower than anything in the array (zero), so it gets increased up to the highest value in the array. Now look at the code for min - that has to start out ...?... so that it gets decreased down to the lowest value in the array.

Well, that works, but it means you have to know what the lowest number is - which is what you are trying to find!
Look at the code for max - that starts out lower than anything in the array (zero), so it gets increased up to the highest value in the array. Now look at the code for min - that has to start out ...?... so that it gets decreased down to the lowest value in the array.

So min starts out at 100 so it may be decreased to the lowest value in the array. Yep, lol that does make sense. I just plugged it into the code and it's working like it should.

** point of interest ** does the calculation for average score have to remain outside the loop, or may I place it inside the loop as well?

That's the idea! Now maybe the next array will have numbers between 1000 and 2000 in it, so ideally you should start with a much bigger number, say 1000000.
Best of all, there's a constant in Java called Integer.MAX_VALUE which is the largest value an int can possibly have, so that's the very best starting value for min.

Average is sum/count, and you don't have a final value for sum until the loop has finished looping, so it makes no sense to calculate it inside the loop.

That's the idea! Now maybe the next array will have numbers between 1000 and 2000 in it, so ideally you should start with a much bigger number, say 1000000.
Best of all, there's a constant in Java called Integer.MAX_VALUE which is the largest value an int can possibly have, so that's the very best starting value for min.

Average is sum/count, and you don't have a final value for sum until the loop has finished looping, so it makes no sense to calculate it inside the loop.

Excellent! Thanks for all your help James... I appreciate it.

Glad to help. Now if that answers your question please mark this thread "solved" (you'll see the link to click at the end of this thread) so everyone else knows not to try and solve it again.
ps: Next time please put your code in code tags (select it and click [code] just above the edit area) because that makes it much easier for us to read.
J

Glad to help. Now if that answers your question please mark this thread "solved" (you'll see the link to click at the end of this thread) so everyone else knows not to try and solve it again.
ps: Next time please put your code in code tags (select it and click [code] just above the edit area) because that makes it much easier for us to read.
J

Will do. Thanks again.

for minimum have to fist assign to the first element in array, not 0:

int min=grades[0];

Hello seneyseanbattambang, welcome to DaniWeb.
Did you notice this thread was marked "solved" two years ago? Your suggestion is perfectky valid, but a little too late!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.