Current location - Education and Training Encyclopedia - University rankings - Recommend some exercises about C language arrays.
Recommend some exercises about C language arrays.
Computer problems in Nankai in 2006

1: The scores of m people in question 1 are stored in the score array. Please write the function fun, whose function is to return people with low average scores as function values, and put people with low average scores in the function specified below.

Answer: int fun (int score [], int m, int below [])

{

int i,k=0,aver = 0;

for(I = 0; I < m; i++)

aver+= score[I];

aver/= m;

for(I = 0; I < m; i++)

If (fraction [I]; n .p=m! /n! (m-n)!

Answer: float fun(int m, int n)

{

float p,t = 1.0;

int I;

for(I = 1; I < = m; i++)

t = t * I;

p = t;

for(t= 1.0,I = 1; I < = n; i++)

t = t * I;

p = p/t;

for(t= 1.0,I = 1; I < = m-n; i++)

t = t * I;

p = p/t;

Return p;

}

10: Write the function fun in the question 10. Its function is to find the real root of the equation cos(x)-x=0 by the following simple iterative method.

The iterative steps are as follows: (1) Take the initial value of x 1 as 0.0; (2)x0=x 1, and assign the value of x 1 to x0;

(3)x 1=cos(x0), and find a new x1;

(4) If the absolute value of x0-x 1 is less than 0.00000 1, execute step (5), otherwise execute step (2);

(5) x 1 is the real root of the equation cos(x)-x=0, which is returned as a function value.

The program will output root=0.739085.

Answer: Gone with the Wind ()

{

float x 1=0.0,x0;

do

{

x0 = x 1;

x 1 = cos(x0);

}

while(fabs(x0-x 1)> 1e-6);

Returns x1;

}

1 1: Question 1 1 The following program defines an n×n two-dimensional array and automatically assigns values in the main function. Please write the function fun(int a[][n]), which is used to set all the values in the lower left triangle element of the array to 0.

Answer: int fun(int a[][N])

{

int I,j;

for(I = 0; I < N; i++)

for(j = 0; j < = I; j++)

a[I][j]= 0;

}

12: Question 12 The following program defines an n×n two-dimensional array and assigns values in the main function. Please write the function fun, whose function is to find the average of the elements around the array and return it to S as the function value in the main function.

Answer: double fun(int w[][N])

{

int I,j,k = 0;

Double s = 0.0

for(j = 0; j < N; j++)

{

s+= w[0][j];

k++;

}

for(j = 0; j < N; j++)

{

s+= w[N- 1][j];

k++;

}

for(I = 1; I < = N-2; i++)

{

s+= w[I][0];

k++;

}

for(I = 1; I < = N-2; i++)

{

s+= w[I][N- 1];

k++;

}

Returns s/= k;

}

13: Question 13, please write a function Voidfun (int TT [m] [n], int pp [n]), TT points to a two-dimensional function group with m rows and n columns, find out the smallest elements in each column of the two-dimensional function group, and put them into the one-dimensional array specified by pp in turn. The numbers in the two-dimensional array have been given in the main function.

Answer: void fun(int tt[M][N], int pp[N])

{

int I,j,min

for(j = 0; j < N; j++)

{

min = TT[0][j];

for(I = 0; I < M; i++)

{

if (tt[i][j]