当前位置:网站首页>指针:最大值、最小值和平均值
指针:最大值、最小值和平均值
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
边栏推荐
- Statistics, 8th Edition, Jia Junping, Chapter 6 Summary of knowledge points of statistics and sampling distribution and answers to exercises after class
- Mysql的事务是什么?什么是脏读,什么是幻读?不可重复读?
- Circular queue (C language)
- Feature extraction and detection 14 plane object recognition
- JDBC read this article is enough
- 网络层—简单的arp断网
- 《英特尔 oneAPI—打开异构新纪元》
- Sword finger offer 23 - print binary tree from top to bottom
- 外网打点(信息收集)
- “人生若只如初见”——RISC-V
猜你喜欢
浅谈漏洞发现思路
Statistics 8th Edition Jia Junping Chapter 5 probability and probability distribution
《统计学》第八版贾俊平第十二章多元线性回归知识点总结及课后习题答案
Database monitoring SQL execution
Attack and defense world misc practice area (GIF lift table ext3)
《统计学》第八版贾俊平第十一章一元线性回归知识点总结及课后习题答案
Solutions to common problems in database development such as MySQL
Hackmyvm target series (1) -webmaster
How does SQLite count the data that meets another condition under the data that has been classified once
记一次,修改密码逻辑漏洞实战
随机推荐
图书管理系统
《统计学》第八版贾俊平第十四章指数知识点总结及课后习题答案
浙大版《C语言程序设计实验与习题指导(第3版)》题目集
Library management system
Low income from doing we media? 90% of people make mistakes in these three points
JDBC read this article is enough
安全面试之XSS(跨站脚本攻击)
Harmonyos application development -- address book management system telmanagesys based on listcontainer [phonebook][api v6]
xray与burp联动 挖掘
Record an edu, SQL injection practice
Mysql的事务是什么?什么是脏读,什么是幻读?不可重复读?
Feature extraction and detection 14 plane object recognition
Sword finger offer 23 - print binary tree from top to bottom
[paper reproduction] cyclegan (based on pytorch framework) {unfinished}
Intel oneapi - opening a new era of heterogeneity
7-14 error ticket (PTA program design)
Hackmyvm target series (3) -visions
How to earn the first pot of gold in CSDN (we are all creators)
DVWA (5th week)
MSF generate payload Encyclopedia