

Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!


Function Swap(int x, y)
1: int temp; 2: temp = x; x = y; y = temp; Algorithm 1.1: Passing through Value
Function Swap(int ∗ x, ∗y)
1: int temp; 2: temp = ∗x; ∗x = ∗y; ∗y = temp; Algorithm 1.2: Passing through Reference
Algorithm (1.1) receives only the values of the parameters of the variables x and y, whereas Algorithm (1.2) receives the addresses of these two variables. 2
(a) In Imperative programming, e.g., C, the programmer directly accesses memory locations through variables and states explicitly what is needed to be done through assignment statements and loops. (b) A Functional programming language, e.g., LISP is applicative in nature, since it bases its description of computation on the evaluation of functions. The basic mechanism of such a language is the function call. Ideally, the notions of variables, assignment and loops, do not exist. (c) The Logic programming paradigm is based on Symbolic Logic, e.g., PROLOG. The idea here is for the programmer to achieve his end by specifying merely what needs to be done, as opposed to explicitly
stating how that thing needs to be done. As in Functional programming, there is no need for control abstractions, such as loops and selection. Such programming is also called “Declarative programming”, since it is sufficient to merely declare properties, without specifying the execution sequence.
2
Solution: The different types of errors that can occur in a program are as follows:
(a) Lexical errors - For instance, a symbol may be used inappropriately, e.g., x@ = 3, in C, or a keyword may be misspelled. (b) Syntax errors - For instance, malformed expressions, missing tokens, etc. (c) Semantic errors - Using undeclared variables, making assignments, without respecting types and so on. (d) Logical errors - The program logic does not correspond to what the programmer intended.
2
S → A 1 B A → 0 A | ≤ B → 0 B | 1 B | ≤
Show that the string 1001 belongs to L, by providing a derivation. Solution:
S ⇒ A 1 B ⇒ 1 B ⇒ 10 B ⇒ 100 B ⇒ 1001 B ⇒ 1001