0% found this document useful (0 votes)
189 views16 pages

sm2 130127132631 Phpapp02

The document describes algorithms for several tasks: 1) Calculating the average and maximum of a data set. 2) Approximating the cosine function using a Taylor series expansion. 3) Finding the real roots of a quadratic equation. 4) Calculating a student's average grade based on quiz, homework, and final exam scores.

Uploaded by

Ashraful A. Khan
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 DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
189 views16 pages

sm2 130127132631 Phpapp02

The document describes algorithms for several tasks: 1) Calculating the average and maximum of a data set. 2) Approximating the cosine function using a Taylor series expansion. 3) Finding the real roots of a quadratic equation. 4) Calculating a student's average grade based on quiz, homework, and final exam scores.

Uploaded by

Ashraful A. Khan
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 DOC, PDF, TXT or read online on Scribd
You are on page 1/ 16

1

CHAPTER 2
2.1 Two possible versions can be developed:
IF x 10 THEN DO x = x 5 IF x < 50 EXIT END DO ELSE IF x < 5 THEN x = 5 ELSE x = 7.5 END IF ENDIF IF x 10 THEN DO x = x 5 IF x < 50 EXIT END DO ELSEIF x < 5 x = 5 ELSE x = 7.5 ENDIF

2.2
DO i = i + 1 IF z > 50 EXIT x = x + 5 IF x > 5 THEN y = x ELSE y = 0 ENDIF z = x + y ENDDO

2.3 Note that this algorithm is made simpler by recognizing that concentration cannot by definition be negative. Therefore, the maximum can be initialized as zero at the start of the algorithm. Step 1: Start Step : !nitialize sum, count and maximum to zero Step ": #xamine top card. Step $: !f it says %end of data& proceed to step '( otherwise, proceed to next step. Step ): *dd value from top card to sum. Step +: !ncrease count by 1. Step ,: !f value is greater than maximum, set maximum to value. Step ,: -iscard top card Step .: /eturn to Step ". Step ': !s the count greater than zero0 !f yes, proceed to step 11. !f no, proceed to step 11. Step 11: 2alculate average 3 sum4count Step 11: #nd 2.4 5lowchart:

PROPRIETARY MATERIAL. 6 The 7c8raw9:ill 2ompanies, !nc. *ll rights reserved. No part of this 7anual may be displayed, reproduced or distributed in any form or by any means, without the prior written permission of the publisher, or used beyond the limited distribution to teachers and educators permitted by 7c8raw9:ill for their individual course preparation. !f you are a student using this 7anual, you are using it without permission.

start sum = 0 count = 0 max = 0

input value T

value = end of data " sum = sum value count = count !

count > 0 "

value > max "

average = sum/count

max = value end

2.5 Students could implement the subprogram in any number of languages. The following 5ortran '1 program is one example. !t should be noted that the availability of complex variables in 5ortran '1 would allow this subroutine to be made even more concise. :owever, we did not exploit this feature, in order to ma;e the code more compatible with languages such as <isual =*S!2 or 2.
PROGRAM Rootfind IMPLICIT NONE INTEGER::ier REAL::a, b, c, r1, i1, r2, i2 DATA a,b,c/1., .,2./ CALL Root!"a, b, c, ier, r1, i1, r2, i2# I$ "ier %% &# T'EN PRINT (, r1,i1,) i) PRINT (, r2,i2,) i) EL*E PRINT (, )No root!) END I$ END *+,RO+TINE Root!"a, b, c, ier, r1, i1, r2, i2# IMPLICIT NONE INTEGER::ier REAL::a, b, c, d, r1, i1, r2, i2 r1%&. r2%&.
PROPRIETARY MATERIAL. 6 The 7c8raw9:ill 2ompanies, !nc. *ll rights reserved. No part of this 7anual may be displayed, reproduced or distributed in any form or by any means, without the prior written permission of the publisher, or used beyond the limited distribution to teachers and educators permitted by 7c8raw9:ill for their individual course preparation. !f you are a student using this 7anual, you are using it without permission.

"
i1%&. i2%&. I$ "a %% &.# T'EN I$ "b /% &# T'EN r1 % -c/b EL*E ier % 1 END I$ EL*E d % b((2 - ..(a(c I$ "d /% &# T'EN r1 % "-b 0 *1RT"d##/"2(a# r2 % "-b - *1RT"d##/"2(a# EL*E r1 % -b/"2(a# r2 % r1 i1 % *1RT"A,*"d##/"2(a# i2 % -i1 END I$ END I$ END

