TiliDili
TiliDili

Reputation: 21

Java 2 dimensional arrays and average

Can someone please help me with this homework?

I have tried something but I'm not sure if my solution covers all the tasks. I have to write a Java program, which initializes a two-dimensional array with the marks from the 5th semester (there are only 5 marks, as you know) of n students (the user should input the number of students).

The program should outputs as a result:

  1. The average grade of all students for 5th semester;
  2. The number of the student with the highest average grade;
  3. The number of the student with the lowest average grade;
  4. The number of students with an average grade greater than the average grade of all students;
  5. The number of students with an average grade less than the average grade of all students;

The program should do data validation as follows: student’s marks should be between 2 and 6, and the number of students should not exceed 30.

and here is my solution so far :

package ocenki;


public static void main(String[] args) {

 Scanner scan = new Scanner (System.in ) ;
 System.out.println ("Enter notes here:") ;

 double [] marks= new double [5] ;

 for ( int i=0; i<=4; i++)   
 {
     System.out.println ("Please, input mark for " + i +(" subject")) ; 

     marks[i] = scan. nextDouble () ; 

     while (marks[i]<2 || marks[i]>6)
     {
         System.out.println ("Please, input marks between 2 and 6:") ;
         marks[i] = scan.nextDouble () ;
     }
 }

 double sum=0; 
 double min=marks[0];
 double max=marks[0];

 for ( int i=0; i<=4; i++)
 {
     sum = sum+marks[i] ; 
     if(marks[i]>max)
     {
         max=marks[i];
     }
     if(marks[i]<min)
     {
         min=marks[i];
     }       
 }
 System.out.println("The average is " + sum/5 + ", the minimum is " + min + " and the maximum is " + max); 
}

Upvotes: 2

Views: 3190

Answers (1)

Thomas
Thomas

Reputation: 508

Please find the solution for your Q-4 AND Q-5

Solution

    double avg= sum/5;
 for ( int i=0; i<=4; i++)
 {
     if(marks[i]>avg)
     {
         moreAvg++;
     }
     if(marks[i]<avg)
     {
         lessAvg++;
     }   
 }

FULL CODE -

public static void main(String[] args) {

 Scanner scan = new Scanner (System.in ) ;
 System.out.println ("Enter notes here:") ;

 double [] marks= new double [5] ;

 for ( int i=0; i<=4; i++)   
 {
     System.out.println ("Please, input mark for " + (i+1) +(" subject")) ; 

     marks[i] = scan. nextDouble () ; 

     while (marks[i]<2 || marks[i]>6)
     {
         System.out.println ("Please, input marks between 2 and 6:") ;
         marks[i] = scan.nextDouble () ;
     }
 }

 double sum=0; 
 double min=marks[0];
 double max=marks[0];
 int lessAvg=1,moreAvg=0;
 for ( int i=0; i<=4; i++)
 {
     sum = sum+marks[i] ; 
     if(marks[i]>max)
     {
         max=marks[i];
     }
     if(marks[i]<min)
     {
         min=marks[i];
     }       
 }

 double avg= sum/5;
 for ( int i=0; i<=4; i++)
 {
     if(marks[i]>avg)
     {
         moreAvg++;
     }
     if(marks[i]<avg)
     {
         lessAvg++;
     }   
 }

 System.out.println("The average is " +avg + ", the minimum is " + min + " and the maximum is " + max);
 System.out.println("4.The number of students with an average grade greater than the average grade of all students"+moreAvg);
 System.out.println("5.The number of students with an average grade less than the average grade of all students"+lessAvg);
}

Upvotes: 0

Related Questions