Download as pdf or txt
Download as pdf or txt
You are on page 1of 5

/*My name is ERIANA LIM BINTI MUHAMMAD RAZWAN LIM from S2K3T4 and this is my assignment

for array.

* I need to write a program that reads number of students in a class, student’s name, and

student’s marks.

* Display the maximum marks along with their names and their

index location respectively and the total of students who gained the maximum

marks.

* Display the minimum marks along with their names and their

index location respectively and the total of students who gained the minimum

marks.

* Display the average marks.

*/

// Import statement

import java.util.Scanner;

// Class header

public class AssignmentArrayErianaLim234

// Main method

public static void main(String[]arg)

Scanner sc= new Scanner(System.in); //Input object

// Reads number of students in a class.

int n=0; //Declare variable

System.out.print("Enter number of students: " );

n=sc.nextInt(); //Read the unknown value


int i=0; //I

//Declare variables

String[]name=new String[n]; //Declare + Create Array

double [] marks = new double [n]; //Declare + Create Array

// Declare initial value

double maxmarks=-999999;

double minmarks=999999;

double sum=0.0;

double totalhighest=0.0;

double totallowest=0.0;

double average=0.0;

// 1st Looping for reads student's name and student's marks.

while(i<name.length) //C

System.out.print("\n");

System.out.print("Enter your name : ");

name[i]=sc.next();

System.out.print("Enter your marks : ");

marks[i]=sc.nextDouble();

sum=sum+marks[i];

i=i+1; //U

} // end repeat

// 2nd Looping for maximum marks and minimum marks.

i=0; //I

while(i<marks.length) //C
{

if(marks[i]>maxmarks)

maxmarks=marks[i];

//end if

i=i+1; //U

} //end repeat

i=0; //I

while(i<marks.length) //C

if(marks[i]<minmarks)

minmarks=marks[i];

//end if

i=i+1; //U

} //end repeat

// 3rd looping to find the names of the highest marks and frequency of students who got the
highest mark

System.out.print("\n");

System.out.println("The highest marks is= " + maxmarks);

i=0; // I

while(i<marks.length) //C

if(marks[i] == maxmarks)

System.out.println(name[i]+" at index location " + (i));

totalhighest=totalhighest + 1;

}
//end if

i=i+1; //U

} //end repeat

System.out.print("\n");

System.out.println( "The number of student(s) obtained the highest marks= "+maxmarks


+("\n"));

// 4th looping to find the names of the lowest marks and frequency of students who got the
lowest mark

System.out.println("The lowest marks is= " + minmarks);

i=0; // I

while(i<marks.length) //C

if(marks[i] == minmarks)

System.out.println(name[i]+" at index location " + (i));

totallowest=totallowest + 1;

//end if

i=i+1; //U

}//end repeat

System.out.print("\n");

System.out.println( "The number of student(s) obtained the lowest marks= "+ minmarks+ "\n");

average=sum/n; //Find average

System.out.print("The average marks are "+ average);

You might also like