View previous topic :: View next topic
|
Author |
Message |
rajrohith
New User
Joined: 15 Apr 2005 Posts: 54 Location: chennai
|
|
|
|
i have one doubt in add statement
for example
01 a pic 9
add -2 to a.
add -4 to a.
add -6 to a.
i want to know what is the value of A right now
thanks
raj |
|
Back to top |
|
|
William Thompson
Global Moderator
Joined: 18 Nov 2006 Posts: 3156 Location: Tucson AZ
|
|
|
|
What do you think it is? |
|
Back to top |
|
|
rajrohith
New User
Joined: 15 Apr 2005 Posts: 54 Location: chennai
|
|
|
|
my interviewer asked this question
but i didnt try this example |
|
Back to top |
|
|
jcbhimani
New User
Joined: 30 Nov 2006 Posts: 12 Location: Ahmedabad
|
|
|
|
In my view -6 will be added to the old value of a and stored in a. |
|
Back to top |
|
|
William Thompson
Global Moderator
Joined: 18 Nov 2006 Posts: 3156 Location: Tucson AZ
|
|
|
|
well, just for fun, give it a try, and explain to me how you arrived at your answer.... |
|
Back to top |
|
|
jcbhimani
New User
Joined: 30 Nov 2006 Posts: 12 Location: Ahmedabad
|
|
|
|
If we have not mentioned any initial value for a then I think some garbage value will be there and -6 will be added to that.
Please give me correct answer.
I am curious to know it...................... |
|
Back to top |
|
|
William Thompson
Global Moderator
Joined: 18 Nov 2006 Posts: 3156 Location: Tucson AZ
|
|
|
|
well, for the sake of argument, assume that it was initialized to zero, what is its value after the adds? |
|
Back to top |
|
|
rajrohith
New User
Joined: 15 Apr 2005 Posts: 54 Location: chennai
|
|
|
|
i told just 6 only
because if we specify sign we will see -6
but in this example we didnt specify any sign
so what is the value of a
by
raj |
|
Back to top |
|
|
jcbhimani
New User
Joined: 30 Nov 2006 Posts: 12 Location: Ahmedabad
|
|
|
|
see it the initial value is 0 then in first operation value of a will be -2 then -6 and finally -12. |
|
Back to top |
|
|
jcbhimani
New User
Joined: 30 Nov 2006 Posts: 12 Location: Ahmedabad
|
|
|
|
I think I am wrong because a is specified as PIC 9 so how can -12 be stored. |
|
Back to top |
|
|
rajrohith
New User
Joined: 15 Apr 2005 Posts: 54 Location: chennai
|
|
|
|
i accept that answer but can we see the minus sign
by
raj |
|
Back to top |
|
|
William Thompson
Global Moderator
Joined: 18 Nov 2006 Posts: 3156 Location: Tucson AZ
|
|
|
|
I think that:
0+-2=-2 but there is no sign so now a=2
2+-4=-2 but there is no sign so now a=2
2+-6=-4 but there is no sign so now a=4
final answer? 4 |
|
Back to top |
|
|
rajrohith
New User
Joined: 15 Apr 2005 Posts: 54 Location: chennai
|
|
|
|
thanks for ur reply
raj |
|
Back to top |
|
|
dick scherrer
Moderator Emeritus
Joined: 23 Nov 2006 Posts: 19244 Location: Inside the Matrix
|
|
|
|
Hello,
The code as shown may very well get a S0C7. "a" doesn't have an initial numeric value. You may get strange results if there happens to already be some number at that location.
If you use the following code:
01 A PIC 9 VALUE 0.
01 B PIC S9 VALUE 0.
DISPLAY A.
ADD -2 TO A.
DISPLAY A.
ADD -4 TO A.
DISPLAY A.
ADD -6 TO A.
DISPLAY A.
DISPLAY ' '.
DISPLAY B.
ADD -2 TO B.
DISPLAY B.
ADD -4 TO B.
DISPLAY B.
ADD -6 TO B.
DISPLAY B.
DISPLAY ' '.
you get:
0
2
2
4
{
K
O
K
The second set of "answers" is due to the sign The { is a signed zero the letters are negative numbers K=-2, O=-6, etc.
|
|
Back to top |
|
|
rajrohith
New User
Joined: 15 Apr 2005 Posts: 54 Location: chennai
|
|
|
|
what is difference between working storage copy book
and procedure division copy book
by
raj |
|
Back to top |
|
|
William Thompson
Global Moderator
Joined: 18 Nov 2006 Posts: 3156 Location: Tucson AZ
|
|
|
|
raj, use a new topic when you change it..
One copys data division statements while the other copys procedure division statements. |
|
Back to top |
|
|
jcbhimani
New User
Joined: 30 Nov 2006 Posts: 12 Location: Ahmedabad
|
|
|
|
William what I have read is::
The operational sign is not stored as a separate character. the operational sign is stored as the zone bits of the rightmost digit position of the data item.
i.e A = +1
B = +2 ................... I = +9
J = -1
K = -2
............. R = -9 |
|
Back to top |
|
|
William Thompson
Global Moderator
Joined: 18 Nov 2006 Posts: 3156 Location: Tucson AZ
|
|
|
|
Yes, but since the pic did not specify the sign, when COBOL stored the result of the add back it made sure there was no sign.
If there had been a sign, what would the results been then? |
|
Back to top |
|
|
jcbhimani
New User
Joined: 30 Nov 2006 Posts: 12 Location: Ahmedabad
|
|
|
|
If PIC S9 was mentioned then in my opinion the answer would be:
Suppose 0 is teh initial value in a
add -2 to a ----------> a = -2 (k)
add -4 to a -----------> a = -6 (o)
add -6 to a -----------> a = -12 (internally it would be stored as k, 1 will be truncated)
What do you say William ?
Correct me if I am wrong. |
|
Back to top |
|
|
William Thompson
Global Moderator
Joined: 18 Nov 2006 Posts: 3156 Location: Tucson AZ
|
|
|
|
ya got it. |
|
Back to top |
|
|
|