Chapter 5 7. num = 0 repeat: if (num is Odd ) print (num is Odd) num = num + 1 until (num < 50) 11. Suppose N is the given integer.

Similar documents
THEORY OF COMPUTATION

Two Approaches to Algorithms An Example (1) Iteration (2) Recursion

Analysis of Algorithms. CSE Data Structures April 10, 2002

RECURSION AND LINKED LISTS 3

Assertions & Verification & Example Loop Invariants Example Exam Questions

Blum-Blum-Shub cryptosystem and generator. Blum-Blum-Shub cryptosystem and generator

LECTURE 9 Data Structures: A systematic way of organizing and accessing data. --No single data structure works well for ALL purposes.

Introduction to Computer Science Midterm 3 Fall, Points

Lecture 2 Algorithms with numbers

Algorithms 4. Odd or even Algorithm 5. Greatest among three numbers Algorithm 6. Simple Calculator Algorithm

Midterm 1. CS Intermediate Data Structures and Algorithms. October 23, 2013

Subject: Computer Science

CS302 Topic: Algorithm Analysis #2. Thursday, Sept. 21, 2006

Assertions & Verification Example Exam Questions

Computer Science E-119 Practice Midterm

CSCE 110 Dr. Amr Goneid Exercise Sheet (7): Exercises on Recursion

DATA STRUCTURES AND ALGORITHMS

1. Draw the state graphs for the finite automata which accept sets of strings composed of zeros and ones which:

Recall from Last Time: Big-Oh Notation

COMP3121/3821/9101/ s1 Assignment 1

Analysis of Algorithms. Unit 4 - Analysis of well known Algorithms

Data structure and algorithm in Python

CSEN 102 Introduction to Computer Science

McGill University Faculty of Science School of Computer Science. MIDTERM EXAMINATION - Sample solutions. COMP-250: Introduction to Computer Science

Sample Problems for Quiz # 2

CS 506, Sect 002 Homework 5 Dr. David Nassimi Foundations of CS Due: Week 11, Mon. Apr. 7 Spring 2014

Example Algorithms. CSE 2320 Algorithms and Data Structures Alexandra Stefan. University of Texas at Arlington. Last updated 9/7/2016

There are algorithms, however, that need to execute statements in some other kind of ordering depending on certain conditions.

Assignment 1 (concept): Solutions

Computability and Complexity Sample Exam Questions

CS:3330 (22c:31) Algorithms

Data Structures and Algorithms for Engineers

Algorithm Design and Recursion. Search and Sort Algorithms

VARIABLE, OPERATOR AND EXPRESSION [SET 1]

CS 103 Lecture 4 Slides

Lecture 1 Contracts. 1 A Mysterious Program : Principles of Imperative Computation (Spring 2018) Frank Pfenning

Programming in OOP/C++

Outline and Reading. Analysis of Algorithms 1

Loop Invariant Examples

UNIT 1 ANALYSIS OF ALGORITHMS

RECURSION, RECURSION, (TREE) RECURSION! 3

Computer Programming

Google placement paper

RECURSION 7. 1 Recursion COMPUTER SCIENCE 61A. October 15, 2012

Algorithms and Data Structures


Functions. Arash Rafiey. September 26, 2017

Lecture 1 Contracts : Principles of Imperative Computation (Fall 2018) Frank Pfenning

CS 103 Unit 8b Slides

How many leaves on the decision tree? There are n! leaves, because every permutation appears at least once.

Introduction to the Analysis of Algorithms. Algorithm

Emil Sekerinski, McMaster University, Winter Term 16/17 COMP SCI 1MD3 Introduction to Programming

Algorithm efficiency can be measured in terms of: Time Space Other resources such as processors, network packets, etc.

PROGRAM EFFICIENCY & COMPLEXITY ANALYSIS

Computer Science & Engineering 150A Problem Solving Using Computers

Sorting. Bubble Sort. Pseudo Code for Bubble Sorting: Sorting is ordering a list of elements.

Algorithm Discovery and Design

CS 161 Computer Security

(6) The specification of a name with its type in a program. (7) Some memory that holds a value of a given type.

Recursion: The Mirrors. (Walls & Mirrors - Chapter 2)

Algorithms. Chapter 8. Objectives After studying this chapter, students should be able to:

This was the second midterm from I have added a few comments in bold type, like this.

Algorithm Complexity Analysis: Big-O Notation (Chapter 10.4) Dr. Yingwu Zhu

COSC242 Lecture 7 Mergesort and Quicksort

Abstract Thinking, Intro to the Theory of Computation. SC/COSC Lecture Notes

More Complicated Recursion CMPSC 122


provided. Use any other space for rough work. The question paper has total 8 pages. Rough Work

Exam Sample Questions CS3212: Algorithms and Data Structures

Algorithm Analysis. Spring Semester 2007 Programming and Data Structure 1

Scientific Computing. Algorithm Analysis

Department of Computer Science & Engineering Indian Institute of Technology Kharagpur. Practice Sheet #04

Chapter 8 Algorithms 1

The Running Time of Programs

Chapter 3. More Flow of Control. Copyright 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

Recall our recursive multiply algorithm:

Quiz 8 May 21, 2015 Computer Engineering 80N

CSE 230 Computer Science II (Data Structure) Introduction

Algorithm. Building blocks of algorithm

CSCI 2170 Algorithm Analysis

Lecture 6 Sorting and Searching

Data Structures CHAPTER 11. Review Questions. Multiple-Choice Questions. Exercises. (Solutions to Odd-Numbered Problems)

REVIEW. The C++ Programming Language. CS 151 Review #2

Algorithm Discovery and Design. Why are Algorithms Important? Representing Algorithms. Chapter 2 Topics: What language to use?

