Write a C Program

Banashree Patra
@banashree-patra-m0OJwR โ€ข Oct 22, 2024

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

Welcome, guest

Join CrazyEngineers to reply, ask questions, and participate in conversations.

CrazyEngineers powered by Jatra Community Platform

  • nareshkumar6539

    @nareshkumar6539-BKuVbx • Apr 22, 2012

    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-7mZH3V • Apr 23, 2012

    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.