I am quite partial to the while with stop variable solution. This can be coded easily in almost any language in a goto-less way, and C compilers these days should be able to optimize the stop variables away so it's as efficient as the solution with goto. It is also easy to derive by hand from the version using last.

Here's a perl implementation, but written in such a style that it's easy to translate to other languages. I show two versions. Either version of the function returns the index of the first all-zero row, or -1 if none of the rows are non-zero.

use warnings; use 5.014; use strict; sub first_allzero_row_a { my($a) = @_; my($r, $q, $c, $f, $o); $o = -1; $r = 0; $q = 0; while (!$q && $r < @$a) { $c = 0; $f = 0; while (!$f && $c < @{$$a[$r]}) { if (0 != ${$$a[$r]}[$c]) { $f = 1; } else { $c++; } } if (!$f) { $q = 1; $o = $r; } else { $r++; } } $o; } sub first_allzero_row_b { my($a) = @_; my($r, $q, $o); $o = -1; $r = 0; $q = 0; while (!$q && $r < @$a) { if (is_allzero($$a[$r])) { $q = 1; $o = $r; } else { $r++; } } $o; } sub is_allzero { my($a) = @_; my($c, $f); $c = 0; $f = 0; while (!$f && $c < @$a) { if (0 != $$a[$c]) { $f = 1; } else { $c++; } } !$f; }
my @array = ( [ qw/ 1 1 1 1 1 / ], [ qw/ 0 1 1 0 0 / ], [ qw/ 0 0 0 0 1 / ], [ qw/ 0 0 0 0 0 / ], [ qw/ 0 1 0 1 0 / ], ); say first_allzero_row_a(\@array); say first_allzero_row_b(\@array);

A more difficult example for using this style can be found in the notebook info1-gy4 (fallback if previous link is broken), near the middle. This I wrote for an introduction to programming course (though here we're interested in the extra material we haven't really covered on the course). The programs are in baby-Python.

The problem solved is the following. (I encourage you all to try to write your own solutions for this one.) We would like to demonstrate a theorem of Gauss that says any natural number can be written as a sum of three triangular numbers. The triangular numbers are the numbers of form n*(n+1)/2 where n is an integer. For this, we want to print one such decomposition to three triangular numbers for each number up to 80. You must not print more than one solution per number, because that would degenerate to a wall of text, and it would also be hard to see whether any number is skipped.

The two relevant solutions, translated to perl for your convenience, can be found in Write any natural number as sum of three triangular numbers (second and third solutions). (The output of the two solutions differ, but they're equally valid.)

Update: moved triangular numbers example code from this reply to separate meditation.


In reply to Re: An exploration of higher-level-language approaches to the "First all-zero row in an NxN matrix" problem (goto Considered Harmful) by ambrus
in thread An exploration of higher-level-language approaches to the "First all-zero row in an NxN matrix" problem (goto Considered Harmful) by davido

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.