南昌大學c程序設計雙語作業(yè)與答案_第1頁
南昌大學c程序設計雙語作業(yè)與答案_第2頁
南昌大學c程序設計雙語作業(yè)與答案_第3頁
南昌大學c程序設計雙語作業(yè)與答案_第4頁
南昌大學c程序設計雙語作業(yè)與答案_第5頁
已閱讀5頁,還剩22頁未讀, 繼續(xù)免費閱讀

下載本文檔

版權說明:本文檔由用戶提供并上傳,收益歸屬內容提供方,若內容存在侵權,請進行舉報或認領

文檔簡介

1、Chapter 1 Introductionidentifiers?Circlethe illegal1、1. Which of the following are illegal identifiers.2、1. Which of the following identifiers.A. xyshouldI B. me_to-2 教師批改: B C DA.3id B._yes C.star*it D.int 教師批改: A C Dare illegal identifiers?Circle the illegalC. one_i_aren t D. 2_i_amChapter 2 Learn

2、ing by Example1、What is correct about the following program?#include #define count 6void main(void)count = count + 1 ; printf(%d, count) ;A. 7 is output and count becomes 7B 7 is output and countbecomes 7C. Runtime ErrorD. 7 is output but count remains 6教師批改: C2、Indicate which of the following are l

3、egal variable names in C:A 、 X B 、formula1 C 、average_rainfall D、%correct教師批改: ABC 3、Indicate which of the following are legal variable names in C:A 、short B 、tiny C 、total rainfall D、aReasonablyLongVariableName教師批改: BD 4、Indicate which of the following are legal variable names in C:A 、 12MonthTotal

4、 B 、marginal-cost C 、b4hand D 、 _stk_depth 教師批改: CD5、Indicate which of the following are legal variable names in C:A 、short B 、 4formula C 、average_rainfall D 、 %correct 教師批改: C6、Indicate which of the following are legal variable names in C: A 、short B 、 formula_5 C 、 average_rainfall D 、4correct 教師

5、批改: BC7、Indicate which of the following are legal variable names in C:A 、 short B 、formula6 C.float D.printf 教師批改: BD8、Consider the following code fragment:int dblarray10,*dblPointer;Which of the following statements are valid (i.e. which ones will compile)? Circle all the correct answers (there may

6、 be more than one correct answer).A. dblPointer = dblArray; B. dblPointer = dblArray4;C. dblPointer = &(dblArray2); D. dblPointer = *dblArray; 教師批改: AC9 、 Indicate the values and types of the following expressions:2+3 value:type:教師批改: 5 integer10 、 Indicate the values and types of the following expr

7、essions:19/5 value: type:教師批改: 3 integer11、 Indicate the values and types of the following expressions:19.0/5 value:type:教師批改: 3.8 double12、 Indicate the values and types of the following expressions:3*6.0 value:type:教師批改: 18.0 double13 、 Indicate the values and types of the following expressions:19

8、%5 value:type:教師批改: 4 integer14、 Indicate the values and types of the following expressions:2%7 value:type:教師批改: 4 integerthe result ofthe result of is 15、 By applying the appropriate precedence rules,calculate the following expression:6+5/4-3,result is 教師批改: 416、By applying the appropriate preceden

9、ce rules,calculate the following expression:10+9*(8+7)%6)+5*4%3*2+1,result教師批改: 4217、By applying the appropriate precedence rules,calculate the result of the following expression:1+2+(3+4)*(5*6%7*8)-9)-10,result is教師批改: 4218、Rewrite the following floating-point constants in Cs form forscientific not

10、ation:29979250000.0教師批改: 2997925000019、Rewrite the following floating-point constants in Cs form forscientific notation:0.00000000529167教師批改: 0.0000000052920、Evaluate the following expression: (2 Points)int i=1,j=2,k=3,m=4;i+=j+k; / i=j*=k=m+5; / j=教師批改: 6 1821、The variables are initialized as follo

