Introduction-Datastructues and Algorithms-Solutions, Exercises of Data Structures and Algorithms

This is solution manual provided by Muhammad Uzair at University of Engineering and Technology Lahore. It related to Data Structures and Algorithms course. Its main points are: Introduction, Round-off, Errors, Routine, Procedure, Recursively, Self-referential, Theorem, Hypothesis

Typology: Exercises

2011/2012

Uploaded on 07/16/2012

sangodkar
sangodkar 🇵🇰

5

(2)

12 documents

1 / 6

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Data Structures & Algorithm Analysis in C
(second edition)
MarkAllenWeiss
FloridaInternationalUniversity
SolutionsManual
By:
Muhammad Uzair
05-E-147
U.E.T. Lahore
Pakistan
pf3
pf4
pf5

Partial preview of the text

Download Introduction-Datastructues and Algorithms-Solutions and more Exercises Data Structures and Algorithms in PDF only on Docsity!

Data Structures & Algorithm Analysis in C

(second edition)

MarkAllenWeiss

FloridaInternationalUniversity

SolutionsManual

By:

Muhammad Uzair

05-E-

U.E.T. Lahore

Pakistan

[email protected]

Preface

Included in this manual are answers to most of the exercises in the textbook Data Structures and Algorithm Analysis in C, second edition, published by Addison-Wesley. These answers reflect the state of the book in the first printing.

Specifically omitted are likely programming assignments and any question whose solu- tion is pointed to by a reference at the end of the chapter. Solutions vary in degree of complete- ness; generally, minor details are left to the reader. For clarity, programs are meant to be pseudo-C rather than completely perfect code.

Errors can be reported to [email protected]. Thanks to Grigori Schwarz and Brian Harvey for pointing out errors in previous incarnations of this manual.

Chapter 1: Introduction

1.3 Because of round-off errors, it is customary to specify the number of decimal places that should be included in the output and round up accordingly. Otherwise, numbers come out looking strange. We assume error checks have already been performed; the routine Separate O is left to the reader. Code is shown in Fig. 1.1.

1.4 The general way to do this is to write a procedure with heading

void ProcessFile( const char *FileName );

which opens FileName, O does whatever processing is needed, and then closes it. If a line of the form

#include SomeFile

is detected, then the call

ProcessFile( SomeFile );

is made recursively. Self-referential includes can be detected by keeping a list of files for which a call to ProcessFile O has not yet terminated, and checking this list before making a new call to ProcessFile. O

1.5 (a) The proof is by induction. The theorem is clearly true for 0 < X O ≤ 1, since it is true for X O = 1, and for X O < 1, log X O is negative. It is also easy to see that the theorem holds for 1 < X O ≤ 2, since it is true for X O = 2, and for X O < 2, log X O is at most 1. Suppose the theorem is true for p O < X O ≤ 2 p O (where p O is a positive integer), and consider any 2 p O < Y O ≤ 4 p O ( p O ≥ 1). Then log Y O = 1 + log ( Y O / 2) < 1 + Y O / 2 < Y O / 2 + Y O / 2 ≤ Y O, where the first ine- quality follows by the inductive hypothesis. (b) Let 2 X O^ = A O. Then A O B O^ = (2 X O) B O^ = 2 XB O. Thus log A O B O^ = XB O. Since X O = log A O, the theorem is proved.

1.6 (a) The sum is 4 / 3 and follows directly from the formula.

(b) S O = 4

__^1

___^2

___^3

+...^. 4 S O = 1 +

__^2

___^3

+...^. Subtracting the first equation from

the second gives 3 S O = 1 + 4

__^1

___^2

+...^. By part (a), 3 S O = 4 / 3 so S O = 4 / 9.

(c) S O = 4

__^1

___^4

___^9

+...^. 4 S O = 1 +

__^4

___^9

___^16

+...^. Subtracting the first equa-

tion from the second gives 3 S O = 1 + 4

__^3 +

___^5 +

_____^7 (^) +... (^). Rewriting, we get

3 S O = 2

i O= 0

Σ

4 i O

_____^ i (^) + i O= 0

Σ

4 i O

_____^1. Thus 3 S O = 2(4 / 9) + 4 / 3 = 20 / 9. Thus S O = 20 / 27.

(d) Let SN O = i O= 0

Σ

4 i O

_____^ i O N O

. Follow the same method as in parts (a) - (c) to obtain a formula for S (^) N O

in terms of SN O− 1 , S (^) N O− 2 , ..., S O 0 and solve the recurrence. Solving the recurrence is very difficult.

______________________________________________________________________________________________________________________________________________________________

double RoundUp( double N, int DecPlaces ) { int i; double AmountToAdd = 0.5;

for( i = 0; i < DecPlaces; i++ ) AmountToAdd /= 10; return N + AmountToAdd; }

void PrintFractionPart( double FractionPart, int DecPlaces ) { int i, Adigit;

for( i = 0; i < DecPlaces; i++ ) { FractionPart *= 10; ADigit = IntPart( FractionPart ); PrintDigit( Adigit ); FractionPart = DecPart( FractionPart ); } }

void PrintReal( double N, int DecPlaces ) { int IntegerPart; double FractionPart;

if( N < 0 ) { putchar(’-’); N = -N; } N = RoundUp( N, DecPlaces ); IntegerPart = IntPart( N ); FractionPart = DecPart( N ); PrintOut( IntegerPart ); /* Using routine in text */ if( DecPlaces > 0 ) putchar(’.’); PrintFractionPart( FractionPart, DecPlaces ); } Fig. 1.1.


i O=OI N O / 2 OK

Σ

N i

__^1 =

i O= 1

Σ

N i

__^1 −

i O= 1

Σ

OI N O / 2 − 1 OK i

__^1 ∼

∼ ln^ N O^ −^ ln^ N O /^^2 ∼∼ ln^ 2.