当前位置:网站首页>指針:最大值、最小值和平均值
指針:最大值、最小值和平均值
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
边栏推荐
- Binary search tree concept
- Build domain environment (win)
- 使用 flask_whooshalchemyplus jieba实现flask的全局搜索
- Hackmyvm target series (2) -warrior
- Intranet information collection of Intranet penetration (5)
- 线程的实现方式总结
- Detailed explanation of network foundation
- WEB漏洞-文件操作之文件包含漏洞
- Statistics 8th Edition Jia Junping Chapter 14 summary of index knowledge points and answers to exercises after class
- 浙大版《C语言程序设计实验与习题指导(第3版)》题目集
猜你喜欢
Sqqyw (indifferent dot icon system) vulnerability recurrence and 74cms vulnerability recurrence
网络基础详解
Interpretation of iterator related "itertools" module usage
Record once, modify password logic vulnerability actual combat
记一次edu,SQL注入实战
Database monitoring SQL execution
On the idea of vulnerability discovery
Wu Enda's latest interview! Data centric reasons
《统计学》第八版贾俊平第十三章时间序列分析和预测知识点总结及课后习题答案
How to understand the difference between technical thinking and business thinking in Bi?
随机推荐
Internet Management (Information Collection)
. Net6: develop modern 3D industrial software based on WPF (2)
《统计学》第八版贾俊平第十二章多元线性回归知识点总结及课后习题答案
Statistics 8th Edition Jia Junping Chapter XIII Summary of knowledge points of time series analysis and prediction and answers to exercises after class
循环队列(C语言)
《统计学》第八版贾俊平第二章课后习题及答案总结
Applet Web Capture -fiddler
《统计学》第八版贾俊平第三章课后习题及答案总结
Intranet information collection of Intranet penetration (I)
How to earn the first pot of gold in CSDN (we are all creators)
The difference between layer 3 switch and router
[err] 1055 - expression 1 of order by clause is not in group by clause MySQL
内网渗透之内网信息收集(二)
浙大版《C语言程序设计实验与习题指导(第3版)》题目集
Feature extraction and detection 14 plane object recognition
JDBC transactions, batch processing, and connection pooling (super detailed)
SQL injection
【指针】删除字符串s中的所有空格
Statistics 8th Edition Jia Junping Chapter 10 summary of knowledge points of analysis of variance and answers to exercises after class
关于超星脚本出现乱码问题