Banashree Patra
Banashree Patra
Branch Unspecified
23 Apr 2012

Write a C Program

Consider the following program fragment:
1. i = 1; sum=0;
2. while (i <= n) do
begin
3. sum = sum + a;
4. i = i + 1;
end
Let
A represent the initialization in line 1
B represent the action within the loop in line 3
I represent the increment in line 4
T represent the test implied by line 2
Which of the following regular expressions represents all possible sequences of steps
taken by this program? Justify your answer.
I. A(TBI)*
II. AT+B*I*
III. AT(BIT)+
IV. AT(BIT)*
V. A(TBI)+
nareshkumar6539

nareshkumar6539

Branch Unspecified
6 years ago
Answer is AT(BIT)*
first of all value will be initialized(A),after that condition will be checked(AT),condition can be TRUE or FALSE,if condition is false it will never enter into loop.If condition is true it will enter into loop first sum will be added after that i value incremented after that it again check for condition if true it enter into loop otherwise come out from the loop.
Note:-
In regular expressions * means 0 or more(0,1,2,---infinity)
+ means 1 or more(1,2,3---infinity)
I. is wrong because condition is not checked if * takes 0 then the expression simply A condition must be checked.First it must be checked the condition(AT)
II is wrong because that expression doesn`t show after entering the loop performing 2 steps we have to again check the condition it is not showing
III is wrong because it is not showing expression for first itself condition is false it doecn`t enter into loop but in this even condition false also it will enter into the loop
V is wrong because of above reason only
Manashree Thokal

Manashree Thokal

Branch Unspecified
6 years ago
IV. AT(BIT)*

A is definitely executed.
Even T is. The condition is definitely checked.
B is always followed by I, as its in a loop. Control goes to T again.
It is a closure as the entry in loop depends on value of n. If i is not <= n, BIT will be empty.

Share this content on your social channels -

Only logged in users can reply.