2017 ip期末答案

Q1

a )

i ) declare a variable $ a $ of type integer and assigning an initial value of 6

ii )declare a variable initial_name of type character and assigning an initial value of 'Z'

iii)declare a pointer $ x $ of type double

iv)declare a function $ powr $ taking two integers as parameters and return a variable of type double

v)declare a three-dimensional arrays $ a $ of type double and the size is 10x6x8

b )

student number :the name of variable can't have the space

bob&alice: the name of variable can't have the sign of '&'

c)

i ) char x = 'a';

ii ) (++n)+3

iii ) int a[2] = {10};

iv ) for (i = 0 \(\color{red}{;}\) i < 5 \(\color{red}{;}\) i++);

v ) if (x \(\color{red}{==}\) 10) printf(‘%d’, x)

Q2

a )

scanf("%d %d %d",&a,&b,&c);

b )

printf("%.2f %.2f %.2f",a,b,c);

c )

i ) \(\color{red}{F}\) Because the grade is smaller than 60 so this equation $ grade >= 60 $ will return false,and it will go to $ printf("F\n") $

ii ) \(\color{red}{b = 1, c = 0}\) Beacuse the equation $ a==1 $ will return 0,that b is the same as before .But the sign behind the $ b=5 $ is \(\color{red}{;}\) .So the code $ c =0; $ will aslo run.Then c is 0.

d )

i ) \(\color{red}{4}\)

ii ) \(\color{red}{4.750000}\)

iii ) \(\color{red}{4.750000}\)

iv ) \(\color{red}{4.000000}\)

v ) \(\color{red}{3}\)

vi ) \(\color{red}{3.800000}\)

Q3

a)

i )

1. int main()
2.{
3. int x;
4. for ( x = 1; x <= 10; x++ ) {// the x is from 1 go to 10
5. if ( x == 5 )//cheak whether x is 5
6. continue;//if x is 5 the go to the start of loop,which means don't output 5 
7. printf( "%d ", x );.// out put x
8. }
9. return 0;
10. }

ii ) \(\color{red}{1{\quad}2{\quad} 3 {\quad}4{\quad}6{\quad}7{\quad}8{\quad}9{\quad}10}\)

b )

i )

1. int powr(int base, int n)// declare a function  powr  taking two integers as parameters and return a integer
3. int i, p=1;
4. for (i=1; i<=n; i++) p=p*base;// let p multiply base for n times,that we can get the base^n
5. return p;
6. }
7. int main()
8. {
9. int a = 4;
10. int b = 2;
11. printf("%d, %d\n", powr(3,b),powr(a,b));//use the fuction powr to calculate the 3^b and a^b 
12. }
  • the sign '^' in this means power but not nor

ii ) \(\color{red}{9,16}\)

c)

i )

if(!(x%3)) printf("it is divisible by number three");

ii )

int sum=0;
for(int i=1;i<=10;++i){
	if(!(i%3)) sum+=i;
}

Q4

a )

int *p=(int *)malloc(20*640*480*8);

b )

\(\color{red}{0}\)
\(\color{red}{8765}\)
\(\color{red}{-987}\)

1. int reverse(int x) {
2. long long tmp = abs((long long)x);//get the absolute value of x
3. long long ret = 0;
4. while (tmp) {// get the tmp divided until it is 0
5. ret = ret * 10 + tmp % 10;//get the number of tmp,and plus the 10
6. if (ret > INT_MAX) return 0; //if the reserve number over integer maximum
7. tmp /= 10;
8. }
9. if (x > 0)//judge if it is postive 
10. return (int)ret;//if it is postive
11. else 
12. return (int)-ret;// if it is negative 
13. }
14. int main (int){
15. int x;
16. scanf ("%d\n", &x);//input x
17. printf ("%d\n",reverse (x));
18. }

c )

1 void main()
2 {
3 int i = 0, j = 0, k = 0, x=9;
4 int arr[3][3][3];
5
6 for(i=0;i<3;i++)//give a start number to arr from 9
7 {
8 for(j=0;j<3;j++)
9 {
10 for(k=0;k<3;k++)
11 {
12 arr[i][j][k] = x++;
13 }
14 }
15 }
16
17 for(i=0;i<3;i++)//reverse the all number of the array arr
18 {
18 for(j=0;j<3;j++)
19 {
20 for(k=0;k<3;k++)
21 {
22 arr[i][j][k] = reverse(arr[i][j][k]);
23 }
24 }
25
26 }
27 for(i=0;i<3;i++)//output the reverse number of array
28 {
29 for(j=0;j<3;j++)
30 {
31 for(k=0;k<3;k++)
32 {
33 printf("%d\t",arr[i][j][k]);
34 }
35 printf("\n");
36 }
37 printf("\n");
38 }
39 }

d )

9	1	11
21	31	41
51	61	71

81	91	2
12	22	32
42	52	62

72	82	92
3	13	23
33	43	53

上一篇:不会真有人觉得聊天机器人难吧——从计算图到自动求导(上)


下一篇:手把手系列--STM32 QSPI操作指南