11、ws,char c= A;int i=7,j=7;double x=0.0,y=2.3;Evaluate the following expressions: (5 Points)!c !(i-j)!i-j!(x+y)!x*!y教師批改: 0 1 -7 1 122、Indicate the values and types of the following expressions:2%7 value:type:教師批改: 2 integer23、By applying the appropriate precedence rules,calculate the result of the fo

12、llowing expression:2+2*(2*2-2)%2/2,result is 教師批改: 224、The following C program is compiled and runs successfully. Write the output the following program produces.#include void main( )int k = 42;printf(%dn, k+);printf(%dn, +k);return 0;教師批改: 42 4425、Writea program that reads in a list of integers unt

13、il the user entersthe value -1 as a sentinel.At that point,the program should display the average of the values entered so far.Chapter 3 Problem Solving1、 What is the output of the following code?#include void main(void)int a ;a = 1;printf(%i,+a) ;IIA. Compile-time Error B. 0 C.1 D. 2教師批改: D2、Use #d

14、efine to introduce a constant named pi with the value 3.14159教師批改: #define pi 3.141593、Write a printf statement to display the floating-point value storedin the variable distance so that exactly three digits appear to the right of the decimal point教師批改: printf(%.3f,distance);4、Use for control line t

15、o count from 15 to 25教師批改: for(i=15;i=25;i+)5、Writea program that displays the message Hello,world. 10 times onseparate lines.教師批改: main() int k;for(k=1;k=10;k+)printf(Hello,world.n);6、Write a program that prints a tabular version of both the squares and cubes of the number from 1 to 10.Chapter 4 St

16、atement Forms1、Consider the following program: #include void main() int a=1,b=2,c=3,d=0;if(a=1 & b+=2) if(b!=2|c-!=3)printf( “%d,%d,%dn”,a,b,c); else printf( “%dn”,a,b,c);else printf( “%d,%d,%dn”,a,b,c); The output of the program is:A)1,2,3 B)1,3,2C)1,3,3 D)3,2,1 教師批改: C2、Consider the program fragme

17、nt and give the output:For (i=0;i4;i+,i+)for(k=1;k k) & (i + j) = k) b) (i+j) != k) | (j*j) k) & (i*i) k) c) k (j i) 教師批改: 0 1 0the4、The following C program is compiled and runs successfully. Write output that this program produces.#include main()int x = 4;int y = 4;if ( (x = y) )printf( “Bingo ”);e

18、lseprintf( “No go” );教師批改: Bingo5、What will be printed to the screen if we execute the code? #include void main( )int index = 0;if(index = 0)printf(A);if(index = 1)printf(B);教師批改: B6、What arethe values ofthe following statement,assuming that i,j,andk are declaredas integervariables:i=(j=4)*(k=16)i=j

19、=,k=教師批改: 64,4,167、Write a for loop control line to count from 1 to 100教師批改: for(k=1;k=0;k=k-2)9、The following C program compiles and runs without errors. Write the output of this program. #include void main( ) int i = -1;int j = 1;if( 0 i | i i) printf(A); else printf(B);else printf(C);教師批改: A10、Wh

20、at is the output of the following code?#include void main() int day_of_week = 4; switch (day_of_week) case 0: printf(today is Sunday.n);case 1: printf(today is Monday.n);case 2: printf(today is Tuesday.n);case 3: printf(today is Wednesday.n);case 4: printf(today is Thursday.n);case 5: printf(today i

