Set 63:
In a given program, the function of function fun is: n × N matrix, according to the given value of M (m < = n), move the values in each row of elements to the right by m positions, and set the left to 0. For example, N=3, m=2, there is the following matrix
1 2 3
4 5 6
7 8 9
The program execution result is
0 0 1
0 0 4
0 0 7
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 m) { int i, j; for(i=0; i<N; ___1___ ) { for(j=N-1-m; j>=0; j--) t[i][j+___2___ ]=t[i][j]; for(j=0; j<___3___; j++) t[i][j]=0; } } main() { int t[][N]={21,12,13,24,25,16,47,38,29,11,32,54,42,21,33,10}, i, j, m; printf("\nThe original array:\n"); for(i=0; i<N; i++) { for(j=0; j<N; j++) printf("%2d ",t[i][j]); printf("\n"); } printf("Input m (m<=%d): ",N);scanf("%d",&m); fun(t,m); 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:
The first place: the increment of the for loop variable, so it should be filled in: i + +.
The second place: m should be filled because it moves m positions to the right.
The third place: the left m columns are set to 0, so the termination value of the for loop should be m.
Given program modi1 The function of function fun in C is to calculate and output the sum of the largest 10 primes within high. The value of high is passed from the main function to the fun function. If the value of high is: 100, the value of the function is: 732.
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> int fun( int high ) { int sum = 0, n=0, j, yes; while ((high >= 2) && (n < 10) { yes = 1; for (j=2; j<=high/2; j++ ) if (high % j ==0 ){ yes=0; break } if (yes) { sum +=high; n++; } high--; } return sum ; } main ( ) { printf("%d\n", fun (100)); }
Problem solving ideas:
First place: parentheses do not match. Error: While statement missing ) in function fun
While statement is missing in function fun).
Second: lack of semicolon. Error: Break statement missing ; in function fun
The break statement is missing in the function fun;
This type of question can find the error of the program as long as it is compiled.
Write the function fun. Its function is to solve the problem by using the simple iterative method shown below
Equation: a real root of cos(x)-x=0. The iterative steps are as follows:
(1) Take the initial value of x1 as 0.0;
(2) x0 = x1, assign the value of X1 to x0;
(3) x1 = cos(x0), find a new x1;
(4) If the absolute value of x0 - x1 is less than 0.00000 1, execute step (5), otherwise execute step (2);
(5) x1 is a real root of the equation cos(x)-x=0, which is returned as a function value. The program will output the result Root=0.739085.
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> float fun() { } main() { printf("Root =%f\n", fun()); NONO(); }
Problem solving ideas:
This problem is to calculate a real root of the equation according to the given formula and conditions.
Reference answer:
float fun() { float x0, x1=0.0; do { x0=x1; x1=cos(x0); } while(fabs(x0-x1)>0.000001); return x1; }