Softwaretechnik. Program verification. Albert-Ludwigs-Universität Freiburg. June 28, Softwaretechnik June 28, / 24

Halting Measures and Termination Arguments

A Public Key Crypto System On Real and Com. Complex Numbers

Assignment 1. Stefano Guerra. October 11, The following observation follows directly from the definition of in order and pre order traversal:

1 Short Answer (10 Points Each)

O(n): printing a list of n items to the screen, looking at each item once.

Lecture 9: Sorting Algorithms

Softwaretechnik. Program verification. Software Engineering Albert-Ludwigs-University Freiburg. June 30, 2011

CSCE 2014 Final Exam Spring Version A

CSI33 Data Structures

1 P a g e A r y a n C o l l e g e \ B S c _ I T \ C \

Algorithm must complete after a finite number of instructions have been executed. Each step must be clearly defined, having only one interpretation.

Karnaugh Maps. Kiril Solovey. Tel-Aviv University, Israel. April 8, Kiril Solovey (TAU) Karnaugh Maps April 8, / 22

Classic Data Structures Introduction UNIT I

CSC128 FUNDAMENTALS OF COMPUTER PROBLEM SOLVING Chapter 6: One Dimensional Array

Lecture 1. Introduction

Transcription:

Chapter 5 7. num = 0 repeat: if (num is Odd ) print (num is Odd) num = num + 1 until (num < 50) 11. Suppose N is the given integer. Then the following will work. You may want to ask your students how this solution could be made more efficient. X = 1 while (X N): if (X divides N): print(x) X = X + 1 13. Pseudocode is a relaxed version of a programming language used to jot down ideas. A formal programming language prescribes strict rules of grammar that must be obeyed. 20. Approximately 122 meters. Let the distance between the hives be y. Since each bee flies at a constant speed, the ratio of the distances traveled must be the same each time the meet. Therefore, 50/(y 50) = (y + 20)/(2y 20). Solving for y implies that y is approximately 7.5 meters or 122 meters. 22. Body: Everything indented below the while Initialization: The first two assignment statements Modification: The last assignment statement (Some could argue that it is the last three assignment statements.) Test: while (Current < 100) The output will be 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89. 23. It prints all prime numbers from 1 to 100 as shown below: 1 2 3 5 7 11 13 17 19 23

29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97 25. When searching for J: H, L, J When searching for Z: H, L, N, 0 26. Sequential search: 3000 Binary search: at least 13 37. a. binary b. sequential c. binary d. binary e. 4 for the sequential search; 3 for the binary search. 38. Armstrong (num, digits) n = num while (num > 0) do digit = n % 10; sum = sum + pow (digit, digits) n = n / 10; if (sum = num) print it is an Armstrong number else print it is not an Armstrong number 46. alphabeticalsorting (char a[m][n]) char temp[100]; int i, j;

for (i =0; i < m; i++) for (j = i+1; j < m+1; j++) if (stringcompare(a[i], a[j]) > 0) stringcopy (temp, a[i]); stringcopy (a[i], a[j]); stringcopy (a[j], temp); print ("Sorted strings are : "); for (i = 0; i < m+1; i++) print (a[i]); int stringcompare (char s1[], char s2[]) int i = 0; while ( s1[i]!= '\0' ) if ( s2[i] == '\0' ) return 1; else if ( s1[i] < s2[i] ) return -1; else if ( s1[i] > s2[i] ) return 1; i++; return 0; stringcopy (char dest[], char src[]) for (i=0; src[i]!= '\0'; ++i) dest[i] = src[i]; dest[i] = '\0'; 53. No. The algorithm will not compute the correct answer when X = 0. 56. a. Preconditions: The input list is arranged in ascending order. Loop invariant: The target value is not equal to any list entry preceding the current entry. b. Since the input list can contain only a finite number of entries and each time

through the body of the loop the current entry is advanced by one, at some point there will no longer remain entries to be considered. Chapter 6 2. Suppose the value of x is stored in the memory cell whose address is XY and the program begins at address 00. 2100 31XY 2003 B110 2201 5112 31XY B006 23. if (X > 5): X = X + 2 else: X = X + 1 29. When passing by value, the sequence 7, 5 would be printed. When passing by reference the sequence 7, 7 would be printed. 42. Any string of the form x n yx n, where n is a nonnegative integer. 45. When performing either assignment statement, the value of X will already be in a register as a result of the comparison performed earlier. Thus, it need not be retrieved from memory. 51. Answers will vary. Chapter 11

28. 29.

30. 31. It does not distinguish between different states as well. Chapter 12 5. The following routine places the quotient of X and Y in Z: clear Z copy X to XX while XX not 0: copy Y to YY while YY not 0: decr XX decr YY incr Z copy X to XX while XX not 0: decr Z clear XX 6. It computes X Y when X Y. 10. One approach would be incr name1 clear name2 repeat: incr name2 decr name1 until (name1 equals 0) decr name2

26. Yes, searching through a binary tree for a particular value is a polynomial problem. Its time complexity is O(nlogn) and nlogn is equals to n ^ m. 27. def fib( n): if (n <= 1) return n return fib(n-1) + fib(n-2) Time Complexity: T(n) = T(n-1) + T(n-2) which is exponential (non polynomial). 34. Yes, it is deterministic. The person following the directions never has to make a choice. At each step, that person merely does what he or she is told. 42. The merge sort would perform 4 name comparisons when sorting the list Alice, Bob, Carol, and David. The precise number performed when sorting the list Alice, Bob, Carol, David, and Elaine depends on the point at which the odd entry is considered. 47. (n - 1) + (n - 2) +... + 1