有几道简单的C语言的题,请大家帮帮忙帮我做一下,谢谢。
2.编写函数,根据整形形参n的值,计算如下公式的值1-1/2+1/3-1/4+1/5-1/6+1/7……..+(-1)n+11/n3.请编写函数,其功能是对传送过来的两个...
2.编写函数,根据整形形参n的值,计算如下公式的值
1-1/2+1/3-1/4+1/5-1/6+1/7……..+(-1)n+11/n
3.请编写函数,其功能是对传送过来的两个浮点数求出和值与差值,并通过形参传送回调用函数。
4.请编写函数,对传送过来的三个数选出最大数和最小数,并通过形参传回调用函数。 展开
1-1/2+1/3-1/4+1/5-1/6+1/7……..+(-1)n+11/n
3.请编写函数,其功能是对传送过来的两个浮点数求出和值与差值,并通过形参传送回调用函数。
4.请编写函数,对传送过来的三个数选出最大数和最小数,并通过形参传回调用函数。 展开
3个回答
展开全部
2.
用double 就行了
3. double sum(double a,double b)
{
return a+b;
}
double lose(double a,doulbe b)
{
if(a>b) return a-b;
else return b-a;
}
4.
int cmpbig(int a,int b,int c)
{
if(a>b && a>c) return a;
else if(b>a && b>c) return b;
else if(c>a && c>b) return c;
}
int cmpsmall(int a,int b,int c)
{
if(a<b && a<c) return a;
else if(b<a && b<c) return b;
else if(c<a && c<b) return c;
}
用double 就行了
3. double sum(double a,double b)
{
return a+b;
}
double lose(double a,doulbe b)
{
if(a>b) return a-b;
else return b-a;
}
4.
int cmpbig(int a,int b,int c)
{
if(a>b && a>c) return a;
else if(b>a && b>c) return b;
else if(c>a && c>b) return c;
}
int cmpsmall(int a,int b,int c)
{
if(a<b && a<c) return a;
else if(b<a && b<c) return b;
else if(c<a && c<b) return c;
}
展开全部
1,
#include<stdio.h>
float foo(int n)
{
int i;
float f = -1, sum = 0;
for (i = 1; i <= n; i++) {
sum += f / i;
f = -f;
}
return sum;
}
int main(void)
{
int n;
printf("Input n:");
scanf("%d", &n);
printf("%.2f\n", foo(n));
return 0;
}
2,
#include<stdio.h>
void foo(float a, float b, float *sum, float *sub)
{
*sum = a + b;
*sub = a - b;
}
int main(void)
{
float a, b, c, d;
printf("Input 2 float:");
scanf("%f%f", &a, &b);
foo(a, b, &c, &d);
printf("sum=%.2f, sub=%.2f\n", c, d);
return 0;
}
3,
#include<stdio.h>
void foo(int a, int b, int c, int *max, int *min)
{
if (a > b) {
*max = a > c ? a : c;
*min = b < c ? b : c;
} else {
*max = b > c ? b : c;
*min = a < c ? a : c;
}
}
int main(void)
{
int a, b, c, max, min;
printf("Input 3 int:");
scanf("%d%d%d", &a, &b, &c);
foo(a, b, c, &max, &min);
printf("max=%d, min=%d\n", max, min);
return 0;
}
#include<stdio.h>
float foo(int n)
{
int i;
float f = -1, sum = 0;
for (i = 1; i <= n; i++) {
sum += f / i;
f = -f;
}
return sum;
}
int main(void)
{
int n;
printf("Input n:");
scanf("%d", &n);
printf("%.2f\n", foo(n));
return 0;
}
2,
#include<stdio.h>
void foo(float a, float b, float *sum, float *sub)
{
*sum = a + b;
*sub = a - b;
}
int main(void)
{
float a, b, c, d;
printf("Input 2 float:");
scanf("%f%f", &a, &b);
foo(a, b, &c, &d);
printf("sum=%.2f, sub=%.2f\n", c, d);
return 0;
}
3,
#include<stdio.h>
void foo(int a, int b, int c, int *max, int *min)
{
if (a > b) {
*max = a > c ? a : c;
*min = b < c ? b : c;
} else {
*max = b > c ? b : c;
*min = a < c ? a : c;
}
}
int main(void)
{
int a, b, c, max, min;
printf("Input 3 int:");
scanf("%d%d%d", &a, &b, &c);
foo(a, b, c, &max, &min);
printf("max=%d, min=%d\n", max, min);
return 0;
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
楼上一定没读过书,题目意思都不看清楚。
2的最后表达式和前面序列不一样。我是按前面表达式写的。
ide:codeblock
#include <iostream>
using namespace std;
double returnNValue(int n)
{
double num = 0;
for(int i = 1; i <= n; i++)
{
double t = (double)i;
if(n%2)
num += 1/t;
else
num += -1*(1/t);
}
return num;
}
void cal(double a,double b,double &sum,double &dif)
{
sum = a + b;
dif = a - b;
}
void getMaxAndMin(double a,double b,double c,double &max,double &min)
{
if(a >= b)
{
if(a >= c)
max = a;
else
max = c;
if(b <= c)
min = b;
else
min = c;
}
else
{
if(b >= c)
max = b;
else
max = c;
if(a <= c)
min = a;
else
min = c;
}
}
int main()
{
cout<<returnNValue(11)<<endl;
double a = 10;
double b = 50;
double sum;
double dif;
cal(a,b,sum,dif);
cout<<"sum:"<<sum<<"dif:"<<dif<<endl;
double c = 30;
double max;
double min;
getMaxAndMin(a,b,c,max,min);
cout<<"max:"<<max<<"min:"<<min<<endl;
return 0;
}
2的最后表达式和前面序列不一样。我是按前面表达式写的。
ide:codeblock
#include <iostream>
using namespace std;
double returnNValue(int n)
{
double num = 0;
for(int i = 1; i <= n; i++)
{
double t = (double)i;
if(n%2)
num += 1/t;
else
num += -1*(1/t);
}
return num;
}
void cal(double a,double b,double &sum,double &dif)
{
sum = a + b;
dif = a - b;
}
void getMaxAndMin(double a,double b,double c,double &max,double &min)
{
if(a >= b)
{
if(a >= c)
max = a;
else
max = c;
if(b <= c)
min = b;
else
min = c;
}
else
{
if(b >= c)
max = b;
else
max = c;
if(a <= c)
min = a;
else
min = c;
}
}
int main()
{
cout<<returnNValue(11)<<endl;
double a = 10;
double b = 50;
double sum;
double dif;
cal(a,b,sum,dif);
cout<<"sum:"<<sum<<"dif:"<<dif<<endl;
double c = 30;
double max;
double min;
getMaxAndMin(a,b,c,max,min);
cout<<"max:"<<max<<"min:"<<min<<endl;
return 0;
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询