GE8151_PSPP_Unit_1
Algorithms, building blocks of algorithms (statements, state, control flow, functions), notation (pseudo code, flow chart, programming language), algorithmic problem solving, simple strategies for developing algorithms (iteration, recursion) Illustrative problems:find minimum in a list, insert a card in a list of sorted cards, Guess an integer number in a range, Towers of Hanoi.
27 flashcards · Programming Languages · by Nandakumar SD
A program is a set of instructions that tells the computer how to solve a particular problem.
An algorithm solves a problem in a finite number of steps. TRUE or FALSE ?
TRUE
Algorithms should be (a) precise (b) unambiguous (c) clear (d) all of these
d
Decision , Repetition and Iteration statements are used to change the sequence of execution of instructions.
Building blocks of algorithm are state instruction control flow - sequence, decision, repetition function
To check whether a given number is even or odd, you will use which type of control structure? (a) sequence (b) decision (c) repetition (d) all of these
b
Algorithm to add two numbers Step 1 : Start Step 2 : Input first number as A Step 3 : Input second number as B Step 4 : Set Sum = A + B Step 5 : Print Sum Step 6 : End
It's an example for Sequence - Sequence means that each step of the algorithm is executed in the specified order. Arithmetic Operator used for addition is +
Algorithm to find greatest for three numbers Step 1: Start Step 2: Read the three numbers A,B,C Step 3: Compare A and B. If A is greater perform step 4 else perform step 5. Step 4: Compare A and C. If A is greater, output "A is greatest" else output "C is greatest" Step 5: Compare B and C. If B is greater, output "B is greatest" else output "C is greatest" Step 6: End
Algorithm to swap two numbers (with third variable) Step 1: Start Step 2: Input first number as A Step 3: Input second number as B Step 4: Set temp = A Step 5: Set A = B Step 6: Set B = temp Step 7: Print A, B Step 8: End
Algorithm to swap two numbers (without third variable) Step 1: Start Step 2: Input first number as A Step 3: Input second number as B Step 4: Set A = A+B Step 5: Set B = A-B Step 6: Set A = A-B Step 7: Print A, B Step 8: End
In a flowchart, which symbol is represented using a rectangle? (a) terminal (b) decision (c) activity (d) input/output
c
Flowchart is used to express algorithms and as a mode of human communication
Flowchart is a graphical representation of algorithm
Terminal symbol depicts the flow of control of the program. True of False ?
False
Above symbol represents what action?
Decision or Conditional
Which of the following details are omitted in pseudocodes? (a) variable declaration (b) system specific code (c) subroutines (d) all of these
d
Recursion is a technique of solving a problem by breaking it down into smaller and smaller sub problems until you get to a small enough problem that it can be easily solved.
Syntax and semantics of a language are strictly checked in (a) algorithm (b) flowchart (c) pseudo code (d) program
d
Programming languages have a vocabulary of _____ for instructing a computer to perform specific tasks. (a) syntax (b) semantics (c) both of these (d) none of these
c
Labelled connectors are square in shape. True or False?
False
A program written using any programming language has to be converted into machine language so that the computer can understand it. There are two ways to do this: compile the program or interpret the program. True of False?
True
Every recursive solution has two major cases. They are base case and recursive case
Repeated execution of a set of statements
Iteration
A “brute-force” approach for solving a given problem is to: (a) Try all possible algorithms for solving the problem (b) Try all possible solutions for solving the problem. (c) Try various representations of the problem. (d) All of the above
Hint:
The syntax of a given language is, (a) the set of symbols in the language. (b) the acceptable arrangement of symbols. (c) both of the above
c
A simple Tower of Hanoi puzzle consists of 3 pegs and 3 circular disks. What is the least number of moves that are required to move the disks to another empty peg?
7
What is the general formula that is used to calculate the least number of moves required to solve a Tower of Hanoi puzzle with 3 pegs and n disks? (Hint: given that the first few terms of the sequence are 1, 3, 7, 15...)
2n-1
Let's say a total of m moves are required to complete a Tower of Hanoi puzzle with 3 pegs and p disks. Now, how many moves are needed to solve the puzzle with (p+1) disks? (a) 2m + 1 (b) m + 2 (c) 2m - 1 (d) m - 2
a
Begin Procedure Hanoi(disk, source, dest, spare) IF disk = 1 THEN Move disk from source to dest ELSE CALL Hanoi(disk -1, source, spare, dest) Move disk from source to dest CALL Hanoi(disk -1, spare, dest, source) End Procedure Hanoi(disk, source, dest, spare)