Quantcast
Channel: Programming Forums
Viewing all articles
Browse latest Browse all 51036

Array Maximum and Minimum values

$
0
0
Hi there! so I have a problem. I'm supposed to write a code that makes a 4X6 array with random values in it then get the sum of all 24 numbers the average, the maximum value, and minimum values of this 2D array. I have gotten the SUM, AVERAGE and MAX but for some reason the minimum just isn't working & i swear its right. Thanks for help in advance this is what i have:

import java.util.*;
import java.math.*;
public class RandomNumberArray2 
{
  public static void main(String[] args) 
  {
    Scanner array = new Scanner(System.in);
    
      int rowWidth = 4; //declaring number of rows
      int colHeight = 6; //declaring number of columns                            
      Random rand = new Random(); //setting up for random number
      int [][] a = new int [rowWidth][colHeight]; //declaring an array    
      double max = 0; //declaring maximum value place
      double min = 0; //declaring minimum value place
        
        for (int row = 0; row < a.length; row++) 
        { //for loop to fill array with random values
          for (int col = 0; col < a[row].length; col++) 
          { //random values from between 1 & 100
            a[row][col] = rand.nextInt(100);  
          }
        }
        for(int i = 0; i < a.length; i++)  
        { //for loop to display array in interactions
          for(int j = 0; j < a[i].length; j++) 
          {
           if(a[i][j] > max)
           { //finding the max number in array
             max = a[i][j];
           }
           if(a[i][j] < min)
           { //finding the min number in array
             min = a[i][j];
           }
            
            System.out.print(a[i][j] + " "); //out printing array 
          }
        System.out.println();
        }
        
        
        int sum = 0;
        int w; //declaring sum of row1
        int x; //declaring sum of row2
        int y; //declaring sum of row3
        int z; //declaring sum of row4
        w = a[0][0] + a[0][1] + a[0][2] + a[0][3] + a[0][4] + a[0][5];  
        x = a[1][0] + a[1][1] + a[1][2] + a[1][3] + a[1][4] + a[1][5]; 
        y = a[2][0] + a[2][1] + a[2][2] + a[2][3] + a[2][4] + a[2][5];
        z = a[3][0] + a[3][1] + a[3][2] + a[3][3] + a[3][4] + a[3][5];
        sum = w + x + y + z; //adding all row sums to get total sum

        
        System.out.println("Sum of the 24 numbers = " + sum);
        System.out.println("Average of the 24 numbers = " +sum/24);
        System.out.println("Maximum of the 24 numbers = " + max);
        System.out.println("Minimum of the 24 numbers = " + min);
        
    
               
  }
}



*Edited: please :code:

Viewing all articles
Browse latest Browse all 51036

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>