How to resolve the algorithm Loops/Nested step by step in the Chapel programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Loops/Nested step by step in the Chapel 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 Chapel programming language

Source code in the chapel programming language

use Random;

var nums:[1..10, 1..10] int;
var rnd = new RandomStream();

[ n in nums ] n = floor(rnd.getNext() * 21):int;
delete rnd;

// this shows a clumsy explicit way of iterating, to actually create nested loops: 
label outer for i in nums.domain.dim(1) {
        for j in nums.domain.dim(2) {
                write(" ", nums(i,j));
                if nums(i,j) == 20 then break outer;
        }
        writeln();
}


  

You may also check:How to resolve the algorithm Assertions step by step in the Slate programming language
You may also check:How to resolve the algorithm URL decoding step by step in the Objeck programming language
You may also check:How to resolve the algorithm Metronome step by step in the Delphi programming language
You may also check:How to resolve the algorithm Higher-order functions step by step in the Insitux programming language
You may also check:How to resolve the algorithm Infinity step by step in the Ada programming language