The answers for the " test cases are: > a? 1.")$ , ).+$+( >b? 1.$( >c? 1.$1+, @ 1.$+'+i( 1.$1+, 1.$+'+i. Several features of this subroutine bear mention: The subroutine does not involve input or output. /ather, information is passed in and out via the arguments. This is often the preferred style, because the !4A is left to the discretion of the programmer within the calling program. Note that an error code is passed >IER % 1? for the case where no roots are possible. 2.6 The development of the algorithm hinges on recognizing that the series approximation of the cosine can be represented concisely by the summation,

i= 1

> 1? i 1

x i > i ?B

where i 3 the order of the approximation. The following algorithm implements this summation: Step 1: Start Step : !nput value to be evaluated x and maximum order n Step ": Set order >i? eCual to one Step $: Set accumulator for approximation >approx? to zero Step ): Set accumulator for factorial product >factor? eCual to one Step +: 2alculate true value of cos>x? Step ,: !f order is greater than n then proceed to step 1" Atherwise, proceed to next step Step .: 2alculate the approximation with the formula

PROPRIETARY MATERIAL. 6 The 7c8raw9:ill 2ompanies, !nc. *ll rights reserved. No part of this 7anual may be displayed, reproduced or distributed in any form or by any means, without the prior written permission of the publisher, or used beyond the limited distribution to teachers and educators permitted by 7c8raw9:ill for their individual course preparation. !f you are a student using this 7anual, you are using it without permission.

$
x i factor

approx = approx + > 1? i 1

Step ': -etermine the error


Derror = true approx 111D true

Step 11: !ncrement the order by one Step 11: -etermine the factorial for the next iteration factor = factor > i "? > i ? Step 1 : /eturn to step , Step 1": #nd 2.7 (a) Structured flowchart

start input x& n i=! approx = 0 factor = ! trut# = cos$x% T i>n " sum = sum value count = count !
i1

approx = approx + (1)

2i 2

factor

error =

true approx 100 D true

i=i ! factor = factor $2i ' (% $2i ' 2% end


PROPRIETARY MATERIAL. 6 The 7c8raw9:ill 2ompanies, !nc. *ll rights reserved. No part of this 7anual may be displayed, reproduced or distributed in any form or by any means, without the prior written permission of the publisher, or used beyond the limited distribution to teachers and educators permitted by 7c8raw9:ill for their individual course preparation. !f you are a student using this 7anual, you are using it without permission.

(b) Eseudocode:
SUBROUTINE Co !o"#$%&x' i = 1 (##)ox = 0 *(!+o) = 1 +),+- = !o $x' DO IF i > % EXIT (##)ox = (##)ox + $.1'i.1/x0i.0 1 *(!+o) 2))o) = $+),2 . (##)ox' 1 +),2' 3 100 DIS4L56 i& +),2& (##)ox& 2))o) i = i + 1 *(!+o) = *(!+o)/$0/i.7'/$0/i.0' END DO END

2.8 Students could implement the subprogram in any number of languages. The following 7*TF*= 79file is one example. !t should be noted that 7*TF*= allows direct calculation of the factorial through its intrinsic function factoria2. :owever, we did not exploit this feature, in order to ma;e the code more compatible with languages such as <isual =*S!2 and 5ortran.
f3nction co!co45"6,n# i % 17 tr3 % co!"6#7 a55ro6 % &7 f % 17 f5rintf"89n8#7 f5rintf"8order tr3e :a23e a55ro6i4ation error9n8#7 ;<i2e "1# if i / n, brea=, end a55ro6 % a55ro6 0 "-1#>"i - 1# ( 6>"2(i-2# / f7 er % "tr3 - a55ro6# / tr3 ( 1&&7 f5rintf"8?@d ?1..1&f ?1..1&f ?12.Af9n8,i,tr3,a55ro6,er#7 i % i 0 17 f % f("2(i-@#("2(i-2#7 end

:ere is a run of the program showing the output that is generated:


// co!co45"1.2B, # order 1 2 @ . B tr3e :a23e &.@1B@22@ 2. &.@1B@22@ 2. &.@1B@22@ 2. &.@1B@22@ 2. &.@1B@22@ 2. &.@1B@22@ 2. a55ro6i4ation 1.&&&&&&&&&& &.21ACB&&&&& &.@2&.CB2 &. &.@1B1CC& DA &.@[email protected] &.@1B@22@@2@ error -21C.1@BC D@A @&. 2 BB&.B -1. @.1 A2A &.&. &CC.D -&.&&&A&.@C &.&&&&&DBB

PROPRIETARY MATERIAL. 6 The 7c8raw9:ill 2ompanies, !nc. *ll rights reserved. No part of this 7anual may be displayed, reproduced or distributed in any form or by any means, without the prior written permission of the publisher, or used beyond the limited distribution to teachers and educators permitted by 7c8raw9:ill for their individual course preparation. !f you are a student using this 7anual, you are using it without permission.

+ 2.9 (a) The following pseudocode provides an algorithm for this problem. Notice that the input of the Cuizzes and homewor;s is done with logical loops that terminate when the user enters a negative grade:
INPUT WQ, WH, WF nq = 0 sumq = 0 DO INPUT quiz (enter negative to signal en o! quizzes" IF quiz # 0 $%IT nq = nq & ' sumq = sumq & quiz $ND DO (Q = sumq ) nq n* = 0 sum* = 0 DO INPUT *ome+or, (enter negative to signal en o! *ome+or,s" IF *ome+or, # 0 $%IT n* = n* & ' sum* = sum* & *ome+or, $ND DO (H = sum* ) n* DI-P.(/ 0Is t*ere a !inal gra e (1 or n"0 INPUT ans+er IF ans+er = 010 TH$N INPUT F$ (2 = (WQ 3 (Q & WH 3 (H & WF 3 F$" ) (WQ & WH & WF" $.-$ (2 = (WQ 3 (Q & WH 3 (H" ) (WQ & WH" $ND IF DI-P.(/ (2 $ND

(b) Students could implement the program in any number of languages. The following <=* code is one example.
*3b Di4 Di4 Di4 Di4 Di4 Grader"# E1 A! Do3b2e, E' A! Do3b2e, E$ A! Do3b2e nF A! InteGer, !34F A! Do3b2e, A1 A! Do3b2e n< A! InteGer, !34< A! Do3b2e, A' A! Do3b2e an!;er A! *trinG, $E A! Do3b2e AG A! Do3b2e

8enter ;eiG<t! E1 % In53t,o6")enter F3iH ;eiG<t)# E' % In53t,o6")enter <o4e;or= ;eiG<t)# E$ % In53t,o6")enter fina2 e6a4 ;eiG<t)# 8enter F3iH Grade! nF % & !34F % & Do F3iH % In53t,o6")enter neGati:e to !iGna2 end of F3iHHe!)# If F3iH I & T<en E6it Do
PROPRIETARY MATERIAL. 6 The 7c8raw9:ill 2ompanies, !nc. *ll rights reserved. No part of this 7anual may be displayed, reproduced or distributed in any form or by any means, without the prior written permission of the publisher, or used beyond the limited distribution to teachers and educators permitted by 7c8raw9:ill for their individual course preparation. !f you are a student using this 7anual, you are using it without permission.

,
nF % nF 0 1 !34F % !34F 0 F3iH Loo5 A1 % !34F / nF 8enter <o4e;or= Grade! n< % & !34< % & Do <o4e;or= % In53t,o6")enter neGati:e to !iGna2 end of <o4e;or=!)# If <o4e;or= I & T<en E6it Do n< % n< 0 1 !34< % !34< 0 <o4e;or= Loo5 A' % !34< / n< 8deter4ine and di!52aJ t<e a:eraGe Grade an!;er % In53t,o6")I! t<ere a fina2 Grade "J or n#)# If an!;er % )J) T<en $E % In53t,o6")fina2 Grade:)# AG % "E1 ( A1 0 E' ( A' 0 E$ ( $E# / "E1 0 E' 0 E$# E2!e AG % "E1 ( A1 0 E' ( A'# / "E1 0 E'# End If M!G,o6 )A:eraGe Grade % ) K AG End *3b

