大连东软信息学院C语言实验三顺序及分支结构程序设计

2024-05-05

大连东软信息学院C语言实验三顺序及分支结构程序设计(精选2篇)

篇1:大连东软信息学院C语言实验三顺序及分支结构程序设计

一、Make a choice 1.For the same data type pointer variable, which operator can notused in C program.A.+

B.-

C.=

D.== 2.when0<=i<10, for the following statements, which reference is wrong to reference array elements.int a[]={1,2,3,4,5,6,7,8,9,0}, *p, i;p=a;A.*(a+i)

B.a[p-a]

C.p+i

D.*(&a[i])3.when0<=i<10, for the following statements, which reference is wrong to reference the address of array elements.int a[]={1,2,3,4,5,6,7,8,9,0}, *p,i;

p=a;

A.&(a+1)

B.a++

C.&p

D.&p[i] 4.What is the result for the following program? #include main(){ int a[]={1,2,3,4,5,6},*p;

p=a;

*(p+3)+=2;printf(“%d,%dn”,*p,*(p+3));}

A.0,5

B.1,5

C.0,6

D.1,6 5.what is the result for the following program? #include main(){ int a[12]={1,2,3,4,5,6,7,8,9,10,11,12}, *p[4],i;for(i=0;i<4;i++)p[i]=&a[i*3];printf(“%dn”,p[3][2]);}

A.输出项不合法

B.6

C.8

D.12 6.For the following statements, what’s the value of(p1-p2)? int a[10], *p1, *p2;p1=a;p2=&a[5];

A.5

B.6

C.10

D.没有指针与指针的减法

7.for this function prototype: void adder(int *ptr,int b);if there are two integers: int op1=2;int op2=4;which statement of function call is right?

A:adder(*op1,op2);B:adder(op1,op2);C:adder(&op1,op2);D:adder(&op1,&op2);8.What does the following program outputs to screen_____ voidToSrn(int *);main(){ int a=8;int *ptr=&a;ToSrn(ptr);} voidToSrn(int *ptr){ printf(“%p”,&a);} A:compile error,can not be run B:8 C:16 D:address of a 9.Assuming int *p,a=4;p=&a;which of the following statements is all means address A:a,p+1 B:&a,*p C:&a,p D:*a,p 10.Assuming inta[10],*p;which one is right A:*p=&a[0];B:*p=a;C:p=a[0];D:p=&a[0];11.Assuming :int a[10],*p=a;which one is the address of a[9] A:a[0]+9 B:&(p+9)C:*(p+9)D:a+9 12.Assuming:

char s1[]=“Hello”,s2[10],*s3=“HelloWorld”,*s4;which one of the following statements is correct? A:strcpy(s1[0],“Morning”);B:s2=“Morning”;C:strcpy(s3,“Morning”);D:strcpy(s4,“Morning”);13.For the following statements,after execution the statement a=p+2;what’s the value of a[0]? float a[3]={1.2,45.6,-23.0};float *p=a;

A.1.2

B.45.6

C.-23.0

D.语句有错

14.What format specifiers(in order)should be used in the printf()statement in thefollowing program? Note that in the program the correct format specifiers have beenreplaced by Z.#include int main(void){ int x = 5;int *x_ptr = &x;printf(“%Z, %Z, %Z, %Zn”, x, *x_ptr, &x, x_ptr);}(a)%f, %p, %d, %p(b)%d, %d, %p, %p(c)%d, %p, %d, %p(d)%p, %d, %d, %p

二、Fill the blank.1.the result of printf(“%sd”,“abcd”);is __【1】___。2.A pointer is a special type of variable which stores the

【1】

of a memory location.3.assuming:int a[]={1,3,5,7,9,11},*p=a;and the value of *p is _【1】__.the value of *(a+5)is __【2】

4.For the following statement, int a[]={8,1,2,5,0,4,7,6,3,9};What’s the value of a[*(a+a[3])]?._____________

二、read the following programs.1.On a machine in which addresses are 4 bytes, what is printed by the following program: #include int main(void){ char fun[] = “Programming is fun.”;char favorite[] = “My favorite class is programming.”;char *x = fun;printf(“%dn”, sizeof(fun));printf(“%dn”, sizeof(favorite));printf(“%dn”, sizeof(x));}

2.What does the following program print? #include int main(void){ int data[] = {1, 2, 3, 4, 5, 6, 7};int *ptr = data;int i;printf(“ i *ptrn--------n”);for(i = 2;i >-4;i--){ printf(“%2d, %2dn”, i, *ptr);ptr+=i;} }

