当前位置:网站首页>指针:最大值、最小值和平均值
指针:最大值、最小值和平均值
2022-07-06 09:24:00 【|光|】
要求
编写程序,输入一个一维整型数组,输出其中的最大值、最小值和平均值。(用指针实现)
代码
#include<stdio.h>
/* * 该函数用来输入一维整数数组,输入的数据存放在形参a数组中 * 输入的数据以0作为结束标志,0不存入数组也不计入输入数据的总数 * 返回值为输入数据的个数 */
int input(int a[])
{
int n=0;
int b = 0;
do
{
scanf("%d",&b);
if(b == 0)
break;
else
{
a[n] = b;
n++;
}
}
while(b != 0);
return n;
}
/* * 该函数用来实现计算形参数组a中的最大值、最小值、平均值 * 最大值、最小值、平均值分别通过形参指针变量pmax、pmin、pavg来传递 * n为形参数组a中数据的个数 */
void fun(int a[],int *pmax,int *pmin,int *pavg,int n)
{
int i,j,k=0;
*pmax = a[0];
*pmin = a[0];
for(i=1;i<n;i++)
{
if(*pmin > a[i])
{
*pmin = a[i];
}
if(*pmax < a[i])
{
*pmax = a[i];
}
k = k + a[i];
*pavg = (k + a[0])/n;
}
}
main函数
int main()
{
int a[200],n,max,min,avg;
n=input(a);
fun(a,&max,&min,&avg,n);
printf("最大值为%d,最小值为%d,平均值为%d\n",max,min,avg);
return 0;
}
测试
测试输入
1 3 5 7 0
输出
最大值为7,最小值为1,平均值为4
边栏推荐
- 《统计学》第八版贾俊平第十二章多元线性回归知识点总结及课后习题答案
- 【指针】求二维数组中最大元素的值
- Low income from doing we media? 90% of people make mistakes in these three points
- 移植蜂鸟E203内核至达芬奇pro35T【集创芯来RISC-V杯】(一)
- Lintcode logo queries the two nearest saplings
- Hackmyvm target series (7) -tron
- Statistics 8th Edition Jia Junping Chapter 12 summary of knowledge points of multiple linear regression and answers to exercises after class
- How to understand the difference between technical thinking and business thinking in Bi?
- Statistics 8th Edition Jia Junping Chapter 14 summary of index knowledge points and answers to exercises after class
- How to turn wechat applet into uniapp
猜你喜欢
《统计学》第八版贾俊平第十章方差分析知识点总结及课后习题答案
SystemVerilog discusses loop loop structure and built-in loop variable I
《统计学》第八版贾俊平第二章课后习题及答案总结
攻防世界MISC练习区(gif 掀桌子 ext3 )
Statistics, 8th Edition, Jia Junping, Chapter 11 summary of knowledge points of univariate linear regression and answers to exercises after class
JVM memory model concept
内网渗透之内网信息收集(四)
《统计学》第八版贾俊平第六章统计量及抽样分布知识点总结及课后习题答案
Mysql的事务是什么?什么是脏读,什么是幻读?不可重复读?
Harmonyos JS demo application development
随机推荐
Detailed explanation of network foundation
Mysql的事务是什么?什么是脏读,什么是幻读?不可重复读?
Xray and Burp linked Mining
MySQL learning notes (stage 1)
Intel oneapi - opening a new era of heterogeneity
C language file operation
《统计学》第八版贾俊平第十一章一元线性回归知识点总结及课后习题答案
内网渗透之内网信息收集(二)
MSF generate payload Encyclopedia
Captcha killer verification code identification plug-in
四元数---基本概念(转载)
移植蜂鸟E203内核至达芬奇pro35T【集创芯来RISC-V杯】(一)
Mathematical modeling idea of 2022 central China Cup
1.支付系统
Detailed explanation of network foundation routing
Hackmyvm target series (6) -videoclub
Sword finger offer 23 - print binary tree from top to bottom
Intranet information collection of Intranet penetration (3)
记一次,修改密码逻辑漏洞实战
“人生若只如初见”——RISC-V