×

Welcome to TagMyCode

Please login or create account to add a snippet.
0
0
 
0
Language: Java
Posted by: Vusi Mbonani
Added: Aug 17, 2021 4:20 PM
Views: 3618
Tags: no tags
  1. package dimensionalarrays;
  2.  
  3. import java.util.Arrays;
  4.  
  5. /**
  6.  *This Snippet demonstrates how to determine the maximum and the minimum values
  7.  * of each row.
  8.  * @author VD-MBONANI
  9.  */
  10. public class DimensionalArrays {
  11.  
  12.     /**
  13.      * @param args the command line arguments
  14.      */
  15.     public static void main(String[] args) {
  16.      
  17.         //Create an array of arrays of int type named values.
  18.         int values[][] = {{10,20,30,30,40,50},
  19.                           {60,70,80,90,100},
  20.                           {110,120,130,140,150}};
  21.        
  22.         //Storing the returned minimum values to an array named minVal of type int.
  23.         int[] minVal = minimumValuesRows(values);
  24.         //Storing the returned maximum values to an array named maxVal of type int.
  25.         int[] maxVal = maximumValuesRows(values);
  26.        
  27.         //Displaying the minimum values of the of the rows.
  28.         System.out.println("Minimum Values of the Rows: "+Arrays.toString(minVal));
  29.        
  30.         //Displaying the maximum values of the of the rows.
  31.         System.out.println("Maximum Values of the Rows: "+Arrays.toString(maxVal));  
  32.     }
  33.     //Creating a method with a nested for loop to determine the min value in the rows.
  34.     public static int [] minimumValuesRows(int[][] val){
  35.         //Create an array to store the min values.
  36.         int [] result = new int[val.length];
  37.  
  38.         for(int i = 0; i < val.length; i++){
  39.             //initial min with MAX int value
  40.             int min = Integer.MAX_VALUE;  
  41.             for(int j = 0; j < val[i].length; j++){
  42.                 // if element smaller than min  
  43.                 if(min > val[i][j])  
  44.                 {
  45.                     //assign a new min value
  46.                     min = val[i][j];    
  47.                 }
  48.             }
  49.             //save the min for the row
  50.             result[i] = min;        
  51.         }
  52.         return result;
  53.     }
  54.     //Creating a method with a nested for loop to determine the max value in the rows.
  55.     public static int [] maximumValuesRows(int[][] val){
  56.         //Create an array to store the max values.
  57.         int [] result = new int[val.length];
  58.  
  59.         for(int i = 0; i < val.length; i++){
  60.             //initial max with MIN int value
  61.             int max = Integer.MIN_VALUE;  
  62.             for(int j = 0; j < val[i].length; j++){
  63.                 // if element bigger than max  
  64.                 if(max < val[i][j])  
  65.                 {
  66.                     //assign a new max value
  67.                     max = val[i][j];    
  68.                 }
  69.             }
  70.             //save the max for the row
  71.             result[i] = max;        
  72.         }
  73.         return result;
  74.     }
  75. }
  76.