三、write the following program.1.Rewrite the following program such that the function has a return type of void andthe variable y gets its value using pointers.#include intdbl(intnum);int main(void){ int x = 13;x = dbl(x);printf(“x doubled is %dn”, x);} intdbl(intnum){ return 2*num;}

2.Write a program that has a function that when passed a string will print every fourthcharacter of the string.In main()you should create the string ”This is an examplestring.” and pass this to your function to be printed.3.Change the program below to print I love programming.You should do this byusing the values in lovetext to change hatetext.Hint: think about the relationshipbetween the index values of the letters in love and the index values for the word hate.This can be done without creating any new variables.#include int main(void){ int i;charhatetext[] = “I hate programming.”;charlovetext[] = “love”;/* Your code goes here.*/ printf(“%sn”, hatetext);}

4.Write a program that has a function that when passed a string will print every fourthcharacter of the string.In main()you should create the string ”This is an examplestring.” and pass this to your function to be printed./*Void printString(char *p);*/ Void printString(char *p){ Int i=0,len=0,length=strlen(p);While(*p!=NULL&&len

{ Printf(“%c”,*p);P+=4;Len+=4;} } main(){ Char str[]=”This is an examplestring.”;printString(str);} 5./* Write a program.In main(), declare an integer array,and initialize the array through keyboard.Create a multiply()function that when given the array and the number of array elements,in which can double the value of the array elements(that is a[i]*2).Also create another print()function for printing the array elements.Call the two functions in main function.Tip: when running the program, input n with 14.*/

#include

#define N 14

void multiply(int a[],int n);void print(int *p);

main(void){ int a[N],i;

for(i=0;i

{ printf(“n No.%d: ”,i+1);scanf(“%d”,&a[i]);

}

/*(1)call the multiply()function*/

/**********Program**********/ multiply(a,N);

/********** End **********/

/*(2)call the print()function*/

/**********Program**********/ Print(a);

/********** End **********/

}

void multiply(int a[],int n){ int I;

/*(3)double the value of the array elements*/

/**********Program**********/

/********** End ***********/

} void print(int *p){ int i;

printf(“nThe output values after multiply are:n”);

/*(4)output the value of the array elements*/

/**********Program**********/

/********** End **********/

}

1.Write a program that has a function that when passed a string will print every fourthcharacter of the string.In main()you should create the string ”This is an examplestring.” and pass this to your function to be printed./*Void printString(char *p);*/ Void printString(char *p){ Int i=0,len=0,length=strlen(p);While(*p!=NULL&&len

{ Printf(“%c”,*p);P+=4;Len+=4;} } main(){ Char str[]=”This is an examplestring.”;printString(str);} 2./* Write a program.In main(), declare an integer array,and the array

initialize through keyboard.Create a multiply()function that when given the array and the number of array elements, in which can double the value of the array elements(that is a[i]*2).Also create another print()function for printing the array elements.Call the two functions in main function.Tip: when running the program, input n with 14.*/

#include

#define N 14

void multiply(int a[],int n);void print(int *p);main(void){

for(i=0;i

{ printf(“n No.%d: ”,i+1);scanf(“%d”,&a[i]);int a[N],i;

}

/*(1)call the multiply()function*/

/**********Program**********/ multiply(a,N);

/********** End **********/

/*(2)call the print()function*/

/**********Program**********/ Print(a);

/********** End **********/

}

void multiply(int a[],int n){

int I;

/*(3)double the value of the array elements*/

/**********Program**********/

/********** End ***********/

} void print(int *p){

printf(“nThe output values after multiply are:n”);

int i;

/*(4)output the value of the array elements*/

/**********Program**********/

/********** End **********/ }

篇2:大连东软信息学院C语言实验三顺序及分支结构程序设计

1、请根据输入的x值和以下公式,计算输出分段函数y的值。

x10 y0  x10

(x0)(0x1)(x1)

2、编写一个C程序,把整数0,1,..6依次转换成Sunday,Monday,„Saturday并输出,整数0,„,6由键盘输入。如果输入错误,输出“error”。

(提示:此题可以用if语句实现,不需要用循环。)

说明:

(1)请大家按学号来坐,便于考勤和管理。

(2)请珍惜宝贵的实验时间!不要做与实验无关的事情,比如聊QQ、上网或打游戏。

(3)直接把C语言代码粘贴到相应的实验题目下方,上交实验报告时只交word文档。

上一篇:【精华】法人授权委托书下一篇:肿瘤晚期患者姑息护理