21、s Friday.n);case 6: printf(today is Saturday.n);default:printf(Something is missing.);break;/*/教師批改: today is Thursdaytoday is Friday.Today is Saturday.Something is missing.11、The following C program is compiled and runs successfully. Write the output the following program produces.#include #define

22、NRows 5void main()int i, j;for (i = 1; i = NRows; i+) for (j = i; j NRows; j+) printf( );for (j = 0; j 2 * i - 1; j+) printf(*);printf(n);教師批改:*12、The following C program is compiled and runs successfully. output the following program produces.#include void main()int a66,i,j; for(i=1;i6;i+)for(j=1;j

23、6;j+) aij=(i/j)*(j/i);for(i=1;i6;i+) for(j=1;j=0;i-=3) printf(%d,i-1);/*/教師批改: 963014、How many lines of output does the following program print? #include void main( ) int i,j;for(i=0;i5;i+) for(j=0;j5;j+);printf(Hello world!n); 教師批改: 115、Complete the program below to produce the following output:1 x

24、 12 = 12Write thecompiled2 x 12 = 243 x 12 = 3610 x 12 = 12011 x 12 = 13212 x 12 = 144#include void main(void) int i;for( ; ; )printf(%d x 12 = %dn, , );教師批改: i=1 i12 i+ i*12 n16、The following C program is compiled and runs successfully. Write the output the following program produces.#define LowerL

25、imit 1#define UpperLimit 4#includevoid main()int i;printf(Number Square Cuben);for (i = LowerLimit; i = UpperLimit; i+) printf( %2d %3d %4dn, i, i * i, i * i * i); 教師批改: 1 1 1、 2 4 8、3 9 2717、While we re on the subject of silly songs,another old standby is This Old Man for which the first verse is T

26、his old man,he played 1.He played knick-knack on my thumb.With a knick-knack,paddy-whack,Give your dog a bone.This old man come rolling home.Each subsequent verse is the same,except for the number and the rhyming word at the end of the second line,which gets replaced as follows: 2-shoe 3-knee 4-door

27、 5-hive 6-sticks 7-heaven 8-pate 9-spine 10-shinChapter 5 Function1、Consider the following program:#include int f(int x,int y)return (y-x)*x);void main() int a=3,b=4,c=5,d;d=f(f(a,b),f(a,c); printf(%dn,d);A)10 B)9 C)8 D)7 教師批改: B2、The following program compiles and executes without errors. Write the

28、 output of this program. (8 Points)#include int foo(int x) x+;return x;int bar(int y) y-; return y; void main() int x = 1;int y = 3;foo(x);bar(y); printf(%d %dn,x,y);y = foo(x);x = bar(y); printf(%d %dn,x,y);教師批改: 1 31 23、The following program compiles and executes without errors. Write the output o

29、f this program.#include int count1(int k) int x = 1;x = x + k;printf(%d ,x);return x;int count2(int k) static int x = 1;x = x + k;printf(%d,x);return x;void main( ) count1(count1(1);count2(count2(2);教師批改: 2 33 64、The following C program is compiled and runs successfully. Write the output the followi

30、ng program fun(int x, int y, int z) z=x*x+y*y; void main( )int a=31;fun(5,2,a); printf(%d,a);/*/教師批改: 315、The following program compiles and executes without errors. Write the output of this program.#include int pfft(int x)if (x=0)return 0;/* Hint: % gives the remainder, example 5%2 is

31、1 */if( (x % 2) != 0 )return (1 + pfft(x - 1);elsereturn (3 + pfft(x - 1);void main()printf(%d n,pfft(7);教師批改: 13 6、The following C program is compiled and runs successfully. Write the(=p l)eq po(0 l)004 po j onpod poo 40 02d 6u50=04 -?-ndlnoroLIM / lCXIL 0宀O- p&y )=d 二&)三二0殳二一) luDUapo 宀 -?)ua 套)ua

32、po ()elu poAlloplsv pnoutt s onpod lue60d 6u50=04 一| 二 nHnooeeCXIL 9L OL寸CCXIL9L0L 仝直舔二匸 q -oq -Ee -oe -A -X -=5p& p& p& p& p& p&=)匕 d =e)eq二匸 q -oq -Ee -oe -A -X -=5p& p& p& p& p& p&=)匕 d-3004二匸 q -oq -Ee -oe -A -X -=5p& p& p& p& p& p&=)匕 ds)eq二匸 q -oq -Ee -oe -A -X -=5p& p& p& p& p& p&=)匕 d_(oe)00

