C Primer Plus(第5版)習(xí)題答案.pdf_第1頁(yè)
C Primer Plus(第5版)習(xí)題答案.pdf_第2頁(yè)
C Primer Plus(第5版)習(xí)題答案.pdf_第3頁(yè)
C Primer Plus(第5版)習(xí)題答案.pdf_第4頁(yè)
C Primer Plus(第5版)習(xí)題答案.pdf_第5頁(yè)
已閱讀5頁(yè),還剩81頁(yè)未讀, 繼續(xù)免費(fèi)閱讀

C Primer Plus(第5版)習(xí)題答案.pdf.pdf 免費(fèi)下載

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡(jiǎn)介

Answers For Programming Exercises in C Primer Plus 5rd Edition by Stephen Prata Chapter 2 PE 2 1 Programming Exercise 2 1 include int main void printf Anton Bruckner n printf Anton nBruckner n printf Anton printf Bruckner n return 0 PE 2 3 Programming Exercise 2 3 include int main void int ageyears age in years int agedays age in days large ages may require the long type ageyears 44 agedays 365 ageyears printf An age of d years is d days n ageyears agedays return 0 PE 2 4 Programming Exercise 2 4 include void jolly void void deny void int main void jolly jolly jolly deny sp Page 1 of 86 August 21 1999 Answers For Programming Exercises in C Primer Plus 5rd Edition by Stephen Prata return 0 void jolly void printf For he s a jolly good fellow n void deny void printf Which nobody can deny n PE 2 5 Programming Exercise 2 5 include int main void int toes toes 10 printf toes d n toes printf Twice toes d n 2 toes printf toes squared d n toes toes return 0 or create two more variables set them to 2 toes and toes toes PE 2 7 Programming Exercise 2 7 include void one three void void two void int main void printf starting now n one three printf done n return 0 void one three void printf one n two printf three n sp Page 2 of 86 August 21 1999 Answers For Programming Exercises in C Primer Plus 5rd Edition by Stephen Prata void two void printf two n Chapter 3 PE 3 2 Programming Exercise 3 2 include int main void int ascii printf Enter an ASCII code scanf d printf d is the ASCII code for c n ascii ascii return 0 PE 3 4 Programming Exercise 3 4 include int main void float num printf Enter a floating point value scanf f printf fixed point notation f n num printf exponential notation e n num return 0 PE 3 6 Programming Exercise 3 6 include int main void sp Page 3 of 86 August 21 1999 Answers For Programming Exercises in C Primer Plus 5rd Edition by Stephen Prata float mass mol 3 0e 23 mass of water molecule in grams float mass qt 950 mass of quart of water in grams float quarts float molecules printf Enter the number of quarts of water scanf f molecules quarts mass qt mass mol printf f quarts of water contain e molecules n quarts molecules return 0 Chapter 4 PE 4 1 Programming Exercise 4 1 include int main void char fname 40 char lname 40 printf Enter your first name scanf s fname printf Enter your last name scanf s lname printf s s n lname fname return 0 PE 4 4 Programming Exercise 4 4 include int main void float height char name 40 printf Enter your height in inches scanf f printf Enter your name scanf s name printf s you are 3f feet tall n name height 12 0 return 0 sp Page 4 of 86 August 21 1999 Answers For Programming Exercises in C Primer Plus 5rd Edition by Stephen Prata PE 4 6 Programming Exercise 4 6 include include int main void float ot f 1 0 3 0 double ot d 1 0 3 0 printf float values printf 4f 12f 16f n ot f ot f ot f printf double values printf 4f 12f 16f n ot d ot d ot d printf FLT DIG d n FLT DIG printf DBL DIG d n DBL DIG return 0 Chapter 5 PE 5 1 Programming Exercise 5 1 include int main void const int minperhour 60 int minutes hours mins printf Enter the number of minutes to convert scanf d while minutes 0 hours minutes minperhour mins minutes minperhour printf d minutes d hours d minutes n minutes hours mins printf Enter next minutes value 0 to quit scanf d printf Bye n return 0 sp Page 5 of 86 August 21 1999 Answers For Programming Exercises in C Primer Plus 5rd Edition by Stephen Prata PE 5 3 Programming Exercise 5 3 include int main void const int daysperweek 7 int days weeks day rem printf Enter the number of days scanf d weeks days daysperweek day rem days daysperweek printf d days are d weeks and d days n days weeks day rem return 0 PE 5 5 Programming Exercise 5 5 include int main void finds sum of first n integers int count sum int n printf Enter the upper limit scanf d count 0 sum 0 while count n sum sum count printf sum d n sum return 0 PE 5 7 Programming Exercise 5 7 include void showCube double x int main void finds cube of entered number double val printf Enter a floating point value scanf lf showCube val return 0 sp Page 6 of 86 August 21 1999 Answers For Programming Exercises in C Primer Plus 5rd Edition by Stephen Prata void showCube double x printf The cube of e is e n x x x x Chapter 6 PE 6 1 pe6 1 c this implementation assumes the character codes are sequential as they are in ASCII include define SIZE 26 int main void char lcase SIZE int i for i 0 i SIZE i lcase i a i for i 0 i SIZE i printf c lcase i printf n return 0 PE 6 3 pe6 3 c this implementation assumes the character codes are sequential as they are in ASCII include int main void char let F char start char end for end let end A end for start let start end start printf c start printf n return 0 sp Page 7 of 86 August 21 1999 Answers For Programming Exercises in C Primer Plus 5rd Edition by Stephen Prata PE 6 5 pe6 5 c include int main void int lower upper index int square cube printf Enter starting integer scanf d printf Enter ending integer scanf d printf 5s 10s 15s n num square cube for index lower index upper index square index index cube index square printf 5d 10d 15d n index square cube return 0 PE 6 7 pe6 7 c include int main void double n m double res printf Enter a pair of numbers while scanf lf lf printf 3g 3g 3g 3g 5g n n m n m res printf Enter next pair non numeric to quit return 0 PE 6 10 pe6 10 c include sp Page 8 of 86 August 21 1999 Answers For Programming Exercises in C Primer Plus 5rd Edition by Stephen Prata define SIZE 8 int main void int vals SIZE int i printf Please enter d integers n SIZE for i 0 i 0 i printf d vals i printf n return 0 PE 6 12 pe6 12 c This version starts with the 0 power include define SIZE 8 int main void int twopows SIZE int i int value 1 2 to the 0 for i 0 i SIZE i twopows i value value 2 i 0 do printf d twopows i i while i SIZE printf n return 0 PE 6 13 pe 13 c Programming Exercise 6 13 include define SIZE 8 sp Page 9 of 86 August 21 1999 Answers For Programming Exercises in C Primer Plus 5rd Edition by Stephen Prata int main void double arr SIZE double arr cumul SIZE int i printf Enter d numbers n SIZE for i 0 i SIZE i printf value d i 1 scanf lf or scanf lf arr i arr cumul 0 arr 0 set first element for i 1 i SIZE i arr cumul i arr cumul i 1 arr i for i 0 i SIZE i printf 8g arr i printf n for i 0 i SIZE i printf 8g arr cumul i printf n return 0 PE 6 15 pe6 15 c include define RATE SIMP 0 10 define RATE COMP 0 05 define INIT AMT 100 0 int main void double daphne INIT AMT double deidre INIT AMT int years 0 while deidre daphne daphne RATE SIMP INIT AMT deidre RATE COMP deidre years printf Investment values after d years n years printf Daphne 2f n daphne printf Deidre 2f n deidre return 0 sp Page 10 of 86 August 21 1999 Answers For Programming Exercises in C Primer Plus 5rd Edition by Stephen Prata Chapter 7 PE 7 1 Programming Exercise 7 1 include int main void char ch int sp ct 0 int nl ct 0 int other 0 while ch getchar if ch sp ct else if ch n nl ct else other printf spaces d newlines d others d n sp ct nl ct other return 0 PE 7 3 Programming Exercise 7 3 include int main void int n double sumeven 0 0 int ct even 0 double sumodd 0 0 int ct odd 0 while scanf d ct odd else sumeven n ct even sp Page 11 of 86 August 21 1999 Answers For Programming Exercises in C Primer Plus 5rd Edition by Stephen Prata printf Number of evens d ct even if ct even 0 printf average g sumeven ct even putchar n printf Number of odds d ct odd if ct odd 0 printf average g sumodd ct odd putchar n printf ndone n return 0 PE 7 5 Programming Exercise 7 5 include int main void char ch int ct1 0 int ct2 0 while ch getchar switch ch case putchar ct1 break case putchar putchar ct2 break default putchar ch printf d replacements of with n ct1 printf d replacements of with n ct2 return 0 PE 7 7 Programming Exercise 7 7 include sp Page 12 of 86 August 21 1999 Answers For Programming Exercises in C Primer Plus 5rd Edition by Stephen Prata define BASEPAY 10 10 per hour define BASEHRS 40 hours at basepay define OVERTIME 1 5 1 5 time define AMT1 300 1st rate tier define AMT2 150 2st rate tier define RATE1 0 15 rate for 1st tier define RATE2 0 20 rate for 2nd tier define RATE3 0 25 rate for 3rd tier int main void double hours double gross double net double taxes printf Enter the number of hours worked this week scanf lf if hours BASEHRS gross hours BASEPAY else gross BASEHRS BASEPAY hours BASEHRS BASEPAY OVERTIME if gross AMT1 taxes gross RATE1 else if gross AMT1 AMT2 taxes AMT1 RATE1 gross AMT1 RATE2 else taxes AMT1 RATE1 AMT2 RATE2 gross AMT1 AMT2 RATE3 net gross taxes printf gross 2f taxes 2f net 2f n gross taxes net return 0 PE 7 9 Programmming Exercise 7 9 include define NO 0 define YES 1 int main void long num value to be checked long div potential divisors long lim limit to values int prime printf Please enter limit to values to be checked printf Enter q to quit n while scanf ld num lim num for div 2 prime YES div div num div if num div 0 prime NO number is not prime if prime YES printf ld is prime n num sp Page 13 of 86 August 21 1999 Answers For Programming Exercises in C Primer Plus 5rd Edition by Stephen Prata printf Please enter another limit printf Enter q to quit n return 0 PE 7 11 pe7 11 c Programming Exercise 7 11 include include int main void const double price artichokes 1 25 const double price beets 0 65 const double price carrots 0 89 const double DISCOUNT RATE 0 05 char ch double lb artichokes double lb beets double lb carrots double lb total double cost artichokes double cost beets double cost carrots double cost total double final total double discount double shipping printf Enter a to buy artichokes b for beets printf c for carrots q to quit while ch getchar q while getchar n continue ch tolower ch switch ch case a printf Enter pounds of artichokes scanf lf break case b printf Enter pounds of beets scanf lf break case c printf Enter pounds of carrots scanf lf break default printf c is not a valid choice n sp Page 14 of 86 August 21 1999 Answers For Programming Exercises in C Primer Plus 5rd Edition by Stephen Prata printf Enter a to buy artichokes b for beets printf c for carrots q to quit cost artichokes price artichokes lb artichokes cost beets price beets lb beets cost carrots price carrots lb carrots cost total cost artichokes cost beets cost carrots lb total lb artichokes lb beets lb carrots if lb total 0 shipping 0 0 else if lb total 5 0 shipping 3 50 else if lb total 100 0 discount DISCOUNT RATE cost total else discount 0 0 final total cost total shipping discount printf Your order n printf 2f lbs of artichokes at 2f per pound 2f n lb artichokes price artichokes cost artichokes printf 2f lbs of beets at 2f per pound 2f n lb beets price beets cost beets printf 2f lbs of carrots at 2f per pound 2f n lb carrots price carrots cost carrots printf Total cost of vegetables 2f n cost total if cost total 100 printf Volume discount 2f n discount printf Shipping 2f n shipping printf Total charges 2f n final total return 0 Chapter 8 PE 8 1 Programming Exercise 8 1 include int main void int ch int ct 0 while ch getchar EOF ct printf d characters read n ct sp Page 15 of 86 August 21 1999 Answers For Programming Exercises in C Primer Plus 5rd Edition by Stephen Prata return 0 PE 8 3 Programming Exercise 8 3 Using ctype h eliminates need to assume ASCII coding include include int main void int ch int uct 0 int lct 0 while ch getchar EOF if isupper ch uct else if islower ch lct printf d uppercase characters read n uct printf d lowercase characters read n lct return 0 or you could use if ch A PE 8 5 Programming Exercise 8 5 binaryguess c an improved number guesser include include int main void int high 100 int low 1 int guess high low 2 char response printf Pick an integer from 1 to 100 I will try to guess printf it nRespond with a y if my guess is right with printf na h if it is high and with an l if it is low n printf Uh is your number d n guess while response getchar y get response if response n sp Page 16 of 86 August 21 1999 Answers For Programming Exercises in C Primer Plus 5rd Edition by Stephen Prata continue if response h printf high l for low or y for correct n continue if response h high guess 1 else if response l low guess 1 guess high low 2 printf Well then is it d n guess printf I knew I could do it n return 0 PE 8 7 Programming Exercise 8 7 include include define BASEPAY1 8 75 8 75 per hour define BASEPAY2 9 33 9 33 per hour define BASEPAY3 10 00 10 00 per hour define BASEPAY4 11 20 11 20 per hour define BASEHRS 40 hours at basepay define OVERTIME 1 5 1 5 time define AMT1 300 1st rate tier define AMT2 150 2st rate tier define RATE1 0 15 rate for 1st tier define RATE2 0 20 rate for 2nd tier define RATE3 0 25 rate for 3rd tier int getfirst void void menu void int main void double hours double gross double net double taxes double pay char response menu while response getfirst q if response n skip over newlines continue response tolower response accept A as a etc switch response sp Page 17 of 86 August 21 1999 Answers For Programming Exercises in C Primer Plus 5rd Edition by Stephen Prata case a pay BASEPAY1 break case b pay BASEPAY2 break case c pay BASEPAY3 break case d pay BASEPAY4 break default printf Please enter a b c d or q n menu continue go to beginning of loop printf Enter the number of hours worked this week scanf lf if hours BASEHRS gross hours pay else gross BASEHRS pay hours BASEHRS pay OVERTIME if gross AMT1 taxes gross RATE1 else if gross AMT1 AMT2 taxes AMT1 RATE1 gross AMT1 RATE2 else taxes AMT1 RATE1 AMT2 RATE2 gross AMT1 AMT2 RATE3 net gross taxes printf gross 2f taxes 2f net 2f n gross taxes net menu printf Done n return 0 void menu void printf n printf Enter the number corresponding to the desired pay rate or action n printf a 4 2f hr b 4 2f hr n BASEPAY1 BASEPAY2 printf c 5 2f hr d 5 2f hr n BASEPAY3 BASEPAY4 printf q quit n printf n int getfirst void int ch ch getchar while isspace ch ch getchar while getchar n continue return ch sp Page 18 of 86 August 21 1999 Answers For Programming Exercises in C Primer Plus 5rd Edition by Stephen Prata Chapter 9 PE 9 1 Programming Exercise 9 1 include double min double a double b int main void double x y printf Enter two numbers q to quit while scanf lf lf printf Next two values q to quit printf Bye n return 0 double min double a double b return a b a b alternative implementation double min double a double b if a b return a else return b PE 9 3 Programming Exercise 9 3 include void chLineRow char ch int c int r int main void char ch int col row printf Enter a character to quit while ch getchar if ch n sp Page 19 of 86 August 21 1999 Answers For Programming Exercises in C Primer Plus 5rd Edition by Stephen Prata continue printf Enter number of columns and number of rows if scanf d d chLineRow ch col row printf nEnter next character to quit printf Bye n return 0 void chLineRow char ch int c int r int col row for row 0 row r row for col 0 col c col putchar ch putchar n return PE 9 5 Programming Exercise 9 5 include void larger of double p1 double p2 int main void double x y printf Enter two numbers q to quit while scanf lf lf printf The modified values are f and f n x y printf Next two values q to quit printf Bye n return 0 void larger of double p1 double p2 double temp p1 p2 p1 p2 p1 p2 temp PE 9 7 sp Page 20 of 86 August 21 1999 Answers For Programming Exercises in C Primer Plus 5rd Edition by Stephen Prata Programming Exercise 9 7 include double power double a int b ANSI prototype int main void double x xpow int n printf Enter a number and the integer power printf to which nthe number will be raised Enter q printf to quit n while scanf lf d function call printf 3g to the power d is 5g n x n xpow printf Enter next pair of numbers or q to quit n printf Hope you enjoyed this power trip bye n return 0 double power double a int b function definition double pow 1 int i if b 0 if a 0 printf 0 to the 0 undefined using 1 as the value n pow 1 0 else if a 0 pow 0 0 else if b 0 for i 1 i b i pow a else b 0 pow 1 0 power a b return pow return the value of pow PE 9 9 Programming Exercise 9 9 include void to base n int x int base int main void int number int b printf Enter an integer q to quit n while scanf d scanf d printf Base d equivalent b to base n number b putchar n printf Enter an integer q to quit n return 0 void to base n int x int base recursive function int r r x base if x 2 to base n x base base putchar 0 r return Chapter 10 PE 10 1 Programming Exercise 10 1 include define MONTHS 12 number of months in a year define YRS 5 number of years of data int main void initializing rainfall data for 1990 1994 const float rain YRS MONTHS 10 2 8 1 6 8 4 2 2 1 1 8 0 2 0 3 1 1 2 3 6 1 7 4 9 2 9 8 4 4 3 3 2 2 0 8 0 4 0 0 0 6 1 7 4 3 5 2 6 6 5 5 3 8 2 8 1 6 0 2 0 0 0 0 0 0 1 3 2 6 4 2 4 3 4 3 4 3 3 0 2 0 1 0 0 2 0 2 0 4 2 4 3 5 6 6 8 5 8 2 1 2 1 6 2 4 0 0 5 2 0 9 0 3 0 9 1 4 7 2 int year month float subtot total printf YEAR RAINFALL inches n for year 0 total 0 year YRS year for each year sum rainfall for each month for month 0 subtot 0 month MONTHS month subtot rain year month printf 5d 15 1f n 1990 year subtot total subtot total for all years printf nThe yearly average is 1f inches n n total YRS printf MONTHLY AVERAGES n n printf Jan Feb Mar Apr May Jun Jul Aug Sep Oct printf Nov Dec n sp Page 22 of 86 August 21 1999 Answers For Programming Exercises in C Primer Plus 5rd Edition by Stephen Prata for month 0 month MONTHS month for each month sum rainfall over years for year 0 subtot 0 year YRS year subtot rain year month printf 4 1f subtot YRS printf n return 0 PE 10 3 Programming Exercise 10 3 include define LEN 10 int max arr const int ar int n void show arr const int ar int n int main void int orig LEN 1 2 3 4 12 6 7 8 9 10 int max show arr orig LEN max max arr orig LEN printf d largest value n max return 0 int max arr const int ar int n int i int max ar 0 don t use 0 as initial max value fails if all array val

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫(kù)網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。

最新文檔

評(píng)論

0/150

提交評(píng)論