当前位置:网站首页>指针:最大值、最小值和平均值
指针:最大值、最小值和平均值
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
边栏推荐
- 记一次,修改密码逻辑漏洞实战
- This article explains in detail how mockmvc is used in practical work
- Harmonyos JS demo application development
- Captcha killer verification code identification plug-in
- 数据库多表链接的查询方式
- Statistics 8th Edition Jia Junping Chapter 3 after class exercises and answer summary
- MySQL interview questions (4)
- Record an edu, SQL injection practice
- Detailed explanation of network foundation routing
- 浙大版《C语言程序设计实验与习题指导(第3版)》题目集
猜你喜欢

sqqyw(淡然点图标系统)漏洞复现和74cms漏洞复现

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

Statistics 8th Edition Jia Junping Chapter 3 after class exercises and answer summary

Data mining - a discussion on sample imbalance in classification problems

Apache APIs IX has the risk of rewriting the x-real-ip header (cve-2022-24112)

Xray and Burp linked Mining

JDBC transactions, batch processing, and connection pooling (super detailed)

Based on authorized access, cross host, and permission allocation under sqlserver

How does SQLite count the data that meets another condition under the data that has been classified once

《统计学》第八版贾俊平第八章假设检验知识点总结及课后习题答案
随机推荐
Hackmyvm target series (3) -visions
MySQL interview questions (4)
数据库多表链接的查询方式
Realize applet payment function with applet cloud development (including source code)
内网渗透之内网信息收集(五)
Statistics, 8th Edition, Jia Junping, Chapter 11 summary of knowledge points of univariate linear regression and answers to exercises after class
SQL injection
SystemVerilog discusses loop loop structure and built-in loop variable I
Overview of LNMP architecture and construction of related services
AQS details
[paper reproduction] cyclegan (based on pytorch framework) {unfinished}
Hackmyvm target series (6) -videoclub
Intranet information collection of Intranet penetration (2)
Chain team implementation (C language)
Mysql的事务是什么?什么是脏读,什么是幻读?不可重复读?
Windows platform mongodb database installation
Statistics 8th Edition Jia Junping Chapter 10 summary of knowledge points of analysis of variance and answers to exercises after class
Interview Essentials: what is the mysterious framework asking?
Network technology related topics
XSS (cross site scripting attack) for security interview