Homework - C programming language

  • Published on
    27-May-2015

  • View
    1.744

  • Download
    6

Transcript

  • 1. LSGroup (tinhocbk123.blogspot.com)1 CC THNH PHN C BN CA NGN NG LP TRNH C Ch thch, nh danh, t kha, hng Cc thnh phn c bn trong chng trnh C Kiu d liu: char, int, long, float, double, sizeof Lnh nhp, xut, chui nh dng Bi 1.1 p kiuCh ra nhng nh danh no l ng, nh danh no l sai di y? 1 identifier; 6 2by2; 2 serven_11; 7 default; 3 _unique; 8 average_weight_of_a_large_pizza; 4 gross-income; 9 variable; 5 gross$income; 10 object.oriented; Bi 1.2Vit chng trnh in ra dng sau: Ngon ngu lap trinh C phan biet chu hoa, chu thuong Moi chuong trinh luon co mot va chi mot ham main. Ham main se la noi thuc hien dau tien cua chuong trinh Moi khi co mot dau mo ngoac thi phai co mot dau dong ngoac tuong ung Tat ca cac cau lenh deu ket thuc bang dau cham phayBi 1.3Cho on chng trnh sau: 1 2 3 4 5 6 7 8 9 10 11 12#include #include #define MAX 25 int main() { int sum; sum = (MAX - 4) /4; printf("Sum = %d", sum); printf("n"); system("pause"); return 0; }Thc hin ln lt cc yu cu sau, v ghi li kt qu chng trnh B cu lnh s 9 B du > dng 1 B cu lnh s 10 B d lib dng 2 B du m ngoc dng 5 Sa ch sum -> SUM B du ng ngoc dng 12 B du nhy i sau %d dng s 8152

2. Ngn ng lp trnh C B du ; dng 6 B du , sau %d dng th 8Bi 1.4Nhp t bn phm 1 s nguyn x, 1 s thc y, sau in ra mn hnh theo qui cch sau: Dng 1: in ra s nguyn canh l phi vi 5 khong trng Dng 2: in ra s nguyn canh l tri vi 5 khong trng Dng 3: in ra s nguyn canh phi vi 3 khong trng Dng 4: in s thc lm trn 2 s thp phn Dng 5: in s thc vi 6 khong trng, canh l phi, lm trn 3 ch s thp phn Dng 6: in s thc 6 khong trng, canh l tri, lm trn 2 ch s thp phn Bi 1.5Bit nhit C v F c cng thc lin h sau: C = (F-32). Vit chng trnh cho php nhp vo C v hin th ln F tng ng. Bi 1.6Nhp vo s thc x, tnh v in ra cc gi tr y1, y2 ly 2 s l: sin( = = 4( + 10 + 3 + 1) Bi 1.7 Bi 1.8)++ cos(4+1 )Vit chng trnh nhp vo mt k t, hin th m ASCII ca k t . Vit chng trnh nhp vo nm sinh ca mt ngi, tnh tui ca ngi .2 KIU D LIU DATA TYPES S hc: +, -, *, /, % So snh: >, >=, 0). S dng hm trn kt lun loi tam gic Bi 5.3Vit chng trnh nh p vo s 0. Cho bit: S ch s ca n, V d: 21432 tr v: 5 v c 5 ch s. Tm s o ca n (S o ca s 2564 l s 4652). S n c phi l s i xng (Palindrome) hay khng ? (S 121, 1331 l s i xng)157 7. LSGroup (tinhocbk123.blogspot.com) S n c phi l s hon ho (Perfect) hay khng? (S hon ho l cc s nguyn dng bng tng tt c cc c tht s ca n 6 = 1 + 2 + 3, 28 = 1 + 2 + 4 + 7 + 14). Bi 5.4Vit chng trnh in ra cc s chnh phng trong khong t 100 n 500. Cho bit c bao nhiu s chnh phng trong khong ny. Bi 5.5Vit chng trnh nhp s 2 s a, b tha iu kin 0 C bao nhiu s nguyn t thuc [a, b]. In ra cc s nguyn t trong khong ny. Bi 5.6 .c s chung ln nht (USCLN) c nh ngha nh sau Bi 5.7Vit hm tm USCLN ca 2 s nguyn dng a, b Mt khch hng gi 1.000.000$ vo mt ti khon tit kim ti ngn hng vi li sut mt nm l 11%. Sau 10 nm, ti khon tr thnh bao nhiu? Bi 5.8Gi s mt loi vi trng s pht trin gp 3 sau mi gi. Lp cng thc qui cho s vi trng sau n gi. Nu 100 con vi trng ban u, th sau 10 gi, chng s pht trin thnh bao nhiu.6 MNG ARRAYS Nhp, xut mng Tm kim, m, hon i v tr Thay th, chn, xa Tch, gp mng Ma trn chuyn v Bi 6.1 ng cho chnh, ng cho phVit chng trnh nh p n (5n20), th c hin cc vic sau: Nhp mng A gm n phn t. Lit k cc s chn ca mng A. m c bao nhiu phn t m v tnh tng cc phn t m, v gi tr trung binh cc phn t trong mng A.158 8. Ngn ng lp trnh C Tnh v in ra mn hnh tng v trung bnh cng ca tt c cc s dng trong on [5;50] trong mng A. Tnh v in ra mn hnh tng v trung bnh cng ca tt c cc s c gi tr tuyt i ln hn 20 trong mng. In ra mn hnh gi tr ca trung v ca N s. Trung v l phn t c mt na dy nh hn n v mt na dy ln hn n. Nu khng c phn t no trong dy tha mn tnh cht trn th trung v bng trung bnh cng 2 s gia dy. Bi 6.2Nhp ngu nhin mng A gm n (10n20) phn t cc gi tr trong (100, 500) Hin th gi tr ca cc phn t trong mng. Tm gi tr nh nht v cho bit c bao nhiu s nh th. Tm gi tr ln nht trong mng v v tr cui cng ca gi tr ln nht. Nhp vo mt s nguyn x, cho bit trong mng c bao nhiu s c gi tr bng x Nhp mt s x. Cho bit v tr u tin ca s x c trong mng. Nhp 2 s nguyn dng x v vt (0vtn). Chn s x vo mng A v tr vt. Bi 6.3Nhp ngu nhin mng A gm n (100n200) phn t cc s nguyn >0. In ra mn hnh cc gi tr phn bit trong n s ny v s ln xut hin ca gi tr . Xa cc s trng nhau trong mng A. Tm v in gi tr ca phn t Max, Min v v tr ca chng trong mng, sau i ch 2 phn t ny cho nhau. In ra dy sau khi i ch. To mng B t A ch bao gm cc s nguyn t. Thay th cc phn t l s nguyn t ca mng A bng s 0. To mng C t A ch bao gm cc s chnh phng. Thay cc phn t chnh phng ca mng A bng s -1. To mng D gm cc phn t ca mng B v C. Bi 6.4Vit chng trnh thc hin: Nhp vo mt ma trn m x n cc s nguyn. Hin th ma trn va nhp A. Ma trn M c phi l ma trn tha khng? (Ma trn tha l ma trn c phn ln cc phn t c gi tr 0). Tm v hin th ma trn chuyn v ca ma trn M To mt ma trn c mx4 ct. Mi hng cha ch s ct ca gi tr ln nht, gi tr ln nht, ch s ct ca gi tr nh nht v gi tr nh nht. Bi 6.5Vit chng trnh thc hin: Nhp ngu nhin ma trn M(nxn) vung kch tht n (n>0 nhp t bn phm). Xut M Ma trn M c bao nhiu s chn, bao nhiu s l.159 9. LSGroup (tinhocbk123.blogspot.com) To mng A gm n phn t cha cc gi tr trung bnh ca mi hng. Xut A To mng B gm 6 phn t sau: tng cc gi tr cc phn t thuc ng cho chnh v ng cho ph, cc phn t trn v di, trn ng cho chnh v ph. Xut B. Bi 6.6Vit chng trnh thc hin: Nhp m,n, k >0. Nhp ngu nhin ma trn M1(mxn) v M2(nxk). Xut M1, M2 Tnh v in ra ma trn M3 = M1xM2. To ma trn M4(i,j) l ma trn c cc phn t nu M1(i,j)= M2(j, j), bng 1 nu M1 khc To mng B gm cc phn t c mt ma trn M1 v M2. To mng C gm cc phn t hp ca ma trn M1 hoc M2. Bi 7.17 XU K T - STRINGVit chng trnh nhp vo mt chui k t v thc hin cc vic sau: Cho bit trong chui c bao nhiu k t l nguyn m, bao nhiu k t l ph m, bao nhiu k t l ch s, bao nhiu k t loi khc Xa k t (c nhp t bn phm) c trong chui. i nhng k t u tin ca mi t thnh ch in hoa Thay th cc ch ci u on v u cu thnh k t ch hoa tng ng vi n. Bi 7.2Vit chng trnh th c hin cc yu cu: Nhp vo mt chui vn bn khng qu 70 k t. Xut ra mn hnh chui o ngc ca chui . V d o ca abcd egh l hge dcba. Kim tra xem chui c i xng khng. V d : Chui ABCDEDCBA l chui i xng. Thay tt c cc k t b trong chui bng k t B, in chui sau khi thay th Tm s ln xut hin ca k t a trong chui. Chun ha chui (xa b cc k t trng tha, k t u on v u cu phi vit hoa), cho bit chui c bao nhiu t. Bi 7.3Vit chng trnh th c hin cc yu cu Nhp vo mt chui S khng qu 100 k t, hin th chui va nhp Cho bit trong chui c bao nhiu k t l nguyn m, bao nhiu k t l ph m (theo bng ch ci latin), bao nhiu k t l ch s, bao nhiu k t loi khc Thay th cc ch ci thng v tr u tin v cc ch ci thng sau du chm v 1du cch bng ch ci in tng ng ca n, in chui mi ra mn hnh160 10. Bi 7.4Ngn ng lp trnh C Nhp vo mt chui l h v tn ca mt ngi, xut chui ra mn hnh di dng mi t mt dng. a chui v dng danh t ring (dng chun), cho bit chui c bao nhiu t. Ct chui h tn thnh chui h lt v tn. V d: Nguyn Vn A ct thnh chui h lt l Nguyn Vn, chui tn l A.Sp xp danh sch tn theo th t t in. In danh sch h v tn sau khi s p xp.8 CU TRC - STRUCTUREBi 8.1 Struct, union,..im ca sinh vin c qun l nh sau: M sinh vin Tn sinh vin (xu k t di 20 k t) im Ton, im L, im Ha (kiu nguyn) nh ngha cu trc (struct) DiemSV lu tr cc thng tin v im thi i hc sinh vin nh m t trn. Gi s d liu im sinh vin nh sau: M SV Tn Ton L Ha SV01 Nam 6 7 9 SV02 Lien 4 6 5 SV03 Thuong 9 9 8 SV04 Luc 3 2 7 S dng mng v struct lu cc d liu trn Hin th bng im ca sinh vin v im trung bnh. Vit hm cho php thm sinh vin v im. Hin th cu thng tin nh sau:Sinh vien co ma xxx, ten la xxx co diem trung binh xxx la cao nhat. Xp loi hc lc cho sinh vin theo qui nh sau: im trung bnh Xp loi Yu