The results should conform to: *G 3 $",4) 3 .,.$ *: 3 )$14+ 3 '1.1++, without final

*8 3
with final

")>.,.$? @ "1>'1.1++,? = ...+,, ") @ "1 ")>.,.$? @ "1>'1.1++,? + ")>' ? = .'..$ ") @ "1 + ")

*8 3
2.10 (a) Eseudocode:
IF a 4 0 TH$N tol = '056 7 = a)8 DO 1 = (7 & a)7")8 e = (1 5 7")1 7 = 1 IF e # tol $%IT $ND DO -quare9oot = 7 $.-$ -quare9oot = 0

PROPRIETARY MATERIAL. 6 The 7c8raw9:ill 2ompanies, !nc. *ll rights reserved. No part of this 7anual may be displayed, reproduced or distributed in any form or by any means, without the prior written permission of the publisher, or used beyond the limited distribution to teachers and educators permitted by 7c8raw9:ill for their individual course preparation. !f you are a student using this 7anual, you are using it without permission.

.
$ND IF

(b) Students could implement the function in any number of languages. The following <=* and 7*TF*= codes are two possible options.
)*A "unction Procedure O5tion E652icit $3nction *F3areRoot"a# Di4 6 A! Do3b2e, J A! Do3b2e Di4 e A! Do3b2e, to2 A! Do3b2e If a / & T<en to2 % &.&&&&1 6 % a / 2 Do J % "6 0 a / 6# / 2 e % Ab!""J - 6# / J# 6 % J If e I to2 T<en E6it Do Loo5 *F3areRoot % 6 E2!e *F3areRoot % & End If End $3nction +AT,A* +-"ile f3nction ! % *F3areRoot"a# if a / & to2 % &.&&&&17 6 % a / 27 ;<i2e"1# J % "6 0 a / 6# / 27 e % ab!""J - 6# / J#7 6 % J7 if e I to2, brea=, end end ! % 67 e2!e ! % &7 end

