0% found this document useful (0 votes)
5K views89 pages

Foundations of Computer Science - Solutions To Selected Exercise

Uploaded by

Ian
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
0% found this document useful (0 votes)
5K views89 pages

Foundations of Computer Science - Solutions To Selected Exercise

Uploaded by

Ian
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
You are on page 1/ 89
s Solutions to Selected Exercises <% Chapter 1. Computer Science: The Mechanization of Abstraction © Section 1.3 1.8.1: The static part of a data model consists of the values for the objects in the model; the dynamic part consists of the operations that can be applied to these ‘values, For example, we can think of the set of integers with the operation addition asa data modd. The static part is the set of integers and the dynamic part is the addition operator. 1.8.3: The data objects in aline-oriented text editor, such as vi, are files consisting of sequences of lines, where each line is a sequence of characters. A cursor identifies a position within a line, There are operators for positioning the cursor within a file. Typical operations on lines inchide inserting an additional line and ddeting an existing line. AA line may be modified by inserting, deleting, or changing characters within it, In addition, there are operators for creating, writing, and reading files. © Section 1.4 L441: An identifier can be one of the names for a box. For example, an identifier xc in C may be attached to a bax containing an integer by means of a variable dedaration int x3. One of the names of that integer box is then x. 2. LOLUTIONI TO HELECTED EXERCHEN, Chapter 2. Iteration, Induction, and Recursion Section 2.2 2.2.1(a): With 5 dements in the amay, SelectionSort makes 4 iterations with the Koop-index i= 0, 1,2,3. The first iteration makes 4 comparisons, the second 3, the third 2, the fourth 1, for a total of 10 comparisons. With the array 6,8,14, 17,23, there are no swaps (exchanges of elements) in any iteration. 2.2.1(b): On the array 17,23,14,6,8, SelectionSort makes 4 iterations, The ‘mumbers of comparisons and swaps made during each iteration are summatized in the following table, We shall not regard a swap as having occurred if the selected clement is already in its proper position. However, the reader should be aware that lines (6) - (8) of Fig. 2.2 are executed regantless of whether a swap is needed. Note ‘that when small = 4, these lines have no effect. ITERATION | ARRAY AFTER ITERATION | No OF COMPARIIONI [No OF SWAPII Start 17,23, 14,6,8 = = 1 6,23,14, 17,8 4 1 2 6,8, 14,17,23 3 1 3 6,8; 14,17,23 2 0 4 6,8; 14, 17,23 1 o 2.2.3: In what fellows, we use the conventions and macres of Section 1.6. To begin, ‘we use the cell/ist macto to define linked lists of characters, as: DefCell(char, CELL, LIST); Here is the function precedes. Bociean precedes(LIST I LIST #) { i#(L~>elenent == H-Pelenent) return precedes(I~™next ,H-next); return (L->elenent < i->elenent); } 2.25: If all n dements in the amay A are the same, then SelectionSort(A, n) makes n(n— 1)/2.comparisons but no swaps. 2.2.7: Let T be an arbitrary type. Define ‘typedef T TARRAY DNAX ; ‘TARRAY A; CHAPTER 2. ITERATION, INDUCTION, AND RECURIION 3 We modify SelectionSort as follows to sort elements of type 7. The function ey(x) returns the key of type K for the element x, We assume that the function ‘t(a,,v) retums TRUE if wis “less than” v and FALSE otherwise, where u and v are elements of type K. void SelectionSort(TARRAY A, int n) { ant i, j, small; T temp; for(i=0; ict; 14) { + + 22ai: S a) Yei-0 “2 » Yer & : 9 Tz s Section 2.3 2.8.1(a): We shall prove the fellowing statement S(n) by induction on n, forn > 1. STATEMENT S(n): Visrm+ ye & BASIS, The basis, n= 1, is obtained by substituting 1 for nin S(n). Doing so, we get SL, i= 1. We thus see that S(1) is true, INDUCTION. Now assume that > 1 and that S(n) is true, We must prove S(n-+1), which is

You might also like