Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- public class Two_D_array_CMPS280_2016 {
- // Initialize the array
- double iDoc[][] = {
- {1,2,3,4},
- {5,6,7,8},
- {9,10,11,12},
- {13,14,15,16}
- };
- public static void main(String[] args) {
- Two_D_array_CMPS280_2016 array = new Two_D_array_CMPS280_2016();
- Scanner sc = new Scanner(System.in);
- int input = 0;
- System.out.println("Total of all elements: " + array.Total_2DArray_Elements());
- System.out.println("Average: " + array.Average_2DArray_Elements());
- System.out.println("Enter row: ");
- input = sc.nextInt();
- System.out.println("Total of elements on row: " + array.Total_2DArray_Elements_by_Certain_RowNo(input));
- System.out.println("Enter column: ");
- input = sc.nextInt();
- System.out.println("Total of elements in column: " + array.Total_2DArray_Elements_by_CertainColumnNo((input)));
- System.out.println("Enter row: ");
- input = sc.nextInt();
- System.out.println("Largest value in row: " + array.The_Largest_Value_In_Certain_RowNo(input));
- System.out.println("Enter column: ");
- input = sc.nextInt();
- System.out.println("Largest value in col: " + array.The_Largest_Value_In_Certain_ColumnNo((input)));
- }
- // Total all elements in the array
- double Total_2DArray_Elements() {
- double sum = 0.0;
- for (int i = 0; i < iDoc.length; ++i) {
- for (int j = 0; j < iDoc[i].length; ++j) {
- sum += iDoc[i][j];
- }
- }
- return sum;
- }
- // Return the average of all elements in the array
- double Average_2DArray_Elements() {
- int count = iDoc.length * iDoc[0].length; // assuming each row is of equal length
- double sum = Total_2DArray_Elements();
- return sum / count;
- }
- // Return the total of all of the elements in the given row
- double Total_2DArray_Elements_by_Certain_RowNo(int row) {
- double sum = 0.0;
- for (int i = 0; i < iDoc[row].length; ++i) {
- sum += iDoc[row][i];
- }
- return sum;
- }
- // Return the total of all of the elements in the given column
- double Total_2DArray_Elements_by_CertainColumnNo(int col) {
- double sum = 0.0;
- for (int i = 0; i < iDoc.length; ++i) {
- sum += iDoc[i][col];
- }
- return sum;
- }
- // Return the largest value in the given row
- double The_Largest_Value_In_Certain_RowNo(int row) {
- double max = iDoc[row][0];
- for (int i = 0; i < iDoc[row].length; ++i) {
- if (iDoc[row][i] > max) max = iDoc[row][i];
- }
- return max;
- }
- // Return the largest value in the given column
- double The_Largest_Value_In_Certain_ColumnNo(int col) {
- double max = iDoc[0][col];
- for (int i = 0; i < iDoc.length; ++i) {
- if (iDoc[i][col] > max) max = iDoc[i][col];
- }
- return max;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement