1.create and populate data into the ragged array. the data must be integer numeric. do not use initialized array. the array must be dynamic.
2. compute the average data in the array
3.compute the total number of data entered into the array
4. print out the array in the ragged way it is.
5.zero out all the elements in the first and last rows of the array.
1
Expert's answer
2013-02-20T09:37:51-0500
import java.util.Random;
class Main { & private static double average(int[][] array) & { double result = 0; int count = 0;
for (int x = 0; x < array.length; x++) for (int y = 0; y < array[x].length; y++) { result += array[x][y]; count++; } return result / count; & } & & private static int total_elements(int[][] array) & { int count = 0; for (int x = 0; x < array.length; x++) count += array[x].length; return count; & } & & private static void zero_row(int[][] array, int rowNum) & { for (int y = 0; y < array[rowNum].length; y++) array[rowNum][y] = 0; & } & & private static void print_array(int[][] array) & { for (int x = 0; x < array.length; x++) { for (int y = 0; y < array[x].length; y++) & System.out.print(String.format("%5d", array[x][y])); System.out.println(); } & } & & public static void main(String[] args) & { int[][] array = new int[5][]; Random rand = new Random(System.currentTimeMillis());
for (int k = 0; k < 5; k++) { array[k] = new int[k + 2]; for (int c = 0; c < array[k].length; c++) & array[k][c] = rand.nextInt() % 40; }
System.out.println("The average value is: " + average(array)); System.out.println("Total number of elements in the array: " + total_elements(array)); System.out.println("The initial array:"); print_array(array);
System.out.println("First and last rows filled with zeros:"); zero_row(array, 0); zero_row(array, array.length - 1); print_array(array); & } }
Comments
Leave a comment