Consider the following algorithm Construct a scenario for wh

Consider the following algorithm: Construct a scenario for which the program terminates What are the possible values of n when the program terminates? Does the program terminate for all scenarios? Does the program terminate for all fair scenarios?

Solution

for P
1) scenario for which program terminates

interger n <- 0
boolean flag <- true

p1.while flag=false
p2.n<-1 = n

or

interger n <- 0
boolean flag <- false

p1.while flag=true
p2.n<-1 = n

2)0

3)no,for some scenario it wont terminates

eg:
interger n <- 0
boolean flag <- false

p1.while flag=false
p2.n<-1 = n

4)no,for some scenario it wont terminates

for q:

1) scenario for which program terminates

interger n <- 0
boolean flag <- true

while flag=false
if n=0
flag<-true


or

interger n <- 0
boolean flag <- false

while flag=true
if n=0
flag<-true


2)0,1,2,3...

3)no,for some scenario it wont terminates
interger n <- 0
boolean flag <-false

while flag=false
if n=0
flag<-true

4)no,for some scenario it wont terminates

 Consider the following algorithm: Construct a scenario for which the program terminates What are the possible values of n when the program terminates? Does the
 Consider the following algorithm: Construct a scenario for which the program terminates What are the possible values of n when the program terminates? Does the

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site