简答题 C语言
题号 #2253916

请编写函数proc(),它的功能是计算:
s=(1-ln(1)-ln(2)-ln(3)-…-In(m))2
S作为函数值返回。
在C语言中可调用log(n)函数求in(n)。109函数的弓用说明是double log(double x)。
例如,若m的值为10,则proc()函数值为198.934454。
注意:部分源程序给出如下。
请勿改动main()函数和其他函数中的任何内容,仅在函数proe()的花括号中填入所编写的若干语句。
试题程序:
#include<stdlib.h>
#include<conio.h>
#include<stdio.h>
#include<math.h>
double proc(int m)
{
}
void main()
{
system("CLS"):
printf("%f\n",proc(10)):
}

进入在线模考

参考答案

暂无

答案解析

double proc(int m)
{
int i;
double s=1.0;//s的初值赋为1,
for(i=1;i<=m;i++)//从1到m
s=s-log(i); //让其求得的差放到S中
return(s*s); //最后把其平方值返回到主函数中
}
【解析】首先我们应定义一个变量来表示其和,一个来表示其项数,然后通过循环,求得其差值,最后把和的平方返回到主函数中。

你可能感兴趣的试题

1 字符串str由数字字符组成(长度不超过5个字符),可看做任意进制的数,请补充函数proc(),该函数的功能是:把str字符串从二进制转换卜进制的数,结果保存在数组。x 中,由函数返回转换后数组xx的实际长度。其中。表示str 原来的进制,y表示要转换成的进制。例如,输入str=”1011”,x=2,y=10,结果输出:11。
注意:部分源程序给出如下。
请勿改动main()函数和其他函数中的任何内容,仅在函数proc()的横线上填入所编写的若干表达式或语句。
试题程序:
#include<stdio.h>
#include<stdlib.h>
#inelude<string.h>
#define M 8
int xx[M];
int proc(char*str,int x,int y)
{
int sum;
int i=0:
char*p=str;
for(i=0;i<M;i++)
xx[i]=0:
sum= 【1】 :
p++;
while(*p)
{
sum=sum*x+*p-'0';
P++:
}
i=0;
while(sum!=O)
{
xx[i]=【2】 ;
【3】 ;
i++:
}
return i;
}
void main()
{
char str[6];
int i;
int n;
int x;
int y;
printf("Enter a string made up of'0' to '9'digits
character:");
gets(str);
if(strlen(str)>5)
{
printf("Error:string too longer!,please input a
gain!\n\n");
exit(0);
}
for(i=0;str[i];i++)
if(str[i]<'0'|| str[i]>'9')
{
printf("Error:%c not is '0'to '9' digits character!\n\n",str[i]);
exit(0);
}
printf("The,original string:");
puts(str);
printf("\nINPUT x=");
scanf("%d".&x);
printf("\nINPUT y=");
scanf("%d".&y);
n=proc(str.x,y);
printf("\n%s is convered to",str);
for(i=n-l:i>=0;i--)
printf("%d\n",xx[i]/n);
}
找试题 · 找答案 · 找资料 在线模考