Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,988 questions

51,933 answers

573 users

How to find the maximum value in a multidimensional array with Scala

1 Answer

0 votes
object MaxInMultiDimArray {
  def findMax(array: Array[Array[Double]]): Double = {
    // Initialize maxValue to the smallest possible value
    var maxValue: Double = Double.MinValue

    // Iterate through each sub-array and element
    for (subArray <- array) {
      for (value <- subArray) {
        if (value > maxValue) {
          maxValue = value // Update maxValue if a larger value is found
        }
      }
    }

    maxValue
  }

  def main(args: Array[String]): Unit = {
    // Define a multidimensional array
    val array = Array(
      Array(1.0, 2.0,  3.14),
      Array(1.0, 1.0, 16.80),
      Array(3.0, 5.0, 17.50),
      Array(2.0, 4.0, 11.03)
    )

    val maxValue = findMax(array)

    println(s"The maximum value in the array is: $maxValue")
  }
}

  
     
/*
run:
  
The maximum value in the array is: 17.5

*/

 



answered Apr 6, 2025 by avibootz
...