2.11 * 7*TF*= 79file can be written to solve this problem as


f3nction f3t3re;ort<"P, i, n# nn % &:n7 $ % P("10i#.>nn7 J % Lnn7$M7 f5rintf"89n Jear f3t3re ;ort<9n8#7 f5rintf"8?Bd ?1..2f9n8,J#7

This function can be used to evaluate the test case,


// f3t3re;ort<"1&&&&&,&.& ,B# Jear & 1 2 @ . B f3t3re ;ort< 1&&&&&.&& 1& &&&.&& 112@ &.&& 11D1&1. & 12 2.C.C& 1@@A22.B

2.12 * 7*TF*= 79file can be written to solve this problem as


f3nction ann3a25aJ4ent"P, i, n# nn % 1:n7 A % P(i("10i#.>nn./""10i#.>nn-1#7 J % Lnn7AM7 f5rintf"89n Jear ann3a2 5aJ4ent9n8#7 f5rintf"8?Bd ?1..2f9n8,J#7
PROPRIETARY MATERIAL. 6 The 7c8raw9:ill 2ompanies, !nc. *ll rights reserved. No part of this 7anual may be displayed, reproduced or distributed in any form or by any means, without the prior written permission of the publisher, or used beyond the limited distribution to teachers and educators permitted by 7c8raw9:ill for their individual course preparation. !f you are a student using this 7anual, you are using it without permission.

'

This function can be used to evaluate the test case,


// ann3a25aJ4ent"BB&&&,&.& Jear 1 2 @ . B ann3a2 5aJ4ent BA @&.&& @&2B1..D 2&A&..A 1 &D1.1C 1@2C&. . ,B#

2.13 Students could implement the function in any number of languages. The following <=* and 7*TF*= codes are two possible options.
)*A "unction Procedure O5tion E652icit $3nction a:Gte45"T4, T5, t!, te# Di4 5i A! Do3b2e, ; A! Do3b2e Di4 Te45 A! Do3b2e, t A! Do3b2e Di4 !34 A! Do3b2e, i A! InteGer Di4 n A! InteGer 5i % . ( Atn"1# ; % 2 ( 5i / @ B !34 % & n % & t % t! $or i % t! To te Te45 % T40"T5-T4#(Co!";("t-2&B## !34 % !34 0 Te45 n % n 0 1 t % t 0 1 Ne6t i a:Gte45 % !34 / n End $3nction +AT,A* +-"ile f3nction Ta % a:Gte45"T4,T5,t!,te# ; % 2(5i/@ B7 t % t!:te7 T % T4 0 "T5-T4#(co!";("t-2&B##7 Ta % 4ean"T#7

