当前位置:网站首页>指針:最大值、最小值和平均值
指針:最大值、最小值和平均值
2022-07-06 14:33: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
边栏推荐
- sqqyw(淡然点图标系统)漏洞复现和74cms漏洞复现
- Sqqyw (indifferent dot icon system) vulnerability recurrence and 74cms vulnerability recurrence
- The most popular colloquial system explains the base of numbers
- 函数:求方程的根
- 安全面试之XSS(跨站脚本攻击)
- 《统计学》第八版贾俊平第六章统计量及抽样分布知识点总结及课后习题答案
- 移植蜂鸟E203内核至达芬奇pro35T【集创芯来RISC-V杯】(一)
- Ucos-iii learning records (11) - task management
- Statistics, 8th Edition, Jia Junping, Chapter VIII, summary of knowledge points of hypothesis test and answers to exercises after class
- MySQL learning notes (stage 1)
猜你喜欢
Binary search tree concept
Realize applet payment function with applet cloud development (including source code)
线程的实现方式总结
《统计学》第八版贾俊平第十一章一元线性回归知识点总结及课后习题答案
内网渗透之内网信息收集(四)
Statistics 8th Edition Jia Junping Chapter 5 probability and probability distribution
Hackmyvm target series (1) -webmaster
“人生若只如初见”——RISC-V
“Hello IC World”
记一次api接口SQL注入实战
随机推荐
Ucos-iii learning records (11) - task management
The difference between layer 3 switch and router
Intranet information collection of Intranet penetration (4)
《统计学》第八版贾俊平第十三章时间序列分析和预测知识点总结及课后习题答案
线程的实现方式总结
内网渗透之内网信息收集(一)
Data mining - a discussion on sample imbalance in classification problems
Record an edu, SQL injection practice
Spot gold prices rose amid volatility, and the rise in U.S. prices is likely to become the key to the future
Sentinel overall workflow
SQL注入
Database monitoring SQL execution
Web vulnerability - File Inclusion Vulnerability of file operation
Intranet information collection of Intranet penetration (I)
Markdown font color editing teaching
Statistics, 8th Edition, Jia Junping, Chapter VIII, summary of knowledge points of hypothesis test and answers to exercises after class
AQS details
Sword finger offer 23 - print binary tree from top to bottom
记一次edu,SQL注入实战
使用 flask_whooshalchemyplus jieba实现flask的全局搜索