How to handle number series logics in cobol pgm



How to handle number series logics in cobol pgm

Postby joseseb » Thu Sep 09, 2010 11:38 pm

Suppose I have a number series like 1^1,2^2,3^3,4^4...i.e,1,4,27,256..
and pattern like
1
101
1001
10001
is there any functions to solve powers?
This was asked for me in IBm technical interview
joseseb
 
Posts: 7
Joined: Thu Sep 09, 2010 9:23 pm
Has thanked: 0 time
Been thanked: 0 time

Re: How to handle number series logics in cobol pgm

 

Re: How to handle number series logics in cobol pgm

Postby dick scherrer » Thu Sep 09, 2010 11:56 pm

Hello and welcome to the forum,

You need to write the code and post here when you get stuck.

People here will help you work thru the code but will not write it for you.

We are here to help people learn, not do the work for them.
Hope this helps,
d.sch.
User avatar
dick scherrer
Global moderator
 
Posts: 6304
Joined: Sat Jun 09, 2007 8:58 am
Has thanked: 3 times
Been thanked: 91 times

Re: How to handle number series logics in cobol pgm

Postby Robert Sample » Fri Sep 10, 2010 12:03 am

What did your search of the Language Reference manual tell you? You did search the manual before posting, right?
Robert Sample
Global moderator
 
Posts: 3363
Joined: Sat Dec 19, 2009 8:32 pm
Location: East Dubuque, Illinois
Has thanked: 1 time
Been thanked: 221 times

Re: How to handle number series logics in cobol pgm

Postby joseseb » Fri Sep 10, 2010 2:00 am

Yeah robert i found tat the logics works out with perform n compute statement..
Plzz check my logic

1, number series 1,4,27,
data division
01 i pic 9(5)
procedure division
move 0 to i.
compute i=i+1
perform until i times
compute i=i*i
end-perform
display i
2,number pattern
data division
01 i pic 9(3).
01 j pic 9(3).
procedure division
move 1 to i,j.
perform para-1 varying i from 1 by 1 until i<4 after varying j from 1 by 1 until j<=i
para-1
if j=1 and i=j then
display 1
else
display 0

plzz correct me if the logics are wrong thanx..
joseseb
 
Posts: 7
Joined: Thu Sep 09, 2010 9:23 pm
Has thanked: 0 time
Been thanked: 0 time

Re: How to handle number series logics in cobol pgm

Postby Robert Sample » Fri Sep 10, 2010 2:36 am

Your solution number 1 will fail miserably. You cannot use I as the index for your PERFORM loop AND your result -- it can be one or the other, but not both. I haven't checked the manual to see if there's a syntax problem with PERFORM UNTIL I TIMES but I suspect even if the statement passes syntax checking, it won't do what you think it will. I would run a loop from 1 to whatever. In the loop, set X to 1, then PERFORM loop TIMES COMPUTE X = X * loop END-PERFORM

Your solution number 2 won't produce a trailing 1 -- nor will it produce a horizontal pattern. I would solve this one by noting that after the first term, the nth term becomes a 1 followed by (n-1) zeroes followed by a 1 -- and I would set up a loop using reference modification to generate a variable with the pattern. What you don't want to do is use repeated DISPLAY statements since each DISPLAY statement starts on a new line (unless you use the language construct to not do so).
Robert Sample
Global moderator
 
Posts: 3363
Joined: Sat Dec 19, 2009 8:32 pm
Location: East Dubuque, Illinois
Has thanked: 1 time
Been thanked: 221 times

Re: How to handle number series logics in cobol pgm

Postby GuyC » Fri Sep 10, 2010 1:59 pm

Table 17. Binary operators
Binary operator M
+ Addition
- Subtraction
* Multiplication
/ Division
** Exponentiation
I can explain it to you, but i can not understand it for you.
GuyC
 
Posts: 315
Joined: Tue Aug 11, 2009 3:23 pm
Has thanked: 1 time
Been thanked: 4 times

Re: How to handle number series logics in cobol pgm

Postby joseseb » Fri Sep 10, 2010 5:15 pm

@all thank u 4 the reply...
yes guyc ..tats it..** operator works fine to get that series...and robert is there any other way other than reference modification
joseseb
 
Posts: 7
Joined: Thu Sep 09, 2010 9:23 pm
Has thanked: 0 time
Been thanked: 0 time

Re: How to handle number series logics in cobol pgm

Postby GuyC » Fri Sep 10, 2010 5:32 pm

Pseudo code :

if i =1 then 1 else (10 ** i + 1)
I can explain it to you, but i can not understand it for you.
GuyC
 
Posts: 315
Joined: Tue Aug 11, 2009 3:23 pm
Has thanked: 1 time
Been thanked: 4 times

Re: How to handle number series logics in cobol pgm

Postby Robert Sample » Fri Sep 10, 2010 5:33 pm

You could use an array, of course, or you could experiment with DISPLAY ... WITH NO ADVANCING.

What's the reluctance to use reference modification? It is a very good technique for dealing with certain problems.
Robert Sample
Global moderator
 
Posts: 3363
Joined: Sat Dec 19, 2009 8:32 pm
Location: East Dubuque, Illinois
Has thanked: 1 time
Been thanked: 221 times

Re: How to handle number series logics in cobol pgm

Postby joseseb » Fri Sep 17, 2010 9:37 am

Thanks robert..i have another doubt regarding this . Can we do arthematic operations on the identifier that we use along with perform statements like if i want to display pattern like
1
2 4
3 6 9
is the follw.. method correct
data
01 number1 pic 9(2) value zero
01 number1 pic 9(2) value zero
01 number1 pic 9(2)
procedure
perform varying number1 1 by 1 until number1<=3
perform varying number2 1 by 1 until number2<=number1
compute number3=number1*number2
display 'bb' number3 with no advancing. //bb - two blank spaces
end-perform
end-perform
stop-run
Do chk thhe code , and correct me if am wrng..:) thank u
joseseb
 
Posts: 7
Joined: Thu Sep 09, 2010 9:23 pm
Has thanked: 0 time
Been thanked: 0 time

Next

Return to Interview Questions

 


  • Related topics
    Replies
    Views
    Last post