1、编写自定义函数average,求2个数的平均值。(C语言编写代码)

1、编写自定义函数average,求2个数的平均值。(C语言编写代码)1、编写自定义函数average,求2个数的平均值。
(提示:2个数由主函数随机提供,子函数average返回平均值。)
用C语言编写代码

参考代码如下:

#include <stdio.h>
#include<stdlib.h>
#include<time.h>

double average(int a,int b);

int main()
{
int a,b;
srand(time(0));
a=rand()%100;
b=rand()%100;
printf("First Data=%d\nSecond Data=%d\n",a,b);
printf("Average=%.1f",average(a,b));

}

double average(int a,int b){
return (float)(a+b)/2.0;
}

执行效果如下:

温馨提示:答案为网友推荐,仅供参考
第1个回答  2016-12-13
float average(int a,int b)
{
return (a+b)/2.0;
}
相似回答