问题 问答题

请编写函数fun(),该函数的功能是:统计各年龄段的人数。N个年龄通过调用随机函数获得,并放在主函数的age数组中。要求函数把0至9岁年龄段的人数在d[0]中,把10至19岁年龄段的人数放在d[1]中,把20至29岁年龄段的人数放在 d[2]中,依次类推,把100岁(含100)以上年龄的人数都放在d[10]中。结果在主函数中输出。 注意:部分源程序给出如下。 请勿改动主函数main和其他函数中的任何内容,仅在函数fun的花括号中填入所编写的若干语句。 试题程序: #include <stdio. h> # define N 50 # define M 11 void fun(int *a, int *b) { } double rnd() { static t=29, c=217,m=1024, r=0; r= (r*t+c)%m; return ((double) r/m); } main ( ) { int age[N], i,d[M]; for(i=0; i<N; i++)age [i]= (int) (i15*rnd ());/*产生一个随机的年龄数组*/ printf ("The original data : \n"); for(i=0; i<N; i++)printf((i+l)%10= =07 "%4d\n":"%4d", age[i]); /*每行输出10个数* / printf ("\n\n"); fun (age, d); for(i=0; i<l0; i++)printf("%4d---%4d :%4d\n", i*l0,i*10+9, d[i] ); printf("Over 100 : %4dkn",d[10]).; }

答案

参考答案:

解析:void fun(int *a, int *b) { int i,j; for (j=0; j<M; j++) b [j]=0; /*数组b初始化为0*/ for (i=0; i<N; i++) if (a [i]>=100) b[10]++; /*如果年龄大于等于100,b[10]自增1*/ else b[a[i]/10]++; /*如果年龄小于100,则将其分别统计到b[a[i]/10]中*/} 本题又是一个分段函数的问题,还是用两个循环来完成。第1个循环的作用是使b的所有元素值都为0。这个循环不能省略,因为若未对b元素赋初值,则它们的值是不可预测的。第2个循环的作用是分别统计a中各年龄段的人数。当a[i]大于等于100时,按题意要将其统计到b[10]中,else的作用是如果年龄小于100,则将其分别统计到b[a[i]/10]中。由运算优先级可知先进行a[i]/10的运算所得结果作为b的下标。若a[i]为0至9时,a[i]/10的值为0,且0至9岁的人数正好要存入b[0]中。若a[i]为10至19岁时a[i]/10的值为1,且10至19岁的人数正好要存入b[1]中,依次类推。

单项选择题 案例分析题
填空题


A How to Use a Painting Knife 使用画刀的方法
Painting with a knife is a bit like putting butter on bread and produces quite a (1) result to a brush. Painting knives are excellent for producing textured, impasto work and (2) areas of flat colour.
What’s the difference between a palette knife and a painting knife
A palette knife is a long, straight spatula that is used (3) mixing paints and scraping a palette clean. They’re made from metal, plastic, or wood and (4) either be completely straight or have a slightly bent handle. A painting knife has (5) large crank in the handle, which takes your hand away from the painting surface. They (6) in numerous shapes(for example pear-, diamond-, or trowel-shaped) and are used for painting (7) of a brush. The edge of the knife is blunt, so that it doesn’t cut the (8) .
·What shape of painting knife should I use
Different shaped painting knives produce different effects. For example, a short blade produces angular strokes (9)a long blade makes it easy to put down sweeps of colour.
·Why can’t I use a palette knife to paint with
You can. Painting (10) just have the advantage of coming in more angular shapes and with sharper points. And (11) larger crank in the handle means there’s less chance of rubbing your knuckles into wet (12) . If you’re unsure whether you’re going to enjoy painting with a knife, first buy a (13) , plastic palette knife and experiment a bit with this before upgrading to a wood-and-metal knife.
·How do I use painting knife
(14) the handle firmly so you’re got good control. Pick up some paint off your palette (15) the tip, as you’d pick up some butter with a knife. Use the side of the (16) to spread paint across your canvas, or press it onto the canvas, as you (17) spread butter across a slice of bread.It’ll seem strange at first as it’s quite (18) to using a brush. Using just the tip of the blade will produce small dots. (19) the edge of the knife down will produce fine lines. Pressing the blade flat down (20) the paint will produce ridges. Scrape back into the paint to reveal underlying layers (called sgraffito).