-
Notifications
You must be signed in to change notification settings - Fork 0
/
JaggedExample.java
47 lines (44 loc) · 1.52 KB
/
JaggedExample.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
import java.util.*;
public class JaggedExample{
public static void main(String args[]){
Scanner sc = new Scanner(System.in);
System.out.println("Number of players: ");
int n = sc.nextInt();
//create a jagged array
int a[][] = new int[n][];
float sum[] = new float[n];
float avg[] = new float[n];
//accept the data from the user
for(int i=0; i<n; i++){
System.out.print("Number of matches played by player"+ (i+1)+" ");
int m=sc.nextInt();
a[i]=new int[m];
}
for(int i=0;i<n;i++){
System.out.println("Enter the runs scored by player "+(i+1)+" in");
for(int j=0;j<a[i].length;j++){
System.out.print("Match "+(j+1)+":");
a[i][j]=sc.nextInt();
}
}
sc.close();
//retrieve the operated data as an output
System.out.println("Sum of the runs scored");
for(int i=0;i<n;i++){
for(int j=0;j<a[i].length;j++){
sum[i]=sum[i]+a[i][j];
}
}
for(int i=0;i<n;i++){
avg[i]=sum[i]/a.length;
}
for(int i=0;i<n;i++){
System.out.println("\nPlayer"+(i+1));
for(int j=0;j<a[i].length;j++){
System.out.println("Match"+(j+1)+" "+a[i][j]+"\t");
}
System.out.println("Total Runs are "+sum[i]+"\t");
System.out.println("Average is "+avg[i]);
}
}
}