-2

my lecturer give me these question:
1. Write a program that does the following:
a. Get the number of students from user (n)
b. Ask user to enter n grades of n students, store them in an array.
c. Print out the max, the min, and the average of those n grades.
Note: write 3 methods to return the max/min/average element of an array and use them in this program.

I try to do it, but the output of my program doesn't like what I'd expected. Here is my code:

package javaapplication2;
import java.util.*;

public class JavaApplication2 {
    public static double max(double[]x) {
        int i = 0;
        int max=0;
        for (i=0; i < x.length; i++) {
          if (max < x[i]) {
            max = i;
          }         
        }
        return max;    
    }
    public static double min(double[]y) {
        double min = max(y);
        for (int i =0; i < y.length; i++) {
            if (y[i] < min) {
                min = y[i];
            }
        }return min;
    }

    public static void main(String[] args) {
        String name ="";
        String choice;
        int times =0;
        double score;  

        Scanner input = new Scanner(System.in); 
        System.out.println("Enter student's name: ");
        name = input.nextLine();
        while (name != "exit") {
            double grades [] = new double [5000];
            System.out.println("Enter student's score: ");
            score = Double.parseDouble(input.nextLine());            
            grades[times] = score;            
            times += 1;            
            System.out.println("The max grade is: " + max(grades));
            System.out.println("The min grades is: " + min(grades));
            System.out.println("Enter student's name: ");
            name = input.nextLine();       

        }

    }
}

And here is my output:

Enter student's name: 
k
Enter student's score:30

The max grade is: 0.0
The min grades is: 0.0
Enter student's name:

Yah, I dont know why my max grade and min grade is 0.0. Anyone, please help me, thank you !!!

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
  • 2
    Your program doesn't seem to try to do part a (read the number of students). – Arndt Jonasson Aug 08 '18 at 11:53
  • You're also assigning the double array holding the students' grades *inside* your loop. – Paul Benn Aug 08 '18 at 11:54
  • 2
    Your code is very confusing and does things which you ware not asked to. For instance you ware not supposed to ask for student name, but you do... and instead of treating it as name you try to compare it with `exit` to decide if you want to exit the loop (which (a) shouldn't be there in first place, (b) your comparing strings incorrectly, see [How do I compare strings in Java?](https://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java)) – Pshemo Aug 08 '18 at 11:56

2 Answers2

0

Your problem comes from the grade array s being reassigned each loop

  public static void main(String[] args) {
                String name ="";
                String choice;
                int times =0;
                double score;  

                Scanner input = new Scanner(System.in); 
                System.out.println("Enter student's name: ");
                name = input.nextLine();
                while (name != "exit") {
                    //you set the grades array each loop to a new empty array
                    double grades [] = new double [5000];       //<--- Move this one out

                    System.out.println("Enter student's score: ");
                    score = Double.parseDouble(input.nextLine());            
                    grades[times] = score;            
                    times += 1;            
                    System.out.println("The max grade is: " + max(grades));
                    System.out.println("The min grades is: " + min(grades));
                    System.out.println("Enter student's name: ");
                    name = input.nextLine();       

                }

            }

Move it out and then try to get the methods done :)

Edit:

You also have a little error in the max method in regard of the value.

public static double max(double[]x) {
        int i = 0;
        int max=0;
        for (i=0; i < x.length; i++) {
          if (max < x[i]) {
            max = i;  //<-- Not max = i but max = x[i]  :)
          }         
        }
        return max;    
    }
0

In the function where you are calculating max, you should use:

if (max < x[i]) {
    max = x[i];
}    

As you want to return the element and not it's index. Also you would want to declare your array named grades before the while loop or else it would create a new array on every iteration.

And for improving the code performance:

1. you can in your max/min functions, exit the loop as soon as you encounter a value=0. In your current code the loop iterates 5000 times even if there is a single entry.

2. in your min function instead of doing double min = max(y); you should use double min = Double.MAX_VALUE;. It will prevent the unnecessary calling of the max function.

Swapnil Pandey
  • 577
  • 3
  • 8
  • 25