版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認領(lǐng)
文檔簡介
1、PE 2-1/* Programming Exercise 2-1 */ #include int main(void) printf(Gustav Mahlern); printf(GustavnMahlern); printf(Gustav ); printf(Mahlern); return 0;PE2-3/* Programming Exercise 2-3 */ #include int main(void) int ageyears; /* age in years */ int agedays; /* age in days */* large ages may require
2、the long type */ ageyears = 101; agedays = 365 * ageyears;printf(An age of %d years is %d days.n, ageyears, agedays); return 0; PE2-4/* Programming Exercise 2-4 */#include void jolly(void); void deny(void); int main(void) jolly(); jolly(); jolly(); deny(); return 0; void jolly(void)printf(For hes a
3、jolly good fellow!n);void deny(void) printf(Which nobody can deny!n);PE2-6/* Programming Exercise 2-6 */#include int main(void) int toes;toes = 10; printf(toes = %dn, toes);printf(Twice toes = %dn, 2 * toes); printf(toes squared= %dn, toes * toes); return 0;/* or create two more variables, set them
4、to 2 * toes and toes * toes */PE 2-8/* Programming Exercise 2-8 */ #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(onen); two();printf(threen); void two(void) printf(twon);Chapter 3 Programming Exer
5、cisesPE 3-2/* Programming Exercise 3-2 */ #include int main(void) int ascii;printf(Enter an ASCII code: ); scanf(%d, &ascii);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: ); s
6、canf(%f, &num); printf(fixed-point notation: %fn, num); printf(exponential notation: %en, num); printf(p notation: %an, num); return 0;PE 3-6/* Programming Exercise 3-6 */ #include int main(void) float mass_mol = ; /* mass of water molecule in grams */ float mass_qt = 950; /* mass of quart of water
7、in grams */ float quarts; float molecules;printf(Enter the number of quarts of water: ); scanf(%f, &quarts); molecules = quarts * mass_qt / mass_mol;printf(%f quarts of water contain %e molecules.n, quarts, molecules); return 0; Chapter 4 Programming ExercisesPE 4-1/* Programming Exercise 4-1 */ #in
8、clude int main(void) char fname40; char lname40;printf(Enter your first name: ); scanf(%s, fname); printf(Enter your last name: ); scanf(%s, lname); printf(%s, %sn, lname, fname); return 0;PE 4-4/* Programming Exercise 4-4 */ #include int main(void) float height; char name40;printf(Enter your height
9、 in inches: ); scanf(%f,&height); printf(Enter your name: ); scanf(%s, name);printf(%s, you are %.3f feet talln, name, height / ; return 0;PE4-7/* Programming Exercise 4-7 */#include #include int main(void) float ot_f = / ; double ot_d =/ ; printf( float values: );printf(%.4f %.12f %.16fn, ot_f, ot_
10、f, ot_f); printf(double values: );printf(%.4f %.12f %.16fn, ot_d, ot_d, ot_d); printf(FLT_DIG: %dn, FLT_DIG); printf(DBL_DIG: %dn, DBL_DIG); return 0;Chapter 5 Programming ExercisesPE5-1/* Programming Exercise 5-1 */ #include int main(void) const int minperhour = 60; int minutes, hours, mins;printf(
11、Enter the number of minutes to convert: ); scanf(%d,&minutes); while (minutes 0 ) hours = minutes / minperhour;mins = minutes % minperhour;printf(%d minutes = %d hours, %d minutesn, minutes, hours, mins); printf(Enter next minutes value (0 to quit): ); scanf(%d, &minutes);printf(Byen); return 0;PE5-
12、3/* Programming Exercise 5-3 */ #include int main(void) const int daysperweek = 7; intdays, weeks, day_rem; printf(Enter the number of days: );scanf(%d, &days); while (days 0) weeks = days / daysperweek;day_rem = days % daysperweek; printf(%d days are %d weeks and %d days.n, days,weeks, day_rem); pr
13、intf(Enter the number of days (0 or less to end): ); scanf(%d, &days);printf(Done!n); 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,&n); count = 0; sum = 0; while (count+ n) sum
14、 = sum + count; printf(sum = %dn, sum); return 0;PE5-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, &val); showCube(val); return 0; void showCube(double x)printf(The cube
15、of %e is %e.n, x, x*x*x );ExercisesChapter 6 ProgrammingPE6-1/* */* this implementation assumes the character codes */ /* are sequential, as they are in ASCII. */ #include #define SIZE 26 int main( void ) char lcaseSIZE; int i;for (i = 0; i SIZE; i+) lcasei= a + i; for (i = 0; i SIZE; i+) printf(%c,
16、 lcasei); printf(n);return 0;PE6-3/* */* this implementation assumes the character codes */ /* are sequential, as they are in ASCII. */ #include int main( void ) char let = F; charstart; char end;for (end = let; end = A; end-)for (start = let; start = end; start-) printf(%c, start); printf(n); retur
17、n0;PE6-6/* */ #include intmain( void ) int lower, upper, index; intsquare, cube;printf(Enter starting integer: ); scanf(%d, &lower); printf(Enter ending integer: ); scanf(%d, &upper);printf(%5s %10s %15sn, num, square, cube); for (index = lower; index = upper; index+) square = index * index;cube = i
18、ndex * square;printf(%5d %10d %15dn, index, square, cube);return 0;PE 6-8/* */ #include int main( void ) double n, m; double res;printf(Enter a pair of numbers: );while (scanf(%lf %lf, &n, &m) = 2)res = (n - m) / (n * m);printf(%.3g - %.3g)/(%.3g*%.3g) = %.5gn, n, m, n, m, res); printf(Enter next pa
19、ir (non-numeric to quit): );return 0;PE 6- 11/* */#include #define SIZE 8int main( void ) int valsSIZE; int i;printf(Please enter %d integers.n, SIZE); for (i = 0;i = 0; i-)printf(%d , valsi); printf(n);return 0;PE 6- 13/* */* This version starts with the 0 power */ #include #define SIZE 8 int main(
20、 void )int twopowsSIZE; int i;int value = 1; /* 2 to the 0 */for (i = 0; i SIZE; i+) twopowsi = value;value *= 2; i =0; do printf(%d , twopowsi);i+; while (i SIZE); printf(n); return 0;PE 6- 14/* */ /* Programming Exercise 6-14 */ #include #define SIZE 8 int main(void) double arrSIZE; double arr_cum
21、ulSIZE; int i;printf(Enter %d numbers:n, SIZE);for (i = 0; i SIZE; i+) printf(value #%d: , i + 1); scanf(%lf, &arri); /* or scanf(%lf, arr + i); */ arr_cumul0 = arr0; /* set first element */ for (i = 1; i SIZE; i+)arr_cumuli = arr_cumuli-1 + arri;for (i = 0; i SIZE; i+) printf(%8g , arri); printf(n)
22、; for (i = 0; i SIZE; i+) printf(%8g , arr_cumuli); printf(n); return 0;PE 6- 16/* */ #include #define RATE_SIMP #define RATE_COMP #define INIT_AMT int main( void )double daphne = INIT_AMT; double deidre = INIT_AMT; int years = 0;while (deidre = daphne) daphne += RATE_SIMP * INIT_AMT; deidre += RATE
23、_COMP * deidre;+years; printf(Investment values after %d years:n, years); printf(Daphne: $%.2fn, daphne); printf(Deidre: $%.2fn, deidre); return 0;Chapter 7 Programming Exercises PE7-1/* Programming Exercise 7-1 */ #include int main(void) char ch; int sp_ct = 0; int nl_ct = 0; int other = 0;while (c
24、h = getchar() != #) if (ch = ) sp_ct+;else if (ch = n)nl_ct+;elseother+;printf(spaces: %d, newlines: %d, others: %dn, sp_ct, nl_ct, other); return 0;PE 7-3/* Programming Exercise 7-3 */ #include int main(void) int n; double sumeven = ;int ct_even = 0; double sumodd= ; int ct_odd = 0;while (scanf(%d,
25、 &n) = 1 & n != 0)if (n % 2 = 0)sumeven += n;+ct_even;else : putchar(!);+ct1; break;case ! : putchar(!);putchar(!); +ct2; break; default : putchar(ch); printf(%d replacement(s) of . with !n, ct1); printf(%d replacement(s) of ! with !n, ct2);return 0;PE7-7f; taxes: $%.2f; net: $%.2fn, gross, taxes, n
26、et);return 0;PE7-9/* Programming Exercise 7-9 */ #include #include int main(void)int limit; int num; int div;bool numIsPrime; n); for (num = 2; num =limit; num+)for (div = 2, numIsPrime = true; (div * div) = num; div+) if (num % div = 0)numIsPrime = false; if (numIsPrime)printf(%d is prime.n, num);p
27、rintf(Enter a positive integer (q to quit): );printf(Done!n);return 0;PE7- 11/* */* Programming Exercise 7-11 */#include #include int main(void)const double price_artichokes = ; const double price_beets = ; const double price_carrots = ;const double DISCOUNT_RATE = ; const double under5= ; const dou
28、ble under20 = ; const double base20= ; const double extralb = ;char ch;double lb_artichokes = 0; doublelb_beets = 0; double lb_carrots = 0; double lb_temp; double lb_total;double cost_artichokes; double cost_beets; double cost_carrots;double cost_total; double final_total; double discount;double shi
29、pping;printf(Enter a to buy artichokes, b for beets, ); printf(c for carrots, q to quit: ); while (ch = getchar() != q & ch != Q) if (ch = n)continue; while (getchar() != n)continue; ch = tolower(ch);switch (ch) case a : printf(Enter pounds of artichokes: );scanf(%lf, &lb_temp);lb_artichokes += lb_t
30、emp;break;case b : printf(Enter pounds of beets: ); scanf(%lf, &lb_temp); lb_beets += lb_temp; break;case c : printf(Enter pounds of carrots: ); scanf(%lf, &lb_temp); lb_carrots += lb_temp; break;default : printf(%c is not a valid choice.n, ch);printf(Enter a to buy artichokes, b for beets, ); print
31、f(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 = ;
32、 else if (lb_total shipping = under5; else if (lb_total discount = DISCOUNT_RATE * cost_total; else discount = ;final_total = cost_total + shipping - discount; printf(Your order:n); printf(%.2f lbs of artichokes at $%.2f per pound:$ %.2fn, lb_artichokes, price_artichokes, cost_artichokes); printf(%.
33、2f lbs of beets at $%.2f per pound: $%.2fn,lb_beets, price_beets, cost_beets);printf(%.2f lbs of carrots at $%.2f per pound: $%.2fn, lb_carrots, price_carrots, cost_carrots); printf(Total cost of vegetables: $%.2fn, cost_total); if (cost_total 100)printf(Volume discount: $%.2fn, discount); printf(Sh
34、ipping: $%.2fn, shipping); printf(Total charges: $%.2fn, final_total); return 0; Chapter 8 Programming ExercisesPE 8-1/* Programming Exercise 8-1 */ #include int main(void) int ch; int ct = 0; while (ch = getchar() != EOF) ct+;printf(%d characters readn, ct);return 0;PE8-3/* Programming Exercise 8-3
35、 */* Using eliminates need to assume consecutive coding */ #include #include int main(void) int ch; unsigned long uct = 0; unsigned long lct = 0; unsigned long oct = 0;while (ch = getchar() != EOF) if (isupper(ch) uct+; else if (islower(ch) lct+; else oct+;printf(%lu uppercase characters readn, uct)
36、; printf(%lu lowercase characters readn, lct); printf(%lu other characters readn, oct);return 0;/* or you could use if (ch = A &ch = a & ch = z) lct+;else oct+;*/PE8-5/* Programming Exercise 8-5 */* - an improved number-guesser */* but relies upon truthful, correct responses */ #include #include int
37、 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);w
38、hile (response = getchar() != y) /* get response */if (response = n) continue;if (response != h & response != l)printf(I dont understand that response. Please enter h forn); printf(high, l for low, or y for correct.n); continue;if (response = h) high = guess - 1; else if (response = l)low = guess +
39、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 #include #define BASEPAY1 */ switch (response)case a: pay = BASEPAY1; break; case b: pay = BASEPAY2; break; case c: pay = BASEPAY3; break;
40、 case d: pay = BASEPAY4; break;default : printf(Please enter a, b, c, d, or q.n); menu();continue; f; taxes: $%.2f; net: $%.2fn, gross, taxes, net); menu(); printf(Done.n);return 0;void menu(void)printf( * *n);printf(Enter the letter corresponding to the desired pay rate or action:n);b) $%hrn, BASEP
41、AY1,d) $%hrn, BASEPAY3,BASEPAY2);BASEPAY4);printf(a) $%hrprintf(c) $%hrprintf(q) quitn);printf( *n);int getfirst(void) int ch;ch = getchar(); while (isspace(ch) ch = getchar(); while (getchar() != n)continue; return ch;Chapter 9PE9-1Programming Exercises/* Programming Exercise 9-1 */#include double
42、min(double, double); int main(void)double x, y; printf(Enter two numbers (q to quit): ); while (scanf(%lf %lf, &x, &y) = 2) printf(The smaller number is %f.n, min(x,y); printf(Next two values (q to quit): );printf(Bye!n);return 0;double min(double a, double b)return a b ? a : b;/* alternative implem
43、entation 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)continue;printf(Enter
44、 number of columns and number of rows: ); if (scanf(%d %d, &col, &row) != 2) break; chLineRow(ch, col, row);printf(nEnter next character (# to quit): );printf(Bye!n);return 0;n, x, y); printf(Next two values (q to quit): );printf(Bye!n);return 0;void larger_of(double *p1, double *p2) if (*p1 *p2)*p2
45、 = *p1; else*p1 = *p2;Enter q); printf( to quit.n); while (scanf(%lf%d, &x, &n) = 2) xpow = power(x,n); /* function call */printf(%.3g to the power %d is %.5gn, 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(do
46、uble 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 valuen); pow = ; else if (a = 0) pow = ; else if (b 0) for(i = 1; i = b; i+) pow *= a; else /* b 0 */ pow = / power(a, - b);return pow; /* return the value of pow */ PE 9- 1
47、0/* Programming Exercise 9-10 */ #include void to_base_n(int x, int base); int main(void) int number; int b; int count; printf(Enter an integer (q to quit):n); while (scanf(%d, &number) = 1) printf(Enter number base (2-10): ); while (count = scanf(%d, &b)= 1& (b 10) printf(base should be in the rang
48、e 2-10: ); if (count != 1) break;printf(Base %d equivalent: , b); to_base_n(number, b); putchar(n);printf(Enter an integer (q to quit):n); printf(Done.n); return0; void to_base_n(int x, int base) /* recursive function */ int r; r = x % base; if (x = base) to_base_n(x / base, base); putchar(0 + r); r
49、eturn;Chapter 10 Programming ExercisesPE10-1/* Programming Exercise 10-1 */ #include #define MONTHS 12 f inches.nn, total/YRS); printf(MONTHLY AVERAGES:nn); printf( Jan Feb Mar Apr May Jun Jul Aug Sep Oct ); printf( Nov Decn); for (month = 0; month MONTHS; month+) /* for each month, sum rainfall ove
50、r years */ for (year =0, subtot =0; year YRS; year+) subtot += *(*(rain + year) + month); printf(% , subtot/YRS); printf(n); return 0; PE10-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 origLEN = 1,2,3,
51、4,12,6,7,8,9,10; int max; show_arr(orig, LEN); max = max_arr(orig, LEN); printf(%d = largest valuen, max);return 0; int max_arr(const int ar, int n) int i; int max = ar0;/* dont use 0 as initial max value - fails if all array values are neg */for (i = 1; i n; i+) if(max ari) max = ari;return max;void show_arr(const int ar, int n) int i; for (i = 0; i n;i+) printf(%d , ari); putchar(n);PE 10-5/* Programming Exercise 10-5 */ #include #define LEN 10 double max_diff(const double ar, int n); void show_ar
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責。
- 6. 下載文件中如有侵權(quán)或不適當內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 蘇州站施工組織設(shè)計方案(幕墻)
- 二零二五年度金融行業(yè)IT運維安全保障協(xié)議3篇
- 專業(yè)化海路物流合作合同(2024版)版B版
- 2025年度環(huán)保建筑材料推廣合作框架協(xié)議4篇
- 2025年度購物中心場地合作開發(fā)及商業(yè)運營合同4篇
- 二零二四圖書購置項目與圖書館無障礙閱讀服務(wù)合同3篇
- 2025年度智能攤位管理系統(tǒng)開發(fā)與實施合同4篇
- 2025年度劇本創(chuàng)作與版權(quán)授權(quán)管理合同3篇
- 二零二五版4S店汽車銷售合同樣本圖2篇
- 2025年度農(nóng)產(chǎn)品質(zhì)量安全追溯體系服務(wù)合同4篇
- 衡水市出租車駕駛員從業(yè)資格區(qū)域科目考試題庫(全真題庫)
- 護理安全用氧培訓課件
- 《三國演義》中人物性格探析研究性課題報告
- 注冊電氣工程師公共基礎(chǔ)高數(shù)輔導課件
- 土方勞務(wù)分包合同中鐵十一局
- 乳腺導管原位癌
- 冷庫管道應急預案
- 司法考試必背大全(涵蓋所有法律考點)
- 公共部分裝修工程 施工組織設(shè)計
- 《學習教育重要論述》考試復習題庫(共250余題)
- 裝飾裝修施工及擔保合同
評論
0/150
提交評論