The function can be used to evaluate the test cases. The following show the results for 7*TF*=,
// a:Gte45"22.1,2A.@,&,BD# an! % 1 .21.A // a:Gte45"1&.C,22.D,1A&,2.2# an! % 22.2.D1

2.14 The programs are student specific and will be similar to the codes developed for <=*, 7*TF*= and 5ortran as outlined in sections .$, .) and .+. The numerical results for the different time steps are tabulated below along with an estimate of the absolute value of the true relative error at t 3 1 s:
PROPRIETARY MATERIAL. 6 The 7c8raw9:ill 2ompanies, !nc. *ll rights reserved. No part of this 7anual may be displayed, reproduced or distributed in any form or by any means, without the prior written permission of the publisher, or used beyond the limited distribution to teachers and educators permitted by 7c8raw9:ill for their individual course preparation. !f you are a student using this 7anual, you are using it without permission.

11

Step 2 1 0.5

v(12) 49.96 48.70 48.09

t (%) 5.2 2.6 1.3

The general conclusion is that the error is halved when the step size is halved. 2.15 Students could implement the subprogram in any number of languages. The following 5ortran '1 and <=*4#xcel programs are two examples based on the algorithm outlined in 5ig. E .1).
"ortran .0 *3bro3tine ,3bb2e$or"n, b# I452icit None N!ort! an arraJ in a!cendinG Norder 3!inG t<e b3bb2e !ort InteGer".#::4, i, n LoGica2::!;itc< Rea2::a"n#,b"n#,d34 4 % n - 1 Do !;itc< % .$a2!e. Do i % 1, 4 If "b"i# / b"i 0 1## T<en d34 % b"i# b"i# % b"i 0 1# b"i 0 1# % d34 !;itc< % .Tr3e. End If End Do If "!;itc< %% .$a2!e.# E6it 4 % 4 - 1 End Do End )*A/Excel O5tion E652icit *3b ,3bb2e"n, b# 8!ort! an arraJ in a!cendinG 8order 3!inG t<e b3bb2e !ort Di4 Di4 Di4 Di4 4 A! InteGer i A! InteGer !;itc< A! ,oo2ean d34 A! Do3b2e

4 % n - 1 Do !;itc< % $a2!e $or i % 1 To 4 If b"i# / b"i 0 1# T<en d34 % b"i# b"i# % b"i 0 1# b"i 0 1# % d34 !;itc< % Tr3e End If Ne6t i If !;itc< % $a2!e T<en E6it Do 4 % 4 - 1 Loo5 End *3b

5or 7*TF*=, the following 79file implements the bubble sort following the algorithm outlined in 5ig. E .1):
f3nction J % ,3bb2e"6# n % 2enGt<"6#7 4 % n - 17 b % 67 ;<i2e"1# ! % &7 for i % 1:4 if b"i# / b"i 0 1#
PROPRIETARY MATERIAL. 6 The 7c8raw9:ill 2ompanies, !nc. *ll rights reserved. No part of this 7anual may be displayed, reproduced or distributed in any form or by any means, without the prior written permission of the publisher, or used beyond the limited distribution to teachers and educators permitted by 7c8raw9:ill for their individual course preparation. !f you are a student using this 7anual, you are using it without permission.

11
d34 % b"i#7 b"i# % b"i 0 1#7 b"i 0 1# % d347 ! % 17 end end if ! %% &, brea=, end 4 % 4 - 17 end J % b7

Notice how the 2enGt< function allows us to omit the length of the vector in the function argument. :ere is an example 7*TF*= session that invo;es the function to sort a vector: // a%L@ . 2 A B CM7 // ,3bb2e"a# an! % 2 @ . B C A 2.16 :ere is a flowchart for the algorithm:
"unction )ol$R& d%

