当前位置:网站首页>指針:最大值、最小值和平均值
指針:最大值、最小值和平均值
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
边栏推荐
- 《统计学》第八版贾俊平第六章统计量及抽样分布知识点总结及课后习题答案
- JVM memory model concept
- Record an API interface SQL injection practice
- Intel oneapi - opening a new era of heterogeneity
- 攻防世界MISC练习区(gif 掀桌子 ext3 )
- Ucos-iii learning records (11) - task management
- 《统计学》第八版贾俊平第一章课后习题及答案总结
- This article explains in detail how mockmvc is used in practical work
- Uibutton status exploration and customization
- 浙大版《C语言程序设计实验与习题指导(第3版)》题目集
猜你喜欢
Detailed explanation of network foundation
Binary search tree concept
JDBC read this article is enough
攻防世界MISC练习区(SimpleRAR、base64stego、功夫再高也怕菜刀)
《统计学》第八版贾俊平第十二章多元线性回归知识点总结及课后习题答案
《统计学》第八版贾俊平第十章方差分析知识点总结及课后习题答案
JDBC看这篇就够了
xray與burp聯動 挖掘
Résumé des points de connaissance et des réponses aux exercices après la classe du chapitre 7 de Jia junping dans la huitième édition des statistiques
《统计学》第八版贾俊平第三章课后习题及答案总结
随机推荐
外网打点(信息收集)
SQL注入
XSS之冷门事件
Web vulnerability - File Inclusion Vulnerability of file operation
JDBC看这篇就够了
内网渗透之内网信息收集(二)
WEB漏洞-文件操作之文件包含漏洞
Always of SystemVerilog usage_ comb 、always_ iff
Statistics, 8th Edition, Jia Junping, Chapter VIII, summary of knowledge points of hypothesis test and answers to exercises after class
A complete collection of papers on text recognition
Mathematical modeling idea of 2022 central China Cup
Statistics 8th Edition Jia Junping Chapter 10 summary of knowledge points of analysis of variance and answers to exercises after class
Intranet information collection of Intranet penetration (4)
指针:最大值、最小值和平均值
函数:用牛顿迭代法求方程的根
Proceedingjoinpoint API use
SQL injection
{1,2,3,2,5}查重问题
Hackmyvm target series (2) -warrior
“Hello IC World”