100 sets of grade II C operation questions in the national computer grade examination question bank (the 96th set)

Posted by cl_stef on Fri, 18 Feb 2022 19:25:52 +0100

Set 96:

In a given program, the function of function fun is: n × N matrix, rotate the peripheral elements of the matrix clockwise. The operation sequence is as follows: first, store the values of the elements in the first row into the temporary array r, then make the first column the first row, the last row the first column, the last column the last row, and the elements in the temporary array the last column.
For example, if N=3, there is the following matrix:
1 2 3
4 5 6
7 8 9
The calculation result is
7 4 1
8 5 2
9 6 3
Please fill in the correct content in the underline of the program and delete the underline to make the program get the correct result.
Note: the source program is stored in blank1 under the examinee folder In C.
Do not add or delete lines, nor change the structure of the program!
Given source program:

#include <stdio.h> 
#define N 4 
void fun(int (*t)[N]) 
{ int j ,r[N]; 
for(j=0; j<N; j++) r[j]=t[0][j]; 
for(j=0; j<N; j++) 
/
t[0][N-j-1]=t[j][___1___ ]; 
for(j=0; j<N; j++) 
t[j][0]=t[N-1][j]; 
/
for(j=N-1; j>=0;___2___ ) 
t[N-1][N-1-j]=t[j][N-1]; 
for(j=N-1; j>=0; j--) 
/
t[j][N-1]=r[___3___]; 
} 
main() 
{ int t[][N]={21,12,13,24,25,16,47,38,29,11,32,54,42,21,33,10}, i, j; 
printf("\nThe original array:\n"); 
for(i=0; i<N; i++) 
{ for(j=0; j<N; j++) printf("%2d ",t[i][j]); 
printf("\n"); 
} 
fun(t); 
printf("\nThe result is:\n"); 
for(i=0; i<N; i++) 
{ for(j=0; j<N; j++) printf("%2d ",t[i][j]); 
printf("\n"); 
} 
} 

Problem solving ideas:
Place 1: save the number in column 1 on line 1, so it should be filled in: 0.
The second place: the increment value of the for loop. Since the loop decreases from large to small, it should be filled in: j -.
The third place: make the elements in the temporary array the last column, so you should fill in: j.

Given program modi1 The function of function fun in C is to calculate the value of S = f(-n) + f(-n+1) +... + f(0) + f(1) + f(2) +... + f(n). For example, when n is 5, the function value should be: 10.407143. The f(x) function is defined as follows:
┌ (x + 1) / (X-2) x > 0 and X ≠ 2
f(x) = 0 x=0 or x=2
└(x-1)/(x-2) x<0
Please correct the errors in the program so that the program can output the correct results.
Note: do not change the main function, do not add or delete lines, and do not change the structure of the program!
Given source program:

#include <stdio.h> 
#include <math.h> 
f( double x) 
{ 
if (x == 0.0 || x == 2.0) 
return 0.0; 
else if (x < 0.0) 
return (x -1)/(x-2); 
else 
return (x +1)/(x-2); 
} 
double fun( int n ) 
{ int i; double s=0.0, y; 
for (i= -n; i<=n; i++) 
{y=f(1.0*i); s += y;} 
return s 
} 
main ( ) 
{ 
printf("%f\n", fun(5) ); 
} 

Problem solving ideas:
First: since the return value is a real value, double is added before the function name.
Second: the semicolon is missing after the statement.

Write the function fun, whose function is to calculate: s is returned as the function value. In C language, the log (n) function can be called to find ln (n). The reference description of the log function is:
double log(double x).
For example, if the value of m is: 20, the value of fun function is: 6.506583.
Note: some source programs are in the file prog1 In C.
Do not change anything in the main function and other functions. Only fill in the curly brackets of the function fun with some statements you have written.
Given source program:

#include <math.h> 
#include <stdio.h> 
double fun( int m ) 
{ 
} 
main() 
{ 
printf("%f\n", fun(20)); 
NONO(); 
} 

Problem solving ideas:
This question is calculated according to the given formula. Use the for loop statement to find the value of each item in turn, accumulate and
Store the result in the variable s, and then find the square root of s after the loop is completed. The result is still stored in s, and finally return to s.
Reference answer:

double fun( int m ) 
{ 
double s = 0.0 ; 
int i ; 
for(i = 1 ; i <= m ; i++) s += log(1.0 * i) ; 
s = sqrt(s) ; 
return s ; 
}