From: Alain Reymond on
Nicolas,

Try to use the recursive posssibilities of modern Cobol. This is good
for teaching and showing that Cobol is no more an amount of go to's in
spaghetti code (troll)...

Here is an example (works under Micro Focus Cobol):

$set sourceformat"free"
identification division.
program-id. factorial.
data division.
working-storage section.
1 m pic 9(4).
local-storage section.
1 n pic 9(4).
linkage section.
1 aNumber pic 9(4).
1 fact pic 9(18). *> The longest you can
procedure division using aNumber fact.
move aNumber to n
if n>0 then
compute m = n - 1
call "factorial" using by content m by reference fact
compute fact = n * fact
else
compute fact = 1
end-if
exit program
.
end program factorial.

aNumber is passed to n in the local storage section.
all n's are stored in a pile until fact=1.
Then, it is taken from the pile and multiplied by each result of the
factorial function.

And then the program for testing factorial :

$set sourceformat"free"
identification division.
program-id. test-factorial.
data division.
working-storage section.
1 n pic 9(4).
1 fact pic 9(18).
procedure division.
display "Enter a positive number: " with no advancing
accept n
call "factorial" using by content n by reference fact
display "Factorial(" n ")= " fact
exit program
.
end-program.

Regards.

Alain


Nicolas Neuss a ?crit :
> Louis Krupp <lkrupp(a)pssw.nospam.com.invalid> writes:
>
>
>>I've never used TinyCobol, but it might be worth a try. I think an
>>iterative factorial implementation might be easier to follow than a
>>recursive routine. This would look something like:
>>
>>move 1 to factorial.
>>perform varying t from 2 to number
>> multiply factorial by t
>>end-perform.
>>
>>Louis
>
>
> Thanks, I'll try this, too.
>
> Nicolas.

From: Chuck Stevens on

"charles hottel" <jghottel(a)yahoo.com> wrote in message
news:a9c14$43c31f96$4f9c6e7$32726(a)DIALUPUSA.NET...
>
> "Nicolas Neuss" <firstname.lastname(a)iwr.uni-heidelberg.de> wrote in
> message news:87lkxpgnwc.fsf(a)ortler.iwr.uni-heidelberg.de...
>
> <snip>
>
>> I had found this one already, but had somehow hoped that a nicer version
>> would be possible, e.g. something like in
>>
>> <http://groups.google.com/group/comp.lang.cobol/msg/fae713b0615c1445>
>>
>> Thank you,
>>
>> Nicolas.
>
> I do not think that the program at this link is valid COBOL. A COBOL
> program can be recursive but COBOL paragraphs are not recursive at least
> not in the compilers that I am familiar with.

Hmmm. I don't have any problem with the following program using either of
the COBOL compilers supported for the Unisys MCP systems ('74 and '85
standards respectively):

IDENTIFICATION DIVISION.
ENVIRONMENT DIVISION.
DATA DIVISION.
WORKING-STORAGE SECTION.
77 FACT-RES PIC 9(8).
77 ARGUMENT PIC 9(8).
PROCEDURE DIVISION.
MAIN-LINE.
MOVE 1 TO FACT-RES. MOVE 5 TO ARGUMENT.
PERFORM COMPUTE-FACTORIAL.
DISPLAY FACT-RES.
STOP RUN.
COMPUTE-FACTORIAL.
IF ARGUMENT > 1
COMPUTE FACT-RES = FACT-RES * ARGUMENT
SUBTRACT 1 FROM ARGUMENT
PERFORM COMPUTE-FACTORIAL.

I've looked at the standards, and don't see anything that says PERFORMing a
paragraph recursively is illegal (though a STACK OVERFLOW fault in a COBOL
program on Unisys MCP systems is virtually diagnostic of re-executing a
PERFORM without ever completing the PERORM range it is in, so such matters
must be handled with care).

Can you cite some documentation somewhere supporting the assertion that one
can't PERFORM a paragraph from within the paragraph (whether or not that
paragraph is also PERFORMed externally)?

-Chuck Stevens


From: Howard Brazee on
On Tue, 10 Jan 2006 07:33:01 -0800, "Chuck Stevens"
<charles.stevens(a)unisys.com> wrote:

>Can you cite some documentation somewhere supporting the assertion that one
>can't PERFORM a paragraph from within the paragraph (whether or not that
>paragraph is also PERFORMed externally)?

I wouldn't be surprised if some compiler choked on this. But we can
certainly program recursive code anyway.
From: Nicolas Neuss on
Hello,

up to now I did not succeed in installing TinyCobol. But in OpenCobol,
your program does not seem to work, unfortunately:

I built the program using:

cobc -c factorial.cob
cobc -c -fmain factorial-test.cob
cobc -o factorial factorial.o factorial-test.o

obtaining warnings like:
In file included from /tmp/cobg6pZps.c:9:
/tmp/cobg6pZps.c.h:11: warning: pointer targets in initialization differ in signedness

Running the program I obtain:

> ./factorial
Enter a positive number: 0
Factorial(0000)= 000000000000000001
> ./factorial
Enter a positive number: 1
Factorial(0001)= 000000000000000000

I used the following programs (with indentation, because OpenCobol did not
understand it otherwise):


factorial.cob:

identification division.
program-id. factorial.
data division.
working-storage section.
1 m pic 9(4).
local-storage section.
1 n pic 9(4).
linkage section.
1 aNumber pic 9(4).
1 fact pic 9(18).
procedure division using aNumber fact.
move aNumber to n
if n>0 then
compute m = n - 1
call "factorial" using by content m by reference fact
compute fact = n * fact
else
compute fact = 1
end-if
exit program
.
end program factorial.


factorial-test.cob:

identification division.
program-id. test-factorial.
data division.
working-storage section.
1 n pic 9(4).
1 fact pic 9(18).
procedure division.
display "Enter a positive number: " with no advancing
accept n
call "factorial" using by content n by reference fact
display "Factorial(" n ")= " fact
exit program
.
end-program.

From: Nicolas Neuss on
"Richard" <riplin(a)Azonic.co.nz> writes:

>> but I have not succeeded in
>> finding a free implementation for COBOL.
>
> http://www.adtools.com/student/index.htm
>
> http://www.opencobol.org/
>
> http://tiny-cobol.sourceforge.net/
>
> http://www.thekompany.com/products/kobol/demo.php3?PHPSESSID=5ba92a9208b660be52e1baa9669df9af

Thank you for the links. Obviously, I did not search sufficiently well.

Nicolas.
First  |  Prev  |  Next  |  Last
Pages: 1 2 3 4 5 6 7 8 9 10
Prev: Data Representation in COBOL
Next: xml acucobol