National Computer Rank Examination Question Base Level 2 C Operational Questions 100 sets (Set 41)

Set 41:

The function of a function fun in a given program is to calculate the average of N numbers in the array indicated by x (specifying that all numbers are positive), return the main function by a parameter, return the number that is less than the average and closest to the average as the function value, and output it in the main function.
For example, there are 10 positive numbers: 46 30 32 40 6 17 45 15 48 26, with an average of:
30.500000
Output in main function: m=30.0
Fill in the correct content at the underline of the program and remove the underline to get the correct result.
Note: The source program is stored in BLANK1 in the candidate folder. C.
Do not add or delete lines or change the structure of the program!
Given source program:

#include <stdlib.h> 
#define N 10 
double fun(double x[],double *av) 
{ int i,j; double d,s; 
s=0; 
for(i=0; i<N; i++) s = s +x[i]; 
__1__=s/N; 
d=32767; 
for(i=0; i<N; i++) 
if(x[i]<*av && *av - x[i]<=d){ 
d=*av-x[i]; j=__2__;} 
return __3__; 
} 
main() 
{ int i; double x[N],av,m; 
for(i=0; i<N; i++){ x[i]=rand()%50; printf("%4.0f ",x[i]);} 
printf("\n"); 
m=fun(x,&av); 
printf("\nThe average is: %f\n",av); 
printf("m=%5.1f ",m); 
printf("\n"); 
} 

Solving ideas:
First, the calculated mean is returned by the parameter av, so it should be filled in: *av.
Second: Calculate the position j below and closest to the mean, so fill in: i.
Third: Returns the number, so fill in: x[j].

Given program MODI1. The function fun in C is to calculate the following values according to the integer parameter n. For example, if n=10, the output would be 0.617977.
Correct the grammar errors in the program so that it produces the correct results.
Note: Do not change the main function, add or delete lines, or change the structure of the program!
Given source program:

#include <stdio.h> 
int fun ( int n ) 
{ float A=1; int i; 
for (i=2; i<n; i++) 
A = 1.0/(1+A); 
return A ; 
} 
main( ) 
{ int n ; 
printf("\nPlease enter n: ") ; 
scanf("%d", &n ) ; 
printf("A%d=%f\n", n, fun(n) ) ; 
} 

Solving ideas:
First, the function returns a floating-point number, so it should be changed to float fun(int n).
Second, the termination condition for should be i<=n.

Program defines N × A two-dimensional array of N, which is automatically assigned in the main function. Write the function fun, which multiplies the values in the upper right triangle element of the array by m.
For example, if m has a value of 2, the value in the a array is
| 1 9 7 | | 2 18 14|
a = | 2 3 8 | The value in the a array after returning to the main program should be | 2 6 16 |
| 4 5 6 | | 4 5 12|
Note: Some source programs have file PROG1.C file.
Don't change anything in the main function or other functions, just fill in the curly brackets of the function fun with a few statements you've written.
Given source program:

#include <stdio.h> 
#include <stdlib.h> 
#define N 5 
int fun ( int a[][N], int m ) 
{ 
} 
main ( ) 
{ int a[N][N], m, i, j; 
printf("***** The array *****\n"); 
for ( i =0; i<N; i++ ) 
{ for ( j =0; j<N; j++ ) 
{ a[i][j] = rand()%20; printf( "%4d", a[i][j] ); } 
printf("\n"); 
} 
do m = rand()%10 ; while ( m>=3 ); 
printf("m = %4d\n",m); 
fun ( a ,m ); 
printf ("THE RESULT\n"); 
for ( i =0; i<N; i++ ) 
{ for ( j =0; j<N; j++ ) printf( "%4d", a[i][j] ); 
printf("\n"); 
} 
NONO ( ); 
} 

Solving ideas:
This question uses a double loop to multiply the value in the upper right triangle element of a two-dimensional array by m.
Reference Answer:

int fun ( int a[][N], int m ) 
{ 
int i, j; 
for(i = 0 ; i < N ; i++) 
for(j = i ; j < N ; j++) 
a[i][j] *= m ; 
} 

Added by Mistah Roth on Thu, 03 Mar 2022 20:15:12 +0200