pi = (/!0!1.(
" T

d2R
T

"

d2(3R

)ol = pi 3 d( / (

)! = pi 3 R( / ( )ol = 45vertop6 )2 = pi 3 R2 $d ' R% )ol = )! )2

End "unction

PROPRIETARY MATERIAL. 6 The 7c8raw9:ill 2ompanies, !nc. *ll rights reserved. No part of this 7anual may be displayed, reproduced or distributed in any form or by any means, without the prior written permission of the publisher, or used beyond the limited distribution to teachers and educators permitted by 7c8raw9:ill for their individual course preparation. !f you are a student using this 7anual, you are using it without permission.

1 Students could implement the function in any number of languages. The following <=* and 7*TF*= codes are two possible options.
)*A "unction Procedure O5tion E652icit $3nction Oo2"R, d# Di4 O1 A! Do3b2e, O2 A! Do3b2e Di4 5i A! Do3b2e 5i % . ( Atn"1# If d I R T<en Oo2 % 5i ( d > @ / @ E2!eIf d I% @ ( R T<en O1 % 5i ( R > @ / @ O2 % 5i ( R > 2 ( "d - R# Oo2 % O1 0 O2 E2!e Oo2 % )o:erto5) End If End $3nction +AT,A* +-"ile f3nction Oo2 % tan=:o234e"R, d# if d I R Oo2 % 5i ( d > @ / @7 e2!eif d I% @ ( R O1 % 5i ( R > @ / @7 O2 % 5i ( R > 2 ( "d - R#7 Oo2 % O1 0 O27 e2!e Oo2 % 8o:erto587 end

The results are:


R 1 1 1 1 0.5 1.2 3 3.1 !o"u#e 0.1309 1.675516 7.330383 o$ertop

2.17 :ere is a flowchart for the algorithm:

PROPRIETARY MATERIAL. 6 The 7c8raw9:ill 2ompanies, !nc. *ll rights reserved. No part of this 7anual may be displayed, reproduced or distributed in any form or by any means, without the prior written permission of the publisher, or used beyond the limited distribution to teachers and educators permitted by 7c8raw9:ill for their individual course preparation. !f you are a student using this 7anual, you are using it without permission.

1"
"unction Polar$x& 7%

= 3.141593
r= x +y
"
x20

" "
7>0

T
x20

"
7>0

y = tan1 x

"
720

"
720

= tan1 y + x

=0

= tan1 y x

Polar

= 1.1

End Polar

Students could implement the function in any number of languages. The following 7*TF*= 79file is one option. <ersions in other languages such as 5ortran '1, <isual =asic, or 2 would have a similar structure.
f3nction 5o2ar"6, J# r % !Frt"6 .> 2 0 J .> 2#7 n % 2enGt<"6#7 for i % 1:n if 6"i# / & t<"i# % atan"J"i# / 6"i##7 e2!eif 6"i# I & if J"i# / & t<"i# % atan"J"i# / 6"i## 0 5i7 e2!eif J"i# I & t<"i# % atan"J"i# / 6"i## - 5i7 e2!e t<"i# % 5i7 end e2!e if J"i# / & t<"i# % 5i / 27 e2!eif J"i# I & t<"i# % -5i / 27 e2!e t<"i# % &7 end end
PROPRIETARY MATERIAL. 6 The 7c8raw9:ill 2ompanies, !nc. *ll rights reserved. No part of this 7anual may be displayed, reproduced or distributed in any form or by any means, without the prior written permission of the publisher, or used beyond the limited distribution to teachers and educators permitted by 7c8raw9:ill for their individual course preparation. !f you are a student using this 7anual, you are using it without permission.

1$
t<"i# % t<"i# ( 1A& / 5i7 end o3%L67J7r7t<M7 f5rintf"89n 6 J radi3! f5rintf"8?A.2f ?A.2f ?1&..f ?1&..f9n8,o3#7

anG2e9n8#7

This function can be used to evaluate the test cases.


// 6%L1 1 1 -1 -1 -1 & & &M7 // J%L1 -1 & 1 -1 & 1 -1 &M7 // 5o2ar"6,J# 6 1.&& 1.&& 1.&& -1.&& -1.&& -1.&& &.&& &.&& &.&& J 1.&& -1.&& &.&& 1.&& -1.&& &.&& 1.&& -1.&& &.&& radi3! 1..1.2 1..1.2 1.&&&& 1..1.2 1..1.2 1.&&&& 1.&&&& 1.&&&& &.&&&& anG2e .B.&&&& -.B.&&&& &.&&&& 1@B.&&&& -1@B.&&&& 1A&.&&&& D&.&&&& -D&.&&&& &.&&&&

2.18 Students could implement the function in any number of languages. The following <=* and 7*TF*= codes are two possible options.
)*A "unction Procedure $3nction Grade"!# If ! /% D& T<en Grade % )A) E2!eIf ! /% A& T<en Grade % ),) E2!eIf ! /% C& T<en Grade % )C) E2!eIf ! /% & T<en Grade % )D) E2!e Grade % )$) End If End $3nction +AT,A* +-"ile f3nction Grade % 2etterGrade"!core# if !core /% D& Grade % 8A87 e2!eif !core /% A& Grade % 8,87 e2!eif !core /% C& Grade % 8C87 e2!eif !core /% & Grade % 8D87 e2!e Grade % 8$87 end

2.19 Students could implement the functions in any number of languages. The following <=* and 7*TF*= codes are two possible options.
)*A "unction Procedure $a% "actorial $3nction factor"n# Di4 6 A! LonG, i A! InteGer 6 % 1 $or i % 1 To n 6 % 6 ( i Ne6t i factor % 6 End $3nction +AT,A* +-"ile f3nction fo3t % factor"n# 6 % 17 for i % 1:n 6 % 6 ( i7 end fo3t % 67

PROPRIETARY MATERIAL. 6 The 7c8raw9:ill 2ompanies, !nc. *ll rights reserved. No part of this 7anual may be displayed, reproduced or distributed in any form or by any means, without the prior written permission of the publisher, or used beyond the limited distribution to teachers and educators permitted by 7c8raw9:ill for their individual course preparation. !f you are a student using this 7anual, you are using it without permission.

1)

$8% +inimum $3nction 4in"6, n# Di4 i A! InteGer 4in % 6"1# $or i % 2 To n If 6"i# I 4in T<en 4in % 6"i# Ne6t i End $3nction $c% Average $3nction 4ean"6, n# Di4 !34 A! Do3b2e Di4 i A! InteGer !34 % 6"1# $or i % 2 To n !34 % !34 0 6"i# Ne6t i 4ean % !34 / n End $3nction

f3nction 64 % 64in"6# n % 2enGt<"6#7 64 % 6"1#7 for i % 2:n if 6"i# I 64, 64 % 6"i#7 end end

f3nction 64 % 64ean"6# n % 2enGt<"6#7 ! % 6"1#7 for i % 2:n ! % ! 0 6"i#7 end 64 % ! / n7

2.20 Students could implement the functions in any number of languages. The following <=* and 7*TF*= codes are two possible options.
)*A "unction Procedure $a% 9:uare root sum of s:uares $3nction ***"6, n, 4# Di4 i A! InteGer, P A! InteGer *** % & $or i % 1 To n $or P % 1 To 4 *** % *** 0 6"i, P# > 2 Ne6t P Ne6t i *** % *Fr"***# End $3nction $8% ;ormali<ation *3b nor4a2"6, n, 4, J# Di4 i A! InteGer, P A! InteGer Di4 4a6 A! Do3b2e $or i % 1 To n 4a6 % Ab!"6"i, 1## $or P % 2 To 4 If Ab!"6"i, P## / 4a6 T<en 4a6 % 6"i, P# End If Ne6t P $or P % 1 To 4 J"i, P# % 6"i, P# / 4a6 Ne6t P Ne6t i End *3b +AT,A* +-"ile f3nction ! % ***"6# Ln,4M % !iHe"6#7 ! % &7 for i % 1:n for P % 1:4 ! % ! 0 6"i, P# > 27 end end ! % !Frt"!#7

f3nction J % nor4a2"6# Ln,4M % !iHe"6#7 for i % 1:n 46 % ab!"6"i, 1##7 for P % 2:4 if ab!"6"i, P## / 46 46 % 6"i, P#7 end end for P % 1:4 J"i, P# % 6"i, P# / 467 end end A2ternate :er!ion: f3nction J % nor4a2"6#

PROPRIETARY MATERIAL. 6 The 7c8raw9:ill 2ompanies, !nc. *ll rights reserved. No part of this 7anual may be displayed, reproduced or distributed in any form or by any means, without the prior written permission of the publisher, or used beyond the limited distribution to teachers and educators permitted by 7c8raw9:ill for their individual course preparation. !f you are a student using this 7anual, you are using it without permission.

1+
n % !iHe"6#7 for i % 1:n J"i,:# % 6"i,:#/4a6"6"i,:##7 end

PROPRIETARY MATERIAL. 6 The 7c8raw9:ill 2ompanies, !nc. *ll rights reserved. No part of this 7anual may be displayed, reproduced or distributed in any form or by any means, without the prior written permission of the publisher, or used beyond the limited distribution to teachers and educators permitted by 7c8raw9:ill for their individual course preparation. !f you are a student using this 7anual, you are using it without permission.

You might also like