问题 问答题

请编写函数fun(),该函数的功能是:将M行N列的二维数组中的数据,按行的顺序依次放到一维数组中,一维数组中数据的个数存放在形参n所指的存储单元中。
例如,若二维数组中的数据为:
33 33 33 33
44 44 44 44
55 55 55 55
则一维数组中的内容应该是33 33 33 33 44 44 44 AA, 55 55 55 55
注意:部分源程序给出如下。
请勿改动主函数main和其他函数中的任何内容,仅在函数fun的花括号中填入所编写的若干语句。
试题程序:
#include <stdio. h>
void fun (int (*s)[10], int *b, int *n,
int mm, int nn)



main ( )

int w[10] [10]=33,33,33,33,44,44,
44,44,55,55,55,55, i, j;
int a[100]=0,n=0 ;
printf ("The matrix: \n" );
for (i=0; i<3; i++)
for (j+0; j<4; j++)
printf ("%3d",w[i] [j] );
printf ("\n");

fun (w,a, &n, 3, 4);
printf ("The A array: In");
for(i=0; i<n; i++)
printf ("%3d", a [i] );
printf ("\n\n");

答案

参考答案:

void fun (int (*s)[A0], int *b, int *n, int mm, int nn)

{

int i, j, k=0;

for (i=0; i <mm; i ++ ) / *将二维数组s中的数据按行的顺序依次放到一维数组b中* /

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

b[k++]=s [i] [j]; /*通过指针返回元素个数*/

*n=k

}

解析:

我们可以用两个循环来处理问题,由于是按行的顺序取出,所以第1个循环用于控制行下标,第2个循环用于控制列下标;若改成按列的顺序取出,则循环应改成:

for(i=0;i<nn;i++)

for(j=0;i<mm;j++)

b[k++]=s[j][i];

要注意s[j][i]的下标,不能用s[i][j]。

若按行取出则列标变化最快,若按列取出则行标变化最快。再根据循环嵌套时,越在内层的循环,其循环变量变化就越快。上题程序中只有当j从头到尾变化完时,i才变化一次(即加1);由于二数组的第1个下标为行下标,第2个下标为列下标,所以第1个程序列变化最快、第2个程序行变化最快。

阅读理解

根据短文内容判断正误,正确A,错误B。

     When you are ill, you will see a doctor. The doctor will write a note to take to the chemist for some

medicine. Chemists are usually good at reading doctor's notes. But sometimes doctors write too badly and

even the chemists can't read them.

     One day a woman wrote to a doctor to invite him to have dinner with her family in the restaurant. The

doctor wrote an answer, but he wrote too badly and the woman could not read it. "What shall I do?" she

asked her husband. "I don't know whether he is going to come or not. I don't want to call him and say that

I don't understand him."

     Her husband thought for a few minutes and then he had and idea."Take it to the chemist," he said. "He

will be able to read it for us." The woman went to the chemist's shop and gave the doctor's"note" to him. The

chemist looked at it for a long time.

     "Could you wait a moment, Miss?" he said. Then he went to the back of the shop. After a few minutes he

came back, carrying a large bottle (瓶子). He gave the bottle to the woman.

     "Take one spoonful (一匙) every day," he told the woman.

( )1. The doctor invited the woman to dinner one day.                             

( )2. The woman couldn 't read the note because the doctor wrote it badly.        

( )3. The husband asked his wife to go to the chemist's shop and get some medicine.

( )4. The chemist gave the woman a large bottle of medicine.                      

( )5. In fact, the chemist didn't know the meaning of the doctor's note at all.   

单项选择题