Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.programming > #260
| From | "Alf P. Steinbach /Usenet" <alf.p.steinbach+usenet@gmail.com> |
|---|---|
| Newsgroups | comp.programming |
| Subject | Mystery program... |
| Date | 2011-05-02 00:10 +0200 |
| Organization | A noiseless patient Spider |
| Message-ID | <ipkl8k$m5n$1@dont-email.me> (permalink) |
In my high-school "electronic data processing" text book from 1974 I found an
"explain what this program does" exercise that I don't quite grok now.
It's exercise 4.25 from page 72 of "EDB for videregående skoler" by Gram, Hope
and Ommundsen, NKS-forlaget 1974.
Here's the program translated from its original Basic to JavaScript (I'm
assuming the Windows Script Host for i/o, and if you have Windows simply copy
this text to a [.js] file and run it via e.g. 'cscript' command):
<code>
lineNum_ = 0
function input()
{
WScript.stdout.write( "? " );
var s = WScript.stdin.readLine();
return parseFloat( s );
}
function print( v )
{
WScript.stdout.writeLine( v );
}
function goto( new_n )
{
lineNum_ = new_n - 1
}
function end()
{
print( "FINISHED!" )
WScript.Quit()
}
function onStatementExecuted( lineNum )
{
//print( "lineNum="+lineNum + ", n="+n + ", m="+m + ", p="+p + ", p2="+p2 )
}
m = null
n = null
p = null
p2 = null
for( ;; )
{
lineNum_ += 1
if( lineNum_ > 99999 )
{
print( "!Program execution beyond last statement." );
WScript.Quit( -1 );
}
var executedLineNum = lineNum_
var didSmth = true
switch( lineNum_ )
{
case -1:
// BEGIN translation of original code:
break; case 30: m = 1
break; case 40: n = input()
break; case 50: p2 = n
break; case 60: p = p2 - 2*m
break; case 70: p2 = p
break; case 80: m = m + 1
break; case 90: if( p < m ) goto( 140 )
break; case 100: p = p - m
break; case 110: if( p > 0 ) goto( 100 )
break; case 120: if( p == 0 ) goto( 160 )
break; case 130: if( p2 > 2*m ) goto( 60 )
break; case 140: print( "n=" + n );
break; case 150: goto( 170 )
break; case 160: print( "m=" + m )
break; case 170: end()
// END translation of original code
break; default:
didSmth = false
}
if( didSmth )
{
onStatementExecuted( executedLineNum )
}
}
</code>
The exercise's part (a) recommends trying out the program "on paper" (noting
down values of variables) for n = 3, 10 and 28, and for part (b) it recommends
running the program and trying n = 2, 14, 22, 36 and 39.
OK, I can see that there's apparently a computation of remainder in there, but
it's not exactly meaningful to me.
I'm probably just blind on both eyes, but any help appreciated.
Cheers,
- Alf
--
blog at <url: http://alfps.wordpress.com>
Back to comp.programming | Previous | Next — Next in thread | Find similar | Unroll thread
Mystery program... "Alf P. Steinbach /Usenet" <alf.p.steinbach+usenet@gmail.com> - 2011-05-02 00:10 +0200
Re: Mystery program... Paul N <gw7rib@aol.com> - 2011-05-04 13:23 -0700
Re: Mystery program... "io_x" <a@b.c.invalid> - 2011-05-05 09:02 +0200
Re: Mystery program... "io_x" <a@b.c.invalid> - 2011-05-06 17:22 +0200
Re: Mystery program... "io_x" <a@b.c.invalid> - 2011-05-07 09:12 +0200
Re: Mystery program... "Alf P. Steinbach /Usenet" <alf.p.steinbach+usenet@gmail.com> - 2011-05-12 04:14 +0200
Re: Mystery program... tm <thomas.mertes@gmx.at> - 2011-05-11 23:42 -0700
Re: Mystery program... "Alf P. Steinbach /Usenet" <alf.p.steinbach+usenet@gmail.com> - 2011-05-12 14:17 +0200
csiph-web