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
  • Create a C++ language program which feeds randomly generated number using the rand function into a...

    Create a C++ language program which feeds randomly generated number using the rand function into a multidimensional array. Create this array such that it has 5 columns and 5 rows of randomly generated values . Then display the contents of the array, and in addition display the sum of each row and the sum of each column in the array.

  • *Answer must be in C* Write a complete program as follows (declare any necessary variables): Create...

    *Answer must be in C* Write a complete program as follows (declare any necessary variables): Create an array of doubles named “hummus” with 2 rows and 300 columns. Initialize all array elements to zero. Write a loop that will repeatedly ask the user to enter a value and store it in the first row of the array (do not overwrite previously entered values) until the whole first row is populated with the user input. (hint the loop should have the...

  • Program in C++! Thank you in advance! Write a menu based program implementing the following functions: (1) Write a funct...

    Program in C++! Thank you in advance! Write a menu based program implementing the following functions: (1) Write a function that prompts the user for the name of a file to output as a text file that will hold a two dimensional array of the long double data type. Have the user specify the number of rows and the number of columns for the two dimensional array. Have the user enter the values for each row and column element in...

  • Programing in Scala: Create a class called “Array” that implements a fixed-sized two-dimensional array of floating-point...

    Programing in Scala: Create a class called “Array” that implements a fixed-sized two-dimensional array of floating-point numbers. Write separate methods to get an element (given parameters row and col), set an element (given parameters row, col, and value), and output the matrix to the console formatted properly in rows and columns. Next, provide an immutable method to perform array addition given two same-sized array.

  • Write a menu based program implementing the following functions: (0) Write a function called displayMenu that...

    Write a menu based program implementing the following functions: (0) Write a function called displayMenu that does not take any parameters, but returns an integer representing your user's menu choice. Your program's main function should only comprise of the following: a do/while loop with the displayMenu function call inside the loop body switch/case, or if/else if/ ... for handling the calls of the functions based on the menu choice selected in displayMenu. the do/while loop should always continue as long...

  • create a new Java application called "Scorer" (without the quotation marks) that declares a two-dimensional array...

    create a new Java application called "Scorer" (without the quotation marks) that declares a two-dimensional array of doubles (call it scores) with three rows and three columns and that uses methods and loops as follows. Use a method containing a nested while loop to get the nine (3 x 3) doubles from the user at the command line. Use a method containing a nested for loop to compute the average of the doubles in each row. Use a method to...

  • Java Programing Code only Ragged Array Assignment Outcome: Student will demonstrate the ability to create and...

    Java Programing Code only Ragged Array Assignment Outcome: Student will demonstrate the ability to create and use a 2D array Student will demonstrate the ability to create and use a jagged array Student will demonstrate the ability to design a menu system Student will demonstrate the ability to think Program Specifications: Write a program that does the following: Uses a menu system Creates an array with less than 25 rows and greater than 5 rows and an unknown number of...

  • Write a c++ program that does the following Create an integer array of size 30. Assign...

    Write a c++ program that does the following Create an integer array of size 30. Assign -1 to each location in the array indicating that the array is empty. Populate half of the array with random integer values between 100 and 500 inclusive. Use the following formula in order to hash and store each number in its proper position/location. Generated Number Table Size: Should a collision occurs, use linear probing to find next available position location. Use the following probing...

  • Write a C program that uses mallloc to create a 2D array. of c columns and...

    Write a C program that uses mallloc to create a 2D array. of c columns and r rows. Elements of array must be initialized to 0 and you must use malloc and free allocated memory fo array.

  • In JAVA Create a program with an array with the following data: 50 12 31 76...

    In JAVA Create a program with an array with the following data: 50 12 31 76 5 23 15 Use a text file (.txt created in notepad) to read the data into the array. Write a sort method (your choice of sorting algorithm) to sort the array values values - highest to lowest. Write a method to determine the minimum value. Your program should call the sort and minimum methods. Output the sorted array and minimum value.  

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