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)+

Replies

  • nareshkumar6539
    nareshkumar6539
    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
    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.

You are reading an archived discussion.

Related Posts

A logic circuit has three Boolean inputs X,Y and Z. Its output is F(X,Y,Z) such that: F(X,Y,Z) = 1 if aX + bY +cZ > d = 0 otherwise. a,b,c,d...
Write an efficient algorithm to find the first non-repeated character in a string defined over the English alphabet set [a-z, A-Z]. For example, the first non-repeated character in teeter is...
Imagine a cubic array made up of an n*n*n arrangement of unit cubes: the cubic array is n cubes wide, n cubes high and n cubes deep. A special case...
The integers 1, 2,........., 10 are circularly arranged in an arbitrary order. Show that there are always three successive integers in this arrangement, whose sum is at least 17.
Marrying advanced micro electronics to biomimetics, UK and US researchers hope to give birth to cybeplasm microbots with a starting size of 1 cm that may well shrink to a...