How to resolve the algorithm Loops/Nested step by step in the Scala programming language
Published on 12 May 2024 09:40 PM
How to resolve the algorithm Loops/Nested step by step in the Scala programming language
Table of Contents
Problem Statement
Show a nested loop which searches a two-dimensional array filled with random numbers uniformly distributed over
[ 1 , … , 20 ]
{\displaystyle [1,\ldots ,20]}
. The loops iterate rows and columns of the array printing the elements until the value
20
{\displaystyle 20}
is met. Specifically, this task also shows how to break out of nested loops.
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Loops/Nested step by step in the Scala programming language
Source code in the scala programming language
import scala.util.control.Breaks._
val a=Array.fill(5,4)(scala.util.Random.nextInt(21))
println(a map (_.mkString("[", ", ", "]")) mkString "\n")
breakable {
for(row <- a; x <- row){
println(x)
if (x==20) break
}
}
You may also check:How to resolve the algorithm Loops/Continue step by step in the NewLISP programming language
You may also check:How to resolve the algorithm Calendar - for REAL programmers step by step in the zkl programming language
You may also check:How to resolve the algorithm Roots of unity step by step in the Tcl programming language
You may also check:How to resolve the algorithm World Cup group stage step by step in the Lua programming language
You may also check:How to resolve the algorithm Enforced immutability step by step in the Factor programming language