A
Alf P. Steinbach
Well, nobody answered in [comp.programming] -- has that group died in the last
few months? -- so I'm reposting here in clc++.
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
few months? -- so I'm reposting here in clc++.
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