南昌大学C程序设计(双语)-作业与答案南昌大学C程序设计(双语)-作业与答案
Chapter 1 Introduction
1、1. Which of the following are illegal identifiers?Circle the illegal identifiers.
A.3id B._yes C.star*it D.int
教师批改:A C D
2、1. Which of the following are illegal identifiers?Circle the illegal identifiers.
A. xyshou...
南昌大学C程序
(双语)-作业与
Chapter 1 Introduction
1、1. Which of the following are illegal identifiers?Circle the illegal identifiers.
A.3id B._yes C.star*it D.int
教师批改:A C D
2、1. Which of the following are illegal identifiers?Circle the illegal identifiers.
A. xyshouldI B. me_to-2 C. one_i_aren’’t D. 2_i_am
教师批改:B C D
Chapter 2 Learning by Example
1、What is correct about the following program?
#include
#define count 6
void main(void)
{
count = count + 1 ;
printf("%d", count) ;
}
A. ‘7‘ is output and count becomes 7 B ‘7‘ is output and count
becomes 7
C. Runtime Error D. ‘7‘ is output but count remains 6
教师批改:C
2、Indicate which of the following are legal 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 B、marginal-cost C、b4hand D、_stk_depth
教师批改:CD
5、Indicate which of the following are legal variable names in C:
A、short B、4formula C、average_rainfall D、%correct
教师批改:C
6、Indicate which of the following are legal variable names in C:
A、short B、formula_5 C、average_rainfall D、4correct
教师批改:BC
7、Indicate which of the following are legal variable names in C:
A、short B、formula6 C.float D.printf
教师批改:BD
8、"Consider the following code fragment:
int dblarray[10],*dblPointer;
Which of the following statements are valid (i.e. which ones will compile)? Circle all the correct answers (there may be more than one correct answer)."
A. dblPointer = dblArray; B. dblPointer = dblArray[4];
C. dblPointer = &(dblArray[2]); D. dblPointer = *dblArray;
教师批改:AC
9、Indicate the values and types of the following expressions:2+3 value:_______type:_______
教师批改:5 integer
10、Indicate the values and types of the following expressions:19/5 value:_______ type:_______
教师批改:3 integer
11、Indicate the values and types of the following expressions:19.0/5 value:_______type:_______
教师批改:3.8 double
12、Indicate the values and types of the following expressions:3*6.0 value:_______type:_______
教师批改:18.0 double
13、Indicate the values and types of the following expressions:19%5 value:_______type:______
教师批改:4 integer
14、Indicate the values and types of the following expressions:2%7 value:_______type:_______
教师批改:4 integer
15、By applying the appropriate precedence rules,calculate the result of the following expression:6+5/4-3,result is ___________
教师批改:4
16、By applying the appropriate precedence rules,calculate the result of the following expression:10+9*((8+7)%6)+5*4%3*2+1,result is ___________
教师批改:42
17、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 ___________
教师批改:42
18、Rewrite the following floating-point constants in C‘s form for
scientific notation:29979250000.0_____________
教师批改:29979250000
19、Rewrite the following floating-point constants in C‘s form for
scientific notation:0.00000000529167_____________
教师批改:0.00000000529
20、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 18
21、The variables are initialized as follows,
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 1
22、Indicate the values and types of the following expressions:2%7 value:_______type:_______
教师批改:2 integer
23、By applying the appropriate precedence rules,calculate the result of
the following expression:2+2*(2*2-2)%2/2,result is ___________
教师批改:2
24、The following C program is compiled and runs successfully. Write the
output the following program produces.
#include
void main( )
{
int k = 42;
printf(""%d\n"", k++);___________
printf(""%d\n"", ++k);___________
return 0;
}
教师批改:42 44
25、Write a program that reads in a list of integers until the user enters the value -1 as a sentinel.At that point,the program should display the average of the values entered so far.
Chapter 3 Problem Solving
1、" What is the output of the following code?
#include
void main(void)
{
int a ;
a = 1;
printf(""%i"",++a) ;
}
"
A. Compile-time Error B. 0 C.1 D. 2
教师批改:D
2、Use #define to introduce a constant named pi with the value 3.14159________________
教师批改:#define pi 3.14159
3、Write a printf statement to display the floating-point value stored in the variable distance so that exactly three digits appear to the right of the decimal point___________________________
教师批改:printf("%.3f",distance);
4、Use for control line to count from 15 to 25_______________
教师批改:for(i=15;i<=25;i++)
5、Write a program that displays the message "Hello,world." 10 times on separate 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 Statement Forms
1、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,%d\n”,a,b,c);
else printf(“%d,%d,%d\n”,a,b,c);
else printf(“%d,%d,%d\n”,a,b,c);
}
The output of the program is:
A)1,2,3 B)1,3,2
C)1,3,3 D)3,2,1
教师批改:C
2、Consider the program fragment and give the output:
For (i=0;i<4;i++,i++)
for(k=1;k<3;k++); printf(“*”);
A)******** B)****
C)** D)*
教师批改:D
3、"Variables i, j, and k are integers where i = 2, j = 3 and k = 5
respectively. Do the following C expressions evaluate to true or false?
You answers should be either 1 or 0.
a) (i > k) && ((i + j) == k) _______________
b) ((i+j) != k) || (((j*j) > k) && ((i*i) < k)) ____________
c) k < (j < i) _________________ "
教师批改:0 1 0
4、"The following C program is compiled and runs successfully. Write the
output that this program produces. #include
main()
{
int x = 4;
int y = 4;
if ( (x <= y) && (x >= y) )
printf(“Bingo”);
else
printf(“No go”);
}
教师批改:Bingo
5、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");
}
教师批改:B
6、What are the values of the following statement,assuming that i,j,and
k are declared as integer variables:i=(j=4)*(k=16) i=___________j=_______________,k=_________________
教师批改:64,4,16
7、Write a for loop control line to count from 1 to 100____________
教师批改:for(k=1;k<=100;k++)
8、Write a for loop control line to count backward by twos from 100 to
0________________
教师批改:for(k=100;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 < 10 )
if(j > i) printf("A");
else printf("B");
else
printf("C");
}
教师批改:A
10、What is the output of the following code?
#include
void main() {
int day_of_week = 4;
switch (day_of_week)
{
n"); case 0: printf("today is Sunday.\
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 is Friday.\n");
case 6: printf("today is Saturday.\n");
default:
printf("Something is missing.");
break;
}
}
/*___________________________*/
教师批改:today is Thursday
today 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 NRows 5
void 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. Write the
output the following program produces. #include
void main()
{
int a[6][6],i,j;
for(i=1;i<6;i++)
for(j=1;j<6;j++)
a[i][j]=(i/j)*(j/i);
for(i=1;i<6;i++)
{
for(j=1;j<6;j++)
printf(""%2d"",a[i][j]);
printf(""\n"");
}
}
教师批改:1 0 0 0 0
0 1 0 0 0
0 0 1 0 0
0 0 0 1 0
0 0 0 0 1
13、The following code fragment is placed in a complete program, compiled
and run without errors. Write the output of the following code:
int i;
for(i=10;i>=0;i-=3)
printf("%d",i-1);/*_____________________*/
教师批改:9630
14、How many lines of output does the following program print?
#include
void main( ) {
int i,j;
for(i=0;i<5;i++)
for(j=0;j<5;j++);
printf("Hello world!\n"); }
教师批改:1
15、Complete the program below to produce the following output:
1 x 12 = 12
2 x 12 = 24
3 x 12 = 36
10 x 12 = 120
11 x 12 = 132
12 x 12 = 144
#include
void main(void) {
int i;
for( __________ ; ____________ ; ___________ )
printf(""%d x 12 = %d\n"", __________ , __________ ); }
教师批改:i=1 i<12 i++ i*12 \n
16、The following C program is compiled and runs successfully. Write the output the following program produces.
#define LowerLimit 1
#define UpperLimit 4
#include
void main()
{
int i;
printf("Number Square Cube\n");
for (i = LowerLimit; i <= UpperLimit; i++) {
printf(" %2d %3d %4d\n", i, i * i, i * i * i); } }
教师批改:1 1 1 、2 4 8 、3 9 27
17、While we‘re on the subject of silly songs,another old standby is "This Old Man" for which the first verse is This 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 5-hive 6-sticks 7-heaven 8-pate 9-spine 10-shin
Chapter 5 Function
1、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("%d\n",d);
}
A)10 B)9 C)8 D)7
教师批改:B
2、The following program compiles and executes without errors. Write the
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 %d\n",x,y); y = foo(x);
x = bar(y);
printf("%d %d\n",x,y); }
_________________________ _________________________
教师批改:1 3
1 2
3、The following program compiles and executes without errors. Write the
output of 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 3
3 6
4、The following C program is compiled and runs successfully. Write the
output the following program produces.
int fun(int x, int y, int z)
{ z=x*x+y*y; }
void main( )
{
int a=31;
fun(5,2,a);
printf("%d",a);
}/*_____________*/
教师批改:31
5、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 1 */
if( (x % 2) != 0 )
return (1 + pfft(x - 1));
else
return (3 + pfft(x - 1)); }
void main()
{
printf("%d \n",pfft(7)); }
教师批改:13
6、The following C program is compiled and runs successfully. Write the
output the following program produces.
#include
void main( )
{
int w=2;
void fun(int);
fun(w);
}
void fun(int k)
{ if (k>0) fun(k-1);
printf(“%d”,k);
}
教师批改:0 1 2
7、What output will the following piece of code produce?
void foo(int c)
{
c = 20;
}
void bar(int d[])
{
d[1] = 30;
}
void main(void)
{
int x = 10;
int y = 15;
int a[2] = {1, 2};
int b[2] = {3, 4};
foo(a[0]);
printf("%d %d %d %d %d %d\n", x, y, a[0], a[1], b[0], b[1]);
/*___________________*/
bar(b);
printf("%d %d %d %d %d %d\n", x, y, a[0], a[1], b[0], b[1]);
/*__________________*/
foo(y);
printf("%d %d %d %d %d %d\n", x, y, a[0], a[1], b[0], b[1]);
/*__________________*/
bar(a);
printf("%d %d %d %d %d %d\n", x, y, a[0], a[1], b[0], b[1]);
/*________________________*/ }
教师批改:10 15 1 2 3 4
10 15 1 2 3 30
10 15 1 2 3 30
10 15 1 30 3 30
8、The following C program is compiled and runs successfully. Write the output that this program produces.
#include
void main()
{ int a=1,b=2,c=3;
a+=b+=++c;
printf("%5d%5d%5d\n",a,b,c);//(1)______________________ {
float b=4.0;
int c;
a+=c=5*b;
printf("%5d%5.1f%5d\n",a,b,c);//(2)_____________________ }
printf("%5d%5d%5d\n",a,b,c);(3)_________________________ }
教师批改:(1)7 6 4 (2)27 4.0 20 (3)27 6 4
9、Write a function Fib(n) to calculate the nth Fibonacci number. Chapter 11 Arrays
1、It is an array declaration:
int a[3];
Which one is invalid to refer to particular members of arrays : A\ a[0] B\ a[1*2] C\ a[4-2] D\ a[3]
教师批改:D
2、Assume we have declared the two dimensional array A in the C language, int A[3][2] = { 0, 1, 2, 3, 4, 5 };
Write the value of A[2][1].____________
教师批改:5
3、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 ARRAY_SIZE 100
main( )
{
int list[ARRAY_SIZE];
int count = 0;
double total = 0.0;
while( scanf("%d", &list[ count ])==1)
{
total =____________;
count =____________;
printf("average = %lf \n",______________);
}
教师批改:total=total+list[count];
count=count+1;
total/count
4、This program reads in an array of integers, reverses the elements of
the array, and then displays the elements in their reversed order. Please
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,array[MaxElement];
input (array,MaxElement);
reverse(array,MaxElement);
output (array,MaxElement); }
void input(int a[],int n){
int i;
for(i=0;i
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,%d\n",*p,*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,
教师批改:B
2、1.What’s the result with the following program?( )
#include
void main(){
char *p=“abcdefgh”;
printf(“%d\n”,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(int 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,%4d\n”,a,b,c);
}
A)5,2,3 B)-5,-12,-7 C)-5,-12,-17 D)5,-2,-7
教师批改:B
4、#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
教师批改:A
5、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
教师批改:A
6、"Consider the following code fragment:
int x = 5;
int *y = &x;
int *z;
(*y)++;
z = y;
printf(""%d"", *z);
What is the output? ___________ "
教师批改:6
7、"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[]) {
b[1] = 15;
}
main( ) {
int a[3] = { 3, 4, 5};
int b[3] = { 3, 4, 5}; int *p2;
p2 = &a[0];
(*p2)++;
bar(p2);
printf(""%d %d %d \n"", a[0],a[1],a[2]);
p2 = &b[1];
foo(p2);
bar(p2);
printf(""%d %d %d \n"", b[0],b[1],b[2]);
}
____________________________________
____________________________________ "
教师批改:"4 15 5 \ 3 9 15 "
8、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) illegal (4) 11 (5) 15
9、#include
void try_me(int [ ][3]);
int main(void){
int a[3][3] = {{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 . . . infinity\n",
a[1][0], -a[1][1], a[0][0], a[2][2]); }
教师批改:0 1 2 3 „ infinity
10、" The program below compiles without errors and executes correctly.
Write the output 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 = a[0];
a[0] = a[1];
a[1] = tmp;
}
void swap3(int *a, int *b) {
int tmp = *a;
*a = *b;
*b = tmp;
}
void tripleswap(int *a, int *b, int *c) {
int tmp = *a;
*a = *b;
*c = tmp;
*b = *c;
}
main(){
int a[2];
a[0] = 0;
a[1] = 1;
swap1( a[0], a[1] );
printf(""%d, %d\n"", a[0], a[1] );
swap2( a );
printf(""%d, %d\n"", a[0], a[1] );
swap3( &a[0], &a[1] );
printf(""%d, %d\n"", a[0], a[1] );
a[0] = 0;
a[1] = 1;
tripleswap( &a[0], &a[1], &a[0] );
printf(""%d, %d\n"", a[0], a[1] ); }
_____________________________________________
_____________________________________________
_____________________________________________
_____________________________________________ "
教师批改:"0 1\1 0\0 1\1 0"
11、Design a function prototype that would allow a single function to find
and return simultaneously both the lowest and highest values in an array
of type double.
南昌大学实验
学生姓名: 学号: 专业班级:
实验类型:? 验证 ? 综合 ? 设计 ? 创新
实验日期: 实验成绩:
一、实验项目名称
N Queen Problem
(Machine Practice)
二、实验目的
【Objectives】
The machine problem is to train the student to integrate all the knowledge from the books to solve the problem. To improve the ability of programming in C and active the interesting at C programming language through this integrated experiment.
三、实验
【Contents】:
A square which is composed of n multiplied by n blocks is called “ checkerboard of n elements ”. If two queens are located in
the same line or the same column or even the same diagonal line, we say they attack each other.
Write a C program to display all of the overall arrangements when all of the n queens on the checkerboard don’t attack each
other.
【Steps】:
1. Solutions
a. The queen couldn’t be put in the same row or column, and
couldn’t in the diagonal line.
b. How to make sure that no queen can be put in the same row and column?
We can use an array x[n] to represent the Nth row and x[n]=k to represent that the Nth queen be put in the Kth column. We can use a loop to compare the value of x[i](00, implement.
x[k]?x[k]+1
go to loop when x[k]?n and place (k)=false , implement.
If x[k]?n and if k=n printf(x[])
else k?k+1; else k?k-1
Then end the Algorithms
Secondly: Write a function “place(k)”to check if the Kth queen can be placed in the position of x[k]. ?. [place the first value] i?1.
a. [check the position ]
make a loop. When i
#include
//主函数Solving Problem main()
{ int place(int a[],int j);//函数声明
int x[9],
r,
count=0,
k=1;
变量定义 x[1]?0 k?1 x[1]=0;//
//以下计算全解
while(k>0)//当k>0时,循环
{
x[k]=x[k]+1;//k>0时,执行 x[k]?x[k]+1
while(x[k]<=8&&place(x,k)==0)//当x[k]?n且place函数返回值为假
时, 进入嵌套循环
x[k]=x[k]+1;//执行 x[k]?x[k]+1
if(x[k]<=8)//If x[k]?n
if(k==8)//嵌套if k=n时,输出结果
{
for(r=1;r<9;r++)
printf("%d ",x
);//输出结果
printf("\n");
count++;//结果计数器
}
嵌套else k=k+1;x[k]=0; else //
{
k=k+1;
x[k]=0;
}
else k=k-1;//Else k=k-1
}
printf("8后问共%d组全解\n\n",count);
count=0,//计数器清零
k=1;//变量再次赋值初始化
x[1]=0;//再次赋值初始化
//以下计算有效解
while(k>0)
{
x[k]=x[k]+1;
while(x[k]<=8&&place(x,k)==0)
x[k]=x[k]+1;
if(x[k]<=8)
if(k==8)
{
if(x[1]<=4) //仅此处与求全解的程序段不同,因为有效解中“后”的位置不能超过n/2
{for(r=1;r<9;r++)
printf("%d ",x
);
printf("\n");
count++;}
}
else
{
k=k+1;
x[k]=0;
}
else k=k-1;
}
printf("%d组是有效解\n",count); }
//place函数判断“后”是否能放在某一位置 int place(int a[8],int j)
{
int i=1;//赋值i<-1
while(i
本文档为【南昌大学C程序设计(双语)-作业与答案】,请使用软件OFFICE或WPS软件打开。作品中的文字与图均可以修改和编辑,
图片更改请在作品中右键图片并更换,文字修改请直接点击文字进行修改,也可以新增和删除文档中的内容。
[版权声明] 本站所有资料为用户分享产生,若发现您的权利被侵害,请联系客服邮件isharekefu@iask.cn,我们尽快处理。
本作品所展示的图片、画像、字体、音乐的版权可能需版权方额外授权,请谨慎使用。
网站提供的党政主题相关内容(国旗、国徽、党徽..)目的在于配合国家政策宣传,仅限个人学习分享使用,禁止用于任何广告和商用目的。