当前位置:网站首页>指針:最大值、最小值和平均值
指針:最大值、最小值和平均值
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
边栏推荐
- List and data frame of R language experiment III
- 《统计学》第八版贾俊平第五章概率与概率分布
- DVWA (5th week)
- 循环队列(C语言)
- 【指针】删除字符串s中的所有空格
- Wei Shen of Peking University revealed the current situation: his class is not very good, and there are only 5 or 6 middle-term students left after leaving class
- This article explains in detail how mockmvc is used in practical work
- How to understand the difference between technical thinking and business thinking in Bi?
- 网络基础之路由详解
- Only 40% of the articles are original? Here comes the modification method
猜你喜欢

List and data frame of R language experiment III

内网渗透之内网信息收集(二)

“人生若只如初见”——RISC-V

外网打点(信息收集)

Statistics 8th Edition Jia Junping Chapter XIII Summary of knowledge points of time series analysis and prediction and answers to exercises after class

Record an API interface SQL injection practice

Network technology related topics

Build domain environment (win)

JDBC看这篇就够了

Intranet information collection of Intranet penetration (2)
随机推荐
Statistics, 8th Edition, Jia Junping, Chapter 6 Summary of knowledge points of statistics and sampling distribution and answers to exercises after class
MSF generate payload Encyclopedia
《统计学》第八版贾俊平第四章总结及课后习题答案
How does SQLite count the data that meets another condition under the data that has been classified once
函数:字符串反序存放
Interview Essentials: what is the mysterious framework asking?
Only 40% of the articles are original? Here comes the modification method
Sqqyw (indifferent dot icon system) vulnerability recurrence and 74cms vulnerability recurrence
The difference between layer 3 switch and router
《统计学》第八版贾俊平第二章课后习题及答案总结
攻防世界MISC练习区(gif 掀桌子 ext3 )
记一次api接口SQL注入实战
Ucos-iii learning records (11) - task management
xray與burp聯動 挖掘
内网渗透之内网信息收集(二)
JDBC事务、批处理以及连接池(超详细)
链队实现(C语言)
XSS unexpected event
Apache APIs IX has the risk of rewriting the x-real-ip header (cve-2022-24112)
内网渗透之内网信息收集(三)