33、4 亠寸aHaq -u 一 -宀CXI-匸 H pco-u 一InL H A_u 一OL Hxlu 一(po)elu po 宀oe丄匸p10 15 1 2 3 3010 15 1 30 3 308、The following C program is compiled and runs successfully. Write the output that this program produces.#includevoid main() int a=1,b=2,c=3;a+=b+=+c;printf(%5d%5d%5dn,a,b,c);/(1)float b=4.0;int c;a+=c=5

34、*b;printf(%5d%5.1f%5dn,a,b,c);/(2)printf(%5d%5d%5dn,a,b,c);(3)教師批改: (1)7 6 4 (2)27 4.0 20 (3)27 6 49、Write a function Fib(n) to calculate the nth Fibonacci number.Chapter 11 Arrays1、It is an array declaration:int a3;Which one is invalid to refer to particular members of arrays :A a0 B a1*2 C a4-2 D

35、a3教師批改: D2、Assumewe have declared the two dimensional array A in the C language, int A32 = 0, 1, 2, 3, 4, 5 ;Write the value of A21.教師批改: 53、This program computes the average of all the values of an array named list . Assume that the count of the number of values is 100 or fewer. #include #define AR

36、RAY_SIZE 100main( )int listARRAY_SIZE;int count = 0;double total = 0.0;while( scanf(%d, &list count )=1)total =;count =;printf(average = %lf n,);教師批改: total=total+listcount;count=count+1;total/count4、This program reads in an array of integers, reverses the elements of the array, and then displays th

37、e elements in their reversed order. complete the program.#define MaxElement 5#include void input(int a,int n);void reverse(int a,int n);void output(int a,int n);void main() int i,arrayMaxElement;input (array,MaxElement);reverse(array,MaxElement);output (array,MaxElement);void input(int a,int n)int i

38、;for(i=0;in;i+)printf(array%d=?n,i);scanf(%d,&ai);/* This function reverses the elements of array,which has n effective size.*/void reverse(int a,int n)int i,temp;for(i=0; ;i+) Pleaseas itsvoid output(int a,int n) int i; for(i=0;in;i+) printf(array%d=%dn,i,ai);教師批改: in/2 temp=ai; ai=an-i-1; an-i-1=t

39、emp;5、The code fragment (below) has an error. Changeone character to remove the total15;int factor115;int factor215;int i;for (i = 0; i = 15; i+)totali = factor1i + factor2i + factor2i/2;教師批改: for (i=0;i15;i+)即去掉 =, 引用數(shù)組元素越界6、The following C program is compiled and runs successfully. Write

40、 the output the following program produces.main( )int aa44=1,2,3,4,5,6,7,8,3,9,10,2,4,2,9,6;int i,s=0; for(i=0;i4;i+) s+=aai1;printf( “%dn”,s);教師批改: 197、The following C program is compiled and runs successfully. Write the output the following program produces.void f(int arr, int len);void main( )int

41、 j;int a3 = 1, 2, 3;f(a, 3); for(j=0;j3;+j) printf( %d n,aj);void f(int arr, int len)int i;for(i=0; ilen; i+)arri+;教師批改: 2 3 48、This function goes through the first n elements of the array, and removes any elements whose value is zero. The remaining elements are shifted to fill up the cells formerly

42、 occupied by the zero values, so that the final result is that the array contains only the non-zero values in its initial RemoveZeroElements(int array , int n)int lh, rh; lh = 0;for (rh = 0; rh n; rh+) if (arrayrh ) arraylh = array ; J 教師批改: !=0 rh lh+10Write a program toalculatethe val

43、ue of the first twenty items (N=20) ofFibonacci list follow the formul:a(1)=a(2)=1 a(n)=a(n-1)+a(n-2) Chapter 13 Pointers1、Consider the following program:#include void fun(int *a,int *b) int *c;c=a;a=b;b=c; void main() int x=3,y=5,*p=&x,*q=&y; fun(p,q);printf(%d,%d,*p,*q);fun(&x,&y);printf(%d,%dn,*p

44、,*q);The output of the program is A)3,5,5,3 B)3,5,3,5 C)5,3,3,5 D)5,3,5, 教師批改: B2、1.What s the result with the following program?( )#includevoid main()char *p= “abcdefgh ”;printf( “%dn”,strlen(strcpy(p+3,”ABCD”);puts(p);A)8 B)12 C)4 D)7 教師批改:3、1.What s the result with the following program?( ) sub(i

45、nt x,int y,int *z) *z=y-x; void main() int a,b,c;sub(10,5,&a); sub(7,a,&b); sub(a,b,&c);printf( “%4d,%4d,%4dn” ,a,b,c);A)5,2,3 B)-5,-12,-7 C)-5,-12,-17 D)5,-2,-7 教師批改: B4、#include void f(int *p,int *q); void main()int m=1,n=2,*r=&m; f(r,&n);printf(%d,%d,m,n);void f(int *p,int *q)p=p+1; *q=*q+1;A)1,3

46、 B)2,3 C)1,4 D)1,2 教師批改: A5、What s the output with the following program? #include void f(int *p,int *q);void main()int m=1,n=2,*r=&m; f(r,&n);printf(%d,%d,m,n);void f(int *p,int *q)p=p+1;*q=*q+1;A)1,3 B)2,3 C)1,4 D)1,2 教師批改: A6、Consider the following code fragment: int x = 5;int *y = &x;int *z;(*y)

47、+;z = y;printf(%d, *z);What is the output? 教師批改: 67、The program below compiles without errors and executes correctly. Write the output of this program.(6 Points)#include void foo(int *a) *a += 5;void bar(int b) b1 = 15;main( ) int a3 = 3, 4, 5;int b3 = 3, 4, 5;int *p2;p2 = &a0;(*p2)+;bar(p2);printf(

48、%d %d %d n, a0,a1,a2);p2 = &b1;foo(p2);bar(p2);printf(%d %d %d n, b0,b1,b2);II教師批改: 4 15 5 3 9 15II8、Declarations and initializations as following:int i=3,j=5,*p=&i,*q=&j,*r;double x;Evaluate the following expressions:p=&i/(1)*&p /(2)r=&x /(3)7*p/ * q+7 /(4)*(r=&j)*=*p/(5)教師批改: (1) 1(2) 3 (3) illega

49、l (4) 11 (5) 159、#include void try_me(int 3);int main(void)int a33 = 2, 5, 7, 0, -1, -2, 7, 9, 3;try_me(a);return 0;void try_me(int (*a)3) printf(%d %d %d %d . . . infinityn, a10, -a11, a00, a22);教師批改:0 1 2 3infinity10、 The program below compiles without errors and executes correctly. Write the outp

50、ut of this program.(8 Points)#include void swap1(int a, int b) int tmp = a; a = b; b = tmp;void swap2(int a) int tmp = a0; a0 = a1; a1 = tmp;void swap3(int *a, int *b) int tmp = *a;*a = *b;*b = tmp;void tripleswap(i nt *a, int *b, int *c) int tmp = *a;*a = *b;*c = tmp;*b = *c;mai n()int a2;a0 = 0;a1 = 1;swap1( a0, a1);prin tf(%d, %dn, a0, a1); swap2( a );prin tf(%d, %dn, a0, a1); swap3( & a0, & a1);prin tf(%d, %dn, a0, a1); a0 = 0;a1 = 1;tripl

溫馨提示

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

評論

0/150

提交評論