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 Im a new learner.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

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

*/

Add a comment
Know the answer?
Add Answer to:
Create a program that uses a Jagged Array. The program will create an array with 50...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT