程序填空题
题目
使用VC++2010打开考生文件夹下blank1中的解诀方案。此解决方案的项目中包含-个源程序文件blank1.co在此
程序中,函数fun的功能是:计算形参x所指数组中N个数的平均值(规定所有数均为正数),将所指数组中大于平均值的
数据移至数组的前部,小于等于平均值的数据移至x所指数组的后部,平均值作为函数值返回,在主函数中输出平均
值和移动后的数据。
例如,有10个正数: 46、30、32、40、6、17、 45、15、48、26,平均值为30500000.
移动后的输出为: 46、32、 40、45、48、30、6、17、15、 26。
请在程序的下画线处填入正确的内容并把下画线删除,使程序得出正确的结果。
注意:部分源程序在文件blank1 .c中。
不得增行或删行,也不得更改程序的结构!.
代码
#include <stdlib.h>
#include <stdio.h>
#define N 10
double fun(double *x)
{ int i, j; double s, av, y[N];
s=0;
for(i=0; i<N; i++) s=s+x[i];
/**********found**********/
av=__1__;
for(i=j=0; i<N; i++)
if( x[i]>av ){
/**********found**********/
y[__2__]=x[i];
x[i]=-1;}
for(i=0; i<N; i++)
/**********found**********/
if( x[i]!= __3__) y[j++]=x[i];
for(i=0; i<N; i++)x[i] = y[i];
return av;
}
void main()
{ int i; double x[N];
for(i=0; i<N; i++){ x[i]=rand()%50; printf("%4.0f ",x[i]);}
printf("\n");
printf("\nThe average is: %f\n",fun(x));
printf("\nThe result :\n",fun(x));
for(i=0; i<N; i++) printf("%5.0f ",x[i]);
printf("\n");
}
解析
程序修改题
题目
使用VC++2010打开考生文件夹下modi1中的解诀方案。此解决方案的项目中包含- -个源程序文件modi1.co此程
序的功能是:读入一个英文文本行,将其中每个单词的第一个字母改成大写, 然后输出此文本行(这里"单词”是指由空
格隔开的字符串)。
例如,若输入"I am a student to take the examination",则应输出"I Am A Student To Take The Examination"。
请改正程序中的错误,使程序能得出正确的结果。
注意:部分源程序在文件modi1.c中。
不得增行或删行,也不得更改程序的结构!
代码
#include <stdlib.h>
#include <string.h>
#include <conio.h>
#include <ctype.h>
#include <stdio.h>
#include <string.h>
/*************found**************/
void upfst(char p)
{
int k=0;
for ( ;*p;p++)
if (k)
{
if (*p==' ')
k=0;
}
else
{
if (*p!=' ')
{
k=1;
*p=toupper(*p);
}
}
}
void main()
{
char chrstr[81];
system("CLS");
printf("\nPlease enter an English text line: ");
gets(chrstr);
printf("\nBofore changing:\n %s",chrstr);
upfst(chrstr);
printf("\nAfter changing:\n %s\n",chrstr);
}
解析
程序设计题
题目
使用VC++2010打开考生文件夹下prog1中的解决方案。此解决方案的项目中包含-一个源程序文件prog1.c。 在此
序中,定义了NxN的二维数组,并在主函数中赋值。请编写函数fun,函数的功能是:求出数组周边元素的平均值并
作为函数值返回给主函数中的s。例如,若a数组中的值为:
则返回主程序后s的值应为3.375。
注意:部分源程序在文件prog1.c中。
请勿改动主函数main和其他函数中的任何内容,仅在函数fun的花括号中填入所编写的若干语句。
代码
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#define N 5
double fun (int w[][N])
{
}
void main()
{
FILE *wf;
int a[N][N]={0,1,2,7,9,1,9,7,4,5,2,3,8,3,1,4,5,6,8,2,5,9,1,4,1};
int i, j;
double s;
system("CLS");
printf("*****The array*****\n ");
for (i=0; i<N; i++)
{ for (j=0;j<N;j++)
{printf("%4d ",a[i][j]);}
printf("\n ");
}
s=fun(a);
printf("*****THE RESULT*****\n ");
printf("The sum is : %lf\n ",s);
/******************************/
wf=fopen("out.dat","w");
fprintf (wf,"%lf",s);
fclose(wf);
/*****************************/
}