Create a program that uses a Jagged Array. The program will create an array with
ID: 3882175 • Letter: C
Question
Create a program that uses a Jagged Array. The program will create an array with 50 rows. Each of the values for each element of the array will be randomly generated. The random values will be between 1 and 20. Depending on the value generated for each row, you will create an array with that number of columns for that row. Each column created will contain the value to the left plus 1. After you create and populate the entire array, you will output what you created. Finally, you will also output the following values, the sum of all values in the array and the average of all values in the array. Remember everything possible needs to be written using methods. Please write the code as simple as possible as I'm a new learner.Explanation / Answer
import java.util.Random;
public class JaggedArray {
public static int[][] getArray() {
Random rand = new Random();
// array with 50 rows
int[][] arr= new int[50][];
// creating columns
for(int i=0; i<50; i++) {
int j = rand.nextInt(20)+1;
arr[i] = new int[j];
// filling rows
for(int k=1; k<j; k++)
arr[i][k] = arr[i][k-1]+1;
}
return arr;
}
public static void printArray(int[][] arr) {
for(int i=0; i<arr.length; i++) {
for(int j=0; j<arr[i].length; j++)
System.out.print(arr[i][j]+" ");
System.out.println();
}
}
public static int getSum(int[][] arr) {
int sum = 0;
for(int i=0; i<arr.length; i++) {
for(int j=0; j<arr[i].length; j++)
sum = sum + arr[i][j];
}
return sum;
}
public static double getAverage(int[][] arr) {
double sum = 0;
int count = 0;
for(int i=0; i<arr.length; i++) {
for(int j=0; j<arr[i].length; j++){
sum = sum + arr[i][j];
count++;
}
}
return sum/count;
}
public static void main(String[] args) {
int [][]arr = getArray();
printArray(arr);
System.out.println("Sum: "+getSum(arr));
System.out.println("Average: "+getSum(arr));
}
}
/*
Sample run:
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
0 1 2 3 4 5 6 7 8 9
0 1 2 3 4 5
0 1 2 3 4 5 6
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
0 1 2 3 4 5 6 7 8 9 10 11 12
0 1 2 3
0 1 2
0 1 2 3 4 5 6 7 8 9 10 11 12
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
0 1 2 3
0 1 2 3 4 5 6 7 8 9
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
0 1 2 3 4 5 6 7 8 9 10 11
0 1 2 3 4 5 6 7
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
0 1 2
0 1 2 3 4 5 6
0 1 2 3 4 5 6 7 8 9
0 1
0 1 2 3 4
0 1 2 3 4 5 6 7 8 9
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14
0
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
0 1 2 3 4 5 6 7 8 9 10 11 12 13
0 1 2 3 4 5 6 7 8 9
0 1 2 3 4 5 6 7
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
0 1
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14
0 1 2 3 4 5 6 7 8 9
0 1 2 3 4 5 6 7
0 1 2 3 4 5 6 7
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
0 1 2 3
0
0 1
0 1 2 3 4 5
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
0 1 2 3 4 5 6 7
0 1 2 3
0 1 2 3 4 5 6 7 8 9
0 1
0 1 2 3 4 5
0 1 2 3 4 5 6 7 8 9 10 11 12 13
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
0 1 2
Sum: 3018
Average: 3018
*/
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.