Question

(20 pts) To understand the value of recursion in a programming language: implement the binary search...

  1. (20 pts) To understand the value of recursion in a programming language: implement the binary search algorithm first as a recursive function and again using a conditional loop. Your program should create an array of characters holding the letters ‘A’ – ‘Z’ and find the index in the array where the letter ‘K’ is stored. You may use any programming language that supports recursion.
  1. (5pts) Define syntax and semantics and give an example.
  1. (5pts) Why is it important for a grammar to be unambiguous?

  1. (5pts) Write an EBNF description for a C switch statement.
  1. (5pts) Rewrite the BNF grammar below to give + precedence over * and force + to be right associative

<assign> à<id> = <expr>

<id> àA | B | C

<expr> à<expr> + <expr>

             | <expr> * <expr>

             | ( <expr> )

             | <id>

  1. (6pts) Consider the following BNF, with start symbol <S>.
 
       
         <S> à[ <B>, <S> ]  | <B>
        <B> à<C>  |  ( <B> )
        <C>  à a  |  b  |  c
 
Which of the following are valid sentences in this language?   Give parse trees if possible.  
 
 
a)  (((b) ))                  
 
b) [ a, (b) ]   
 
c)  [ [c,c], [b,b] ]  
 
 
  1. (5pts) What are attribute grammars used for?
  1. (5pts) Compute the weakest precondition for b = (c + 10)/3 {b > 6}

  1. (5pts) Computer the weakest precondition for the statement sequence:

a = 2b + 1;

b = a – 3 {b < 0}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Answer to the first question is provided below. Please ask other questions in a separate post.

Binary search algorithm:

# Iterative Binary Search
def binarySearch(arr, l, r, x):
while l <= r:
mid = int(l + (r - l)/2)
print(ord(arr[mid]),ord(x) )
if arr[mid] == x:
return mid
elif arr[mid] > x:
l = mid + 1
else:
r = mid - 1
return -1

arr = [ 'A', 'C', 'K', 'L', 'M' ]
x = 'K'
result = binarySearch(arr, 0, len(arr)-1, x)
if result != -1:
print("Element is present at index ",result)
else:
print("Element is not present in array")


# Recursive binary search.
def binarySearch (arr, l, r, x):
if r >= l:
mid = int(l + (r - l)/2)
if arr[mid] == x:
return mid
elif arr[mid] > x:
return binarySearch(arr, l, mid-1, x)
else:
return binarySearch(arr, mid+1, r, x)
else:
return -1

# Test array
arr = [ 'A', 'C', 'K', 'L', 'M' ]
x = 'K'

# Function call
result = binarySearch(arr, 0, len(arr)-1, x)
if result != -1:
print("Element is present at index ",result)
else:
print("Element is not present in array")

OUTPUT:

In the recursive search algorithm, the number of comparisons is one while in the iterative algorithm of binary search, the number of comparisons is 3. Thus, the recursive is faster than iterative one in worst cases. But in bast cases, like if the element is present at the first index then the iterative algorithm will be faster.

Add a comment
Know the answer?
Add Answer to:
(20 pts) To understand the value of recursion in a programming language: implement the binary search...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • 2. Consider the following grammar:                         <assign> à <id> = <expr>       

    2. Consider the following grammar:                         <assign> à <id> = <expr>            <id>   à A | B | C            <expr> à <id> + <expr>                      | <id> * <expr>                      | ( <expr> )                      | <id> Show a parse tree and leftmost derivation for the following statements: (a)        A = ( A + B ) * C (b)        A = A * ( B + C ) 3. [10 Points] Show that the following grammar is...

  • Solve the following questions. All questions are mandatory. Q1: What’s wrong in the following grammar? S...

    Solve the following questions. All questions are mandatory. Q1: What’s wrong in the following grammar? S → ABC A → aA|aa B → bB|B C → cC|cccc Q2: Describe the strings generated by the following grammar S → aSa S → bSb S → aa S → bb Q3: Consider the following grammar: S → ABC A → aA|a B → bB|b C → cC|c Change the above grammar such that it generates L1={anbmck; n>=3,m>=3,k>=4} Q4: Use the following grammar...

  • Problem 4: (7 pts) To understand the value of recursion in a programming language, write a...

    Problem 4: (7 pts) To understand the value of recursion in a programming language, write a Program in C++ or Python that implements quicksort, first using recursion and then without recursion. Submit your program source code and  screenshot(s) of the output in a separate file (pdf or .docx) Problem 5: (7 pts) To understand the value of counting loops, write a program in C++ or Python that implements matrix multiplication using counting loop constructs. Let the user input the size/dimensions of...

  • 1. (p. 2-3.) Which of the following is NOT a reason for studying concepts of programming...

    1. (p. 2-3.) Which of the following is NOT a reason for studying concepts of programming languages according to Sebesta? a. Increased capacity to express ideas. b. Improved background for choosing appropriate languages. c. Increased ability to design new languages. d. Increased ability to learn new languages. 2. (p. 5-6.) What programming language has dominated scientific computing over the past 50 years? a. FORTRAN b. ALGOL c. SNOBOL d. PL/I 3. (p. 6.) What programming language has dominated artificial intelligence...

  • 3 points) Question Three Consider the context-free grammar S >SS+1 SS 1a and the string aa...

    3 points) Question Three Consider the context-free grammar S >SS+1 SS 1a and the string aa Give a leftmost derivation for the string. 3 points) (4 poiots) (5 points) (3 points) sECTION IWOLAttcmpt.any 3.(or 2) questions from this.scction Suppose we have two tokens: (1) the keyword if, and (2) id-entifiers, which are strings of letters other than if. Show the DFA for these tokens. Give a nightmost derivation for the string. Give a parse tree for the string i) Is...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT