Respuesta :
Answer:
import java.util.Scanner;
/**
*
* @author pc
*/
public class JaggedARrayAssignment {
private static int students[][] = new int[25][];//create a jagged array having 25 rows and unknown columns
public static void menu()
{
int index=0;//intitalize number of students to zero intially
Scanner sc=new Scanner(System.in);// create scanner for taking input from user
int choice;//choice
int maxExams=0;//maximum number of exams taken by any student
do
{
//print the menu
System.out.println("1-Input grades for next student");
System.out.println("2-Display the exam Average by student");
System.out.println("3-Display the exam Average by exam");
System.out.println("4-Display the current class average for all exams");
System.out.println("5-Exit");
choice=sc.nextInt();
//if choice is 1
if(choice==1)
{//ask how many xams
System.out.println("Please enter how many exams this student has taken?");
int noOfExams=sc.nextInt();//take input
students[index]=new int[noOfExams];//intialize array index by no of exams
System.out.println("Please enter the each of those exam scores one by one");//enter scores
if(maxExams<noOfExams)//if this noofexams is greater then update maxexams
maxExams=noOfExams;
//take scores from user
for(int i=0;i<noOfExams;i++)
{
students[index][i]=sc.nextInt();
}
index++;//increase index
}
else if(choice==2)
{
//calculate avregage by student and print it
System.out.println("*** Average by Student Id ***");
System.out.println("Student Id\tAverage Score");
for(int i=0;i<index;i++)
{
System.out.print((i+1)+"\t");//print student id
int sum=0;
//calculate sum
for(int j=0;j<students[i].length;j++)
{
sum=sum+students[i][j];
}
double avg=sum*1.0/students[i].length;//find average
System.out.printf("%.2f",avg);//print average by precison of two
System.out.println();
}
}
else if(choice==3)
{//caculate averag eby exams and print it
System.out.println("*** Average by Exam Number ***");
System.out.println("Exam Number\tAverage Score");
for(int i=0;i<maxExams;i++)
{
int sum=0,total=0;
for(int j=0;j<index;j++)
{
if(students[j].length>=i+1)
{
total++;
sum=sum+students[j][i];
}
}
double avg=sum*1.0/total;
System.out.print((i+1)+"\t");
System.out.printf("%.2f",avg);
System.out.println();
}
}
else if(choice ==4)
{//find iverall average
int sumtotal=0;
for(int i=0;i<index;i++)
{
int sum=0;
for(int j=0;j<students[i].length;j++)
{
sum=sum+students[i][j];
}
double avg=sum*1.0/students[i].length;
sumtotal+=avg;
}
double avgTotal=sumtotal*1.0/index;
System.out.println("Current class Average for all exams - "+avgTotal);
}
}while(choice!=5);
}
public static void main(String[] args